Setting the axes?

Alright…

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?

Thanks in advance.

Ditto

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.

Can anyone help?

Are you using transform.forward to translate the ball? Or are you physically moving it on an axis? Can I see your code?

Ok, I have the ball rolling forward with the arrow keys.

if(Input.GetKey(KeyCode.RightArrow))(
{
rigidbody.velocity.x+=0.1;
}

and the same for the other arrows, just in different directions and axes.

With the a and d keys, I have the following

if(Input.GetKey("a")){
transform.Rotate(0,1,0);
}

and the same for d; however, it’s negative 1.

Basically, that’s all the code I have for the ball moving. I know that the Rotate function isn’t working the way that I want.

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(Input.GetKey("a")){
   	var deltaRotation = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime);
    rigidbody.rotation = deltaRotation;
   }

eulerAngleVelocity is set to Vector3(0,10,0)

Is there a way to get this to function the way I want? The ball still doesn’t turn at all with this 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:

emptyRotator.eulerAngles = (0,transform.eulerAngles.y,0);
rigidbody.velocity = emptyRotator.forward * moveSpeed;

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.

Hope this helps.

what about keeping a reference for the world rotation and use it whenever you move forward?