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

Resolved Can't figure out how to move an object along an x any y through an angle and distance

Discussion in 'Scripting' started by KnoblePersona, Oct 31, 2020.

  1. KnoblePersona

    KnoblePersona

    Joined:
    Oct 13, 2020
    Posts:
    15
    I can't figure out a problem I have. It goes like this:

    Let's say I have an object (2d for simplicity's sake) at (0, 0) and i want to move the object at an angle a distance then set an objects x and y to that. I thought it when something like: x/y = distance * sin/cos(angle). But this dose not seem to work for me. I've tried swapping sin and cos, but that doesn't work. All the player dose is spin rapidly with little to no control. How do I fix this problem? Is sin/cos even the right thing to do?

    Camera always looks at the player. Here is my script:

    Code (CSharp):
    1. public class PlayerMovementScr : MonoBehaviour
    2. {
    3.     PlayerInput input;
    4.  
    5.     Vector3 move;
    6.  
    7.     float moveAngle;
    8.     float moveAngleRelative; //Move angle RELATIVE to the camera
    9.  
    10.     public GameObject playerCamera;
    11.  
    12.     public float moveSpeed = 1.0f;
    13.  
    14.     void Awake()
    15.     {
    16.         input = new PlayerInput();
    17.  
    18.         input.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
    19.         input.Gameplay.Move.canceled += ctx => move = Vector2.zero;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.  
    25.         moveAngle = Mathf.Atan2(move.x, move.y) * Mathf.Rad2Deg;
    26.         moveAngleRelative = moveAngle + playerCamera.transform.eulerAngles.y;
    27.  
    28.         transform.eulerAngles = new Vector3(0, moveAngleRelative, 0);
    29.  
    30.         Vector3 m = new Vector3(Mathf.Cos(moveAngleRelative), 0, Mathf.Sin(moveAngleRelative)) * moveSpeed * Time.deltaTime;
    31.         transform.Translate(m, Space.World);
    32.     }
    33.  
    34.     void OnEnable()
    35.     {
    36.         input.Gameplay.Enable();
    37.     }
    38.     void OnDisable()
    39.     {
    40.         input.Gameplay.Disable();
    41.     }
    42. }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    KnoblePersona likes this.
  3. KnoblePersona

    KnoblePersona

    Joined:
    Oct 13, 2020
    Posts:
    15
    So the thing I was missing was Mathf.Deg2Rad. I just put it in Mathf.Sin(moveAngleRelative * Mathf.Deg2Rad) like so and it's working as intended. Thank you so much. This was vary helpful
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Another handy shortcut for producing a vector based on a direction is to multiply a Vector3.forward by the rotation:

    Code (csharp):
    1. float angleDegrees = 123;
    2.  
    3. var CompassVectorNorth = Quaternion.Eu.er( 0, angleDegrees, 0) * Vector3.forward;
     
    Yoreki and KnoblePersona like this.
  5. KnoblePersona

    KnoblePersona

    Joined:
    Oct 13, 2020
    Posts:
    15
    Okay. That will be useful in the future. Thank you so much!
     
    Kurt-Dekker likes this.