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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Direction Vector to Euler/Quaternion

Discussion in 'Scripting' started by George_93, Feb 26, 2019.

  1. George_93

    George_93

    Joined:
    Jan 28, 2019
    Posts:
    11
    I'm making an isometric game within Unity for a university project.

    I am trying to control the players rotation by the position of the mouse cursor. I have this 'somewhat' working at the moment, but the rotation is very limited. The character moves with the mouse, but only if the mouse is moving in a horizontal direction, rather than following around in a circle.
    My lecturer has stated that I need to convert the Direction to a Euler Angle, or Quaternion to achieve this result correctly.

    This is the current PlayerMovement Script;
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour {
    6.  
    7.     public float speed = 2.0f;
    8.     public Vector3 Velocity;
    9.  
    10.     //Player Rotation
    11.     //Convert an DIRECTION into a EULER ANGLE
    12.     public float horizontalSpeed = 10.0f;
    13.     public float verticalSpeed = 0.0f;
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.        
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.         GameObject PlayerCharacter = GameObject.Find("PlayerCharacter");
    23.         GameObject Enemy = GameObject.Find("Enemy");
    24.  
    25.         //Documentation for getting DIRECTION and DISTANCE from One Object to Another
    26.         //docs.unity3d.com/Manual/DirectionDistanceFromOneObjectToAnother.html
    27.  
    28.         if (Input.GetKey(KeyCode.A))
    29.         {
    30.             PlayerCharacter.transform.position += new Vector3(0.0f, 0.0f, 0.05f);
    31.             //Velocity += Vector3.left;
    32.         }
    33.  
    34.         if (Input.GetKey(KeyCode.D))
    35.         {
    36.             PlayerCharacter.transform.position += new Vector3(0.0f, 0.0f, -0.05f);
    37.            // Velocity += Vector3.right;
    38.         }
    39.  
    40.         if (Input.GetKey(KeyCode.W))
    41.         {
    42.             PlayerCharacter.transform.position += new Vector3(0.05f, 0.0f, 0.0f);
    43.             //Velocity += Vector3.up;
    44.         }
    45.  
    46.         if (Input.GetKey(KeyCode.S))
    47.         {
    48.             PlayerCharacter.transform.position += new Vector3(-0.05f, 0.0f, 0.0f);
    49.             //Velocity += Vector3.down;
    50.         }
    51.  
    52.         float h = horizontalSpeed * Input.GetAxis("Mouse X");
    53.         float v = verticalSpeed * Input.GetAxis("Mouse Y");
    54.         transform.Rotate(v, h, 0);
    55.  
    56.         //Enemy.transform.position += Velocity * Time.deltaTime;
    57.         //Velocity /= 1.1f; //Slows enemy down
    58.     }
    59. }
    60.  
    Any pointers on how to solve the problem?
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Have a look at the Quaternion class in the docs. It lists a method that converts a direction to a quaternion, exactly what you want.

    Please post which method you think it is.

    (I would appreciate it if nobody posts code for this yet, as the OP is trying to learn how to do it properly)
     
    Danahi and lordofduct like this.
  3. George_93

    George_93

    Joined:
    Jan 28, 2019
    Posts:
    11
    Brilliant, thank you! I'll take a look now.

    Edit: I'm judging by the Methods Listed, the correct one I'm after is 'Quaternion.LookRotation'.
     
    ffxiangyu and hippocoder like this.
  4. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yep, that's one of them. You can also convert a Quaternion (note: all rotations are by default Quaternions in unity) to euler angles as well.

    And you can rotate a vector by any rotation by multiplying the rotation by the vector:
    myvec = Quaternion.Euler(0, 90, 0)*myvec; //rotate vector by yaw by 90 degrees!

    Or myvec = rot * myvec;
     
    Danahi and tchris like this.
  5. tkramer811

    tkramer811

    Joined:
    Dec 8, 2019
    Posts:
    2
    If you want the rotation to a certain Vector, you can do this in 2D:

    Vector3 direction = target - transform.position;
    transform.rotation = Quaternion.FromToRotation(Vector3.right, direction);