- Home /
Stop A player Turning at Specific Point.
Hey I created a player that when you click A and D it will move in the respective direction and also turn but I don't know how to get him to stop turning when he is facing in the correct direction. This is what I have so far: using UnityEngine; using System.Collections;
public class PlatformControls : MonoBehaviour
{
private Rigidbody rb;
public float speed;
public float turnSpeed = 0.5f;
public float jump;
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector2 movement = new Vector2(moveHorizontal, 0.0f);
transform.Rotate(0, moveHorizontal, 0);
var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
transform.Rotate(0, x, 0);
rb.velocity = (movement * speed);
if (Input.GetKey(KeyCode.W))
{
Vector2 jump = new Vector2(0.0f, moveVertical);
rb.velocity = (jump * speed);
}
}
}
Any Help is appreciated!
Comment
Answer by Cynikal · Nov 03, 2016 at 03:11 PM
You could use Mathf.Clamp(BeginningFloat, EndFloat)
You could: Vector3.Angle..
public Transform target;
// prints "close" if the z-axis of this transform looks
// almost towards the target
void Update ()
{
Vector3 targetDir = target.position - transform.position;
float angle = Vector3.Angle( targetDir, transform.forward );
if( angle < 5.0f )
print( "close" );
}
next time I'm on unity I'll give it a try, thanks for the suggestion.