I’m trying to rotate player back to it’s initial position after ship get’s hit from the side. I don’t really know whats wrong, but this function twitches my player back and forth instead of smoothly rotating back to it’s initial position.
This is what it looks like:
http://giphy.com/gifs/xT8qAZcxlCPlFlRBtu/fullscreen
My code:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
// Movement
public float thrustVertical = 2.0f; // Optimal 2
public float thrustHorizontalLeft = -1.0f; // Optimal -1
public float thrustHorizontalRight = 1.0f; //Optimal 1
Quaternion desiredRotation;
// Physics
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
desiredRotation = transform.rotation;
}
// Update is called once per frame.
void Update () {
transform.rotation = Quaternion.Lerp(transform.rotation, desiredRotation, Time.deltaTime * 10f);
}
// Rigidbody manipulation
void FixedUpdate(){
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch (0);
float middle = Screen.width / 2;
if (Input.touchCount == 2) {
moveUp ();
} else if (touch.position.x > middle) {
moveRight ();
} else if (touch.position.x < middle) {
moveLeft ();
}
}
}
// Move character left
private void moveLeft(){
rb2d.AddRelativeForce (new Vector2 (thrustHorizontalLeft, 0));
}
// Move character right
private void moveRight(){
rb2d.AddRelativeForce (new Vector2 (thrustHorizontalRight, 0));
}
// Move character up
private void moveUp(){
rb2d.AddRelativeForce (new Vector2 (0, thrustVertical));
}
}