- Home /
2D C# Rotation Stopped Working Since Unity 5, Please Help?
Hello all, can anyone please tell me why my 2D rotation feature that I use on a sprite in my game has stopped working since I've updated to Unity 5?
Here is the code, I've removed all other elements that are used for movement up and down etc. I believe this is all that is needed to work out the problem.
using UnityEngine;
using System.Collections;
public class PlayerControl : MonoBehaviour {
float maxMove=2f; // speed of the movement of the object
float maxTurn=50; // max angle it turns up/down
float maxGrav=500;
float turnRate=10;
Rigidbody2D rb2d;
void Start() {
rb2d = GetComponent<Rigidbody2D> ();
}
void Update() {
float delta = 0;
float deltaGrav = 0f;
if (Input.GetKey (KeyCode.UpArrow)) {
delta++;
} else if (Input.GetKey (KeyCode.DownArrow)) {
delta--;
} else {
deltaGrav--;
}
rb2d.rotation = Mathf.Lerp (rb2d.rotation, maxGrav * deltaGrav, turnRate * Time.deltaTime);
rb2d.MovePosition (rb2d.position + Vector2.up * delta * maxMove * Time.deltaTime);
rb2d.rotation = Mathf.Lerp (rb2d.rotation, maxTurn * delta, turnRate * Time.deltaTime);
}
}
Thank you all in advanced, I really appreciate the help!
Should grav/maxgrav should be controlling rb2d's rotation and not its position?
Possibly? I thought it was with the other two?
And it was working fine before I upgraded so I'm not sure..
Anyone have the solution? Even if it's a completely different one, I just need 2D rotations to work in this manner.
has stopped working
I just need 2D rotations to work
What do you mean ? What is happening and what do you want to happen ? Is the object not rotating at all ?
You have 2 separate lines adjusting rotation in each Update() call with all kinds of different values in $$anonymous$$athf.Lerp. $$anonymous$$aybe those 2 values are roughly complementary and neutralize each other's rotation ?
And your $$anonymous$$ovePosition adds the current position of the object on top of its old position every Update() which seems odd... It's really hard to figure out what you want to achieve.
Well I just need to object to rotate on the z axis, it's a 2D sprite, so imagine a head without a body that is looking up or down depending on which arrow key is being pressed.
Yeah, one of those lines is to accommodate for gravity (maxGrav) and the other is for rotation on key press.