Question by
BadJukeBox · Jun 23, 2017 at 04:17 AM ·
c#movement
Rotating object towards (0,0,0) during idle (no input)
Hey! I was programming an object to rotate and tilt depending on the keyboard input (wasd) and I got it mostly working. The only problem is with the current code I tried to make it reset to the starting position (0,0,0) when there is no input. void Start () { mSpeed = 10f; current = new Vector3(0, 0, 0); center = new Vector3(0, 0, 0); }
// Update is called once per frame
void Update ()
{
moveAndRotate();
//transform.Translate(0, 0, 1f, Space.World);
}
void moveAndRotate()
{
transform.Translate(mSpeed * Input.GetAxis("Horizontal") * Time.deltaTime, mSpeed * Input.GetAxis("Vertical") * Time.deltaTime, 0, Space.World);
current = target;
yRotation -= Input.GetAxis("Horizontal") * 15;
if (yRotation > 45 || yRotation < -45)
if (yRotation > 45) yRotation = 45;
else yRotation = -45;
xRotation -= Input.GetAxis("Vertical") * 15;
if (xRotation > 45 || xRotation < -45)
if (xRotation > 45) xRotation = 45;
else xRotation = -45;
float step = mSpeed * Time.deltaTime;
target = new Vector3(xRotation, -yRotation, yRotation);
if(Input.GetAxis("Horizontal") == 0 && Input.GetAxis("Vertical") == 0)
{
transform.eulerAngles = Vector3.RotateTowards(current, center, step, 0.0f);
}
else transform.eulerAngles = Vector3.RotateTowards(current, target, step, 0.0f);
}
}
The aforementioned code is in the if(inputs == 0) statement and I've tried a couple solutions but I don't seem to understand how rotateTowards works in this case because the only thing I could get it to do was snap to 0 instantly. If this question has already been asked feel free to just drop the link but i didn't find it googling! Thank you.
Comment