Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Input Axis Rotation in Unity

Discussion in 'Scripting' started by tommehnet, Aug 1, 2014.

  1. tommehnet

    tommehnet

    Joined:
    Aug 19, 2013
    Posts:
    2
    I am trying to rotate the input axis by an angle offset, depending on where the character is looking.

    Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical"));
    Quaternion targetRotation = Quaternion.LookRotation (input, Vector3.up);
    transform.Find ("Aim").rotation = targetRotation;​

    float angle = Vector3.Angle(transform.forward, transform.Find("Aim").forward);
    Vector3 cross =Vector3.Cross(transform.forward, transform.Find("Aim").forward);
    if(cross.y >0)
    angle =-angle;​

    input =Quaternion.AngleAxis(angle,Vector3.up)*Vector3.forward;​


    The Aim game object is set by the input axis. The character should still move given an X, Y axis whilst still looking at a target, using a RTS-style camera. As I am using the animator to move the character, I need to figure out how to set the blend tree coordinates to move relative to where the character is facing.

    I am getting weird blending results due to the axis not being constrained to a square. A 45 degree turn results in (0.7, 0.7) instead of (1, 1). Is there a way I can stretch the Vector to snap to a 1,1 square, or a generally better way to achieve what I am working towards?



    UPDATE: To try my project so far https://00f075b097c7f17b07b129853fa.../host/0B6rDPzFC8Iitb1Q3THdtZlZpb28/Unity.html
     
    Last edited: Aug 1, 2014
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  3. tommehnet

    tommehnet

    Joined:
    Aug 19, 2013
    Posts:
    2
    Hi Fluzing, I manage to generate a rotated vector by doing Quaternion.AngleAxis(angle,Vector3.up)*Vector3.forward but if I want the rotation to be 45 degrees, the axis becomes something like 0.7, 0.7 instead of 1, 1