The question is answered
I am having issues with rotating the player. I know this is simple but kindly have a look. You will be flabbergasted.
I am following Space Shooter tutorial and the player doesn't rotate but when I uncomment 'Debug.Log (rb.rotation);' the player starts rotating. I had implemented this game earlier on older version of unity and it worked. It seems that this issue is related to new version of unity. Also the player stays in rotated orientation when I hard code the angle
private Rigidbody rb;
public float speed;
public float tilt;
public Boundary boundary;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate(){
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
// move player
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
// restrict the player from going out of playing area
transform.position = new Vector3 (
Mathf.Clamp (transform.position.x, boundary.xMin, boundary.xMax),
0.0f,
Mathf.Clamp (transform.position.z, boundary.zMin, boundary.zMax)
);
// rotate the player based on velocity along X axis
//Debug.Log (rb.rotation);
rb.rotation = Quaternion.Euler (0.0f, 0.0f, rb.velocity.x * -tilt);
}
Have you followed the "Upgrade Guide to Unity 5", which can be downloaded from https://unity3d.com/learn/tutorials/s/space-shooter-tutorial ?
Thankyou so much. I appreciate your help. I had taken all annotations in the video into consideration. I just went through the 'upgrade guide' and the code is upto the mark. I am not using the code of the older version of unity rather I created the new project
I added following line before rb.rotation = Quaternion.Euler (); Quaternion rotation = rb.rotation; And guess what the player rotated
The above line makes no sense but what I figured out is that whenever I write a line that references 'rb.rotation' before rotating the object, the player rotates.
Is it because the 'rb.velocity.x' is not updated when the execution reaches the last line?
Answer by meat5000 · Jan 06, 2018 at 02:48 PM
You are performing tasks in FixedUpdate which should be in Update... that is everything that is not directly Physics.
Debug.Log is intensive and is likely slowing things down enough for your FixedUpdate control method to actually be detected. I'm guessing, there's no other reason that I can think of that Debug.Log would affect things in this way.
This line from the docs may be key :
"If you change the rotation of a Rigidbody using Rigidbody.rotation, the transform will be updated after the next physics simulation step." So it could be just a timing issue.
@meat5000 I moved transform.position in Update() method and it worked 100 rewards for you Thanks, I really appreciate your help. But i couldn't find that line in docs. Doesn't matter I understood your explanation very well
$$anonymous$$ost welcome. I added a link to the page in the answer for you. Seems to contain some other useful information.
Follow this Question
Related Questions
Keep Rigidbody upright using torque. 1 Answer
Player rotates spastically on all axes when camera is turned 0 Answers
[VR] Rigidbody.AddForce() following controllers rotation 0 Answers
fromtorotation inacurate Help fixing? 0 Answers
How can I get a rigid body object to face in the direction my joystick is pointing smoothly? 0 Answers