Search Unity

mathf.clamp and temp variables stuck!!!!

Discussion in 'Scripting' started by andez, Mar 26, 2014.

  1. andez

    andez

    Joined:
    Jan 1, 2014
    Posts:
    3
    hi unity peeps ,

    I have been trying to figure out for a while now how to apply .clamp to a rotation in c# and was really struggling . I got as far as finding out about needing to store a temp vector3 then using that , but have tried every way I can think of and using lots of docs and tutorials trying to peace things together to try and understand , it was hitting me back with errors and that's okay , gives me something to work with.
    but now nothing. I've got this code applied it and no errors but nothing happens ? now its got me confused what it is supposed to do is rotate an object on the y axis and then clamp it so far if someone can have a look and see if they can point out what I am missing would be great , even a point in the right direction would help

    Here is my code . and thanks again too such an amazing commUNITY...

    using UnityEngine;
    using System.Collections;


    public class turn : MonoBehaviour
    {
    public GameObject head;
    private float right = 1.0f, left = -1.0f ;
    private Vector3 rot;
    public Transform headrot ;
    // Use this for initialization
    void Start ()
    {
    rot = headrot.eulerAngles ;
    left = rot.y;
    right = rot.y;


    }


    // Update is called once per frame
    void Update ()
    {
    if (Input.GetKey(KeyCode.A))
    {
    left = left * Time.smoothDeltaTime;


    print ("rotate left active");


    left = Mathf.Clamp(left,-180,180);


    headrot.eulerAngles = new Vector3 (0.0f,left,0.0f);
    }

    if (Input.GetKey(KeyCode.D))
    {
    right = right * Time.smoothDeltaTime;

    print ("rotate right active");

    right = Mathf.Clamp(right,-180,180);

    headrot.eulerAngles = new Vector3 (0.0f,right,0.0f);

    }

    }




    }