- Home /
How to rotate 180 degrees on Trigger??
Hi first of all excuse my English in not American
Ok so heres the deal Im trying to make a open world driving game but I have encountered a problem if the car trips and lands on its back theres no way of getting it back to the right position.
I have googled my answer and got nothing but scripts that dont work
All I need is it to rotate 180 degrees on trigger.
Thanks for your time Anyways I include my code so it helps.
#pragma strict
var forwardSpeed : float = 3.0;
var turnspeed : float = 2.0;
function Update ()
{
// This is the forward speed
var forwardMoveAmount = Input.GetAxis("Vertical") *forwardSpeed;
//this rotation speed
var turnMoveAmount = Input.GetAxis("Horizontal") * turnspeed;
//rotation
transform.Rotate(0,turnMoveAmount,0);
//moving by applying force
rigidbody.AddRelativeForce(0,0,forwardMoveAmount);
}
Please let us know if your question has been answered, if your issue has been resolved.
Answer by clunk47 · Aug 07, 2013 at 11:20 PM
Just grab the original euler angles on start or awake, if the user hits the key you wish to assign for this action, have it rotate back to the original angle on z axis.
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
{
Vector3 reset;
Vector3 currentAngle;
void Start()
{
reset = transform.localEulerAngles;
currentAngle = transform.localEulerAngles;
}
void Update()
{
if(Input.GetKeyDown (KeyCode.E))
{
currentAngle.z = reset.z;
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, currentAngle.z);
}
}
}
Thanks for responding to my question.
I tested out what you said and it works but the thing is i need it to rotate on trigger of a button since its part of the game mechanics
Very simple. Just change the if statement from angles, to user Input. Say you want to use the "E" $$anonymous$$ey. I have edited my answer accordingly.
Your answer
