I’ve been trying to get this to work for the past hour or so, but I simply have no idea how to something like this. Basically what I am trying to do, when a certain button is pressed an gameObject has to rotate over it’s Y axis to a 0 degree angle over a set time of (lets say) 0.5 seconds (I might change the time later on), but it must be able to do this from any angle that it currently might be on.
Here the script I want to implement it in (I took my past mistakes out so you guys don’t have to deal with that)
using UnityEngine;
using System.Collections;
public class flight : MonoBehaviour {
bool forward = false;
bool backward = false;
float AccelerationSpeed = 5000f;
float DecelerationSpeed = -5000f;
public GameObject thruster1;
public GameObject thruster2;
void Start () {
Screen.lockCursor = true;
}
void goForward() {
if (Input.GetKeyDown (KeyCode.LeftShift)) {
forward = true;
}
if (Input.GetKeyUp (KeyCode.LeftShift)) {
forward = false;
}
if (forward == true) {
rigidbody.AddForce(transform.up * AccelerationSpeed);
//insert the rotation here
}
}
void goBackward() {
if (Input.GetKeyDown (KeyCode.LeftControl)) {
backward = true;
}
if (Input.GetKeyUp (KeyCode.LeftControl)) {
backward = false;
}
if (backward == true) {
rigidbody.AddForce(transform.up * DecelerationSpeed);
//I'll be implenting another rotation here, but don't worry about that
}
}
void Update () {
goForward();
goBackward();
var pitch = -Input.GetAxis("Vertical")*60*Time.deltaTime;
var roll = Input.GetAxis("Horizontal")*60*Time.deltaTime;
var yaw = -Input.GetAxis("RollOver")*60*Time.deltaTime;
transform.Rotate (pitch, yaw, roll);
}
}
I hope you guys can help me figure this one out, because I am all out of ideas. Thanks already to all of you.
P.S. I do know there are more topics asking help with the same sort of thing, but the ways they solve it tend to mess up my code in some way or just don’t make any sense to me. I’m still kind of a newby if it comes to coding in languages such as C#