I downloaded a bullet model from TurboSquid.
But the bullet is pointing to the left (negtive X axis).
So the whole thing needed to be turned 90 degree on Y axis to face "forward" (Z axis).
I'm making a first-person shooting game so I needed the bullet to face exactly where the camera was facing.
So if I simply set
bullet.gameObject.transform.rotation = Camera.main.transform.rotation;
The bullet will just facing "left"!
I tried to do a additional rotation after the rotation assignment as follow:
bullet.gameObject.transform.Rotate(Camera.main.transform.up,90);
It worked fine if the view is parallel to the horizon.
But it will started to facing some weird ways if you're shooting up or down!
I also tried to create an empty parent GameObject and throw the bullet in as its child, and set the parent's rotation.
Now it "always" facing "forward" (Z axis)! No matter where I turned my camera!
Could somebody please be so kind and teach me how to fix this!?
Much appreciated!
Wrap your model in a parent object, like this:
Then you can rotate the BulletModel 90 degrees and attach the script that moves the object to the ParentBullet. This should do the trick.
It's usually considered good practice overall to separate your model and logic/actual object like this.
I fixed it by some weird ways!
I just set its forward to the camera's right, and its right to the camera's back.
bullet.gameObject.transform.forward = Camera.main.transform.right;
bullet.gameObject.transform.right = -Camera.main.transform.forward;
But I still don't know why the empty parent GameObject trick don't work!?
Related
I am working on an Asteroids clone in 3D with top-down camera. This setup is static and will not change (so no, I do not want to transform the project to 2D game instead).
This means that I need to limit all movement to X and Y axis. I created movement for both asteroids and player and everything worked fine. All movements are done using AddForce on respective RigidBody components.
The problem is then I start dealing with collisions. I use Mesh Collider components to get a nice and precise "touch reaction". The problem is that when collision like this occurs, the new movement vector has Z value different from 0. This is a problem as the object will start moving on Z axis.
What have I tried:
Freezing constraints on RigidBody
Manully reseting Z in Update function
The first solution (freezing constraints) did not work and neither did the second one (moreover, the second one seems quite messy)
So the question is
What would be the optimal way to force physics-based movement only to X and Y axis while using precise collision with Mesh Colliders?
are you sure you used the position restriction correctly? You can check to set the restrictions with a vector as in the documentation. https://docs.unity3d.com/ScriptReference/RigidbodyConstraints.FreezePosition.html
to see how its done. If not please share the code or a screenshot of the rigidbody restrictions you tried in the editor
Hi I'm just new to unity2d, so I have a gameObject that has an hinge joint attached to another gameObject with rigidbody, Im trying to make a Wooden board that will behave like a "seesaw" and return back to its current position when the character is not set foot on it. How can I make it behave like a seesaw thanks!
This is the Original position of the gameObject where the other end of the wood is inclined down.
This should what must happen when my character puts weigh on the opposite end help please.
I recommend in this case to use the "Motor" behaviour of joints:
https://docs.unity3d.com/ScriptReference/HingeJoint-motor.html
So if you set to the "motor" of the joint a little bit of force, it will come back to the start position.
One more recomendation, depending of the force, the joint can "bounce" so I suggest to make some script that sets the motor force only when has interacted with the player, or stop doing the force if it's already on the start position, it's up to you! :D
I have a cube with Rigidbody attached to it would slide slightly whenever I pressed the play button. If I leave it for awhile, it would slide to other side of the screen.
Anyone know how to solve this problem without using the "freeze position"? I don't know what I messed up in my project...
This is what the object looks like:
Note: I need to use the gravity.
Thank you!
Heh! The solution here is:
You had a rigidbody on the floor :)
You don't do that :) Never.
If the "floor" surface is flat, then,
it will not slide.
You've got something strange going on, such as
"floor" is NOT flat
a feature like "Wind" turned on
perhaps other objects invisible in the scene you have forgotten about are nudging it
PhysX does not have a "mind of it's own". There is some simple reason it is moving.
Let's say the "floor" is indeed on an angle, so it SHOULD move, but you WANT it to NOT move.
What you obviously do:
Just as in the real world, put something there to stop it moving.
A small invisible wall will do the trick. That's PhysX!
Usually rigidbody sliding happens when a lower rigidbody has lower mass than a higher rigidbody (forcing down the lower rigidbody). Typical problem with player having say mass 80 jumping on a cube with mass 1. In this case the collision is so violent that the cube will probably fly out (not only slide).
The situation is very similar to the real world. Try to stay on a box of milk if you have 120 kg (ok, ok, 80 :) ).
When you try to eliminate this behavior, you need either increase the mass of the lower object or decrease the mass of the higher one or set the lower rigidbody to kinematic.
The solutions above is not proper way of solving the problem. Unity has more features of physics than mass. If you get sliding on movement or because of other objects you should add proper drag value on your rigidbody. For example , lets say you have blocks spawning over top of other blocks and this creates horizontal sliding. In my case I add drag of 1 to the objects which has mass of ~ 1 kg. It depends on the scene and you should try different values on your case. Do not use bigger values and angular drag if it is not important.
I'm using the first person controller for my characters movement. On a left arrow keypress, I'd like the character to instantly rotate 90 degrees and keep moving forward. Currently, when I hit the arrow key, the character makes the sharp 90 degree turn, but the forward momentum the character previously had takes a second to wear off so the character ends up sliding in the direction he was previously moving a short bit.
The closest example I can think of to visually explain what I'm trying to do is how the character turns sharp in Temple Run. How my game is currently working, if I had the character on a ledge make a sharp left turn, he'd likely keep the original momentum and slide off the edge right after he turns.
Since my character is running on the x/z axis, I'm wondering if there would just be some way to maybe swap the directional velocity/momentum? The speed the character had on the x axis would instantly be switched to the z when it turns and the other would be set to zero. I'm obviously open to any solution that accomplishes what I'm looking for.
I dug into the CharacterMotor class in the first person controller, but have yet to find what part I can tweak to accomplish this.
I'd greatly appreciate any help.
Thank you.
You can try to stop the velocity of the Rigidbody before turning.
this.rigidbody.velocity = Vector3.zero;
this.rigidbody.angularVelocity = Vector3.zero;
If you want the object to continue like it did, you can try playing around with it by saving the current velocity in a variable, setting it to 0, rotate it and then putting back the old velocity (still forward).
If it works with global vectors (so from the point of view of the world, not the object), then you can try negativing the velocity, actually causing it to go 'backwards'. I can't test it for now but either way I think you need to set the velocity to zero first before turning the character.
I need to animate an object along a path. I am doing so by creating an animation like:.
This works great but since my terrain is not flat it will be nice if I don't have to deal with the y component. In other words I want to move an object along the x-axis and z-axis and if there is a small slope increase then increase the object y position. same thing if there is a downward slope. Or maybe I have to create a script where it checks to see if the object is colliding with the terrain. if not then decrease its y position. I don't know if this will work meanwhile animating an object though.
I think the easiest way I can think of to solve this is for you to make the object a rigid body and use collision (probably a mesh or capsule collider if its a player character) to get it to sit on the floor.