I have a ball in the game and a camera following the ball. I want the user to be able to press a and d to turn the ball. I used transform.Rotate to turn the ball, but it just turns the graphics on the ball and doesn’t change the axes. I have the camera rotating around the ball when the user pushes a or d, so basically my problem is that when I rotate to the right 90 degrees, the camera faces to the right and when I push the up arrow, the ball moves “straight” which is really the new left… I tried many different functions in the documentation, but couldn’t find the right one. So, how can you set the axes of an object?
Are you saying the ball has one or more child objects (including one for the graphics) and the camera is tracking the parent object that doesn’t rotate?
The ball is the parent and it has two children: an empty gameobject and the camera. When I used transform.Rotate(), you could see the graphics on the ball rotate and the camera rotates around the ball; however, pushing forward on the arrow keys will still move the ball “forward”, which isn’t forward anymore.
Rotating the object with tranform.Rotate won’t activate the physics. Try using rigidbody.AddTorque to apply rotation. The axes are essentially the same as what you would use with transform.Rotate.
I’ll have to try that later, thanks! I didn’t think to look in the rigidbody documentation :roll: … I’ll let you know if it works, but based on the documentation of the code, it looks like it will do the trick.
Alright… That didn’t work either. I was looking around in rigidbody documentation and found that you can set the rotation, but I can’t seem to get it to function.
If you want the ball to always move forward based on its angle, you can use transform.forward to get its real forward vector. If you say rigidbody.velocity = transform.forward * moveSpeed;, that would move it forward every frame. you’d just need to set rigidbody.velocity = Vector3.zero; whenever you’re not pressing forward or backward.
However, if the ball is rotating around more than just the y-axis, you’ll have to get more creative. More often than not, the easiest technique is to have an additional empty that you set to the ball’s y rotation and nothing else. After that, you can use the empty object’s transform.forward to get the right movement vector. So:
You might want to break open the third person controller scripts inside the standard assets folder in Unity. I wouldn’t call that a finished product, but it’s a great example or even a starting point. It might give you a good idea of how to proceed.
Once you get it working, you might want to switch to using the axis properties of the input manager instead of getting keystrokes. It’s much faster in regards to performance.
Also, I’ve noticed that things become a lot easier if you encapsulate all of your meshes inside a GameObject, put all of the scripts on that GameObject, and just link your mesh via external variables for animation control. Do all of your transforms on the GameObject. At least, that’s what I’ve experienced.