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

Setting rotation to local space

Discussion in 'Scripting' started by Hootlook, Feb 24, 2019.

  1. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    Hi guys maybe some of you have the solution,

    I am making a player controller that can be controlled on every angles, so i thought of making my player's Y axis rotate by setting its angle relative to the joystick position like so
    Code (CSharp):
    1. float targetRotation = Mathf.Atan2(input.x, input.y) * Mathf.Rad2Deg;
    Problem is that i can't figure a way to set the local Y axis of my player, there is of course
    Code (CSharp):
    1. transform.Rotate(transform.up * targetRotation, Space.Self);
    but it rotates the player and i want to SET the rotation, it could be a setup problem but whatever how i try to change it i always fall back on the fact that i need to rotate something with the player's rotation

    I saw this online but i dont get how i am supposed to use this
    Code (CSharp):
    1. parent.rotation = parent.localRotation * Quaternion.identity;
    2.  
    3. child.rotation = transform.localRotation * parent.rotation;
     
    Last edited: Feb 24, 2019
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    You were close, just a slight change to the maths

    Code (CSharp):
    1.     private Vector3 _input;
    2.  
    3.     private void Update()
    4.     {
    5.         _input.x = Input.GetAxisRaw("Horizontal");
    6.         _input.y = Input.GetAxisRaw("Vertical");
    7.  
    8.         var angle = Mathf.Atan2(_input.y, _input.x) * Mathf.Rad2Deg;
    9.  
    10.         transform.rotation = Quaternion.AngleAxis(angle, transform.up);
    11.  
    12.     }
     
  3. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    Doesn't work, its weird actually because it works as long as the other axis are 0


    But if i change the values i get this



    the rotation madness depend on the joystick position here i have made a full rotation and it stabilize itself when reaching the forward of the stick and snap the X and Z axis and it get back to work like in the previous video


    this is the code:
    Code (CSharp):
    1.     void Update () {
    2.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    3.         Vector2 inputDir = input.normalized;
    4.  
    5.         if (inputDir != Vector2.zero)
    6.         {
    7.             float targetRotation = Mathf.Atan2(input.x, input.y) * Mathf.Rad2Deg; //+ cameraT.eulerAngles.y;
    8.  
    9.             transform.rotation = Quaternion.AngleAxis(targetRotation, transform.up);
    10.         }
    11.     }
     
    Last edited: Feb 24, 2019
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It kind of looks like you were adding the transform rotation in the second clip.

    What is it you are trying to do. Rotate to a position on all axis depending on a stick position or move around an axis when a stick is moved?
     
  5. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    I just want to set the rotation of the Y axis of my player localy with the joystick, exactly like a traform.rotate() like this:
    Code (CSharp):
    1.     void Update () {
    2.         Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
    3.  
    4.         if (inputDir != Vector2.zero)
    5.         {
    6.             float targetRotation = Mathf.Atan2(input.x, input.y) * Mathf.Rad2Deg; //+ cameraT.eulerAngles.y;
    7.             transform.Rotate(Vector3.up * targetRotation, Space.Self)
    8.         }
    9.     }
    I want to set the Y rotation exatly like that but i want to SET it while transform.Rotate() rotates the local axis which doesn't work with the joystick
     
    Last edited: Feb 24, 2019
  6. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    transform.Rotate() does exacly what i want, it rotates the Y axis localy meaning that i can rotate the other axis to what ever i want and it will still rotate only the Y axis no matter the angle :


    The problem with that is that if i tilt the joystick it will continue to rotate indefinatly rather than setting the Y rotation to the angle of the joystick

    For example if i tilt the joystick to the upper right, the Y axis of the player should rotate to something like 45°
     
  7. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Ok. So is it just the Y axis you wish to control with the joypad?
     
  8. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    Correct and the other axis shouldn't be affected kind of with the transform.Rotate()

    I know that its not that simple under the hood but i feel like i tried everything
     
  9. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    It depends on how you need to rest of it to work but an easy to achieve that would be to put the script below on an empty gameobject which you rotate to the direction you want and then have the cube/player model as a child object. You then just rotate the child on the local y.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class JoystickRotate : MonoBehaviour
    4. {
    5.     private Vector2 _input;
    6.     public Transform playerModel;
    7.  
    8.     private void Update()
    9.     {
    10.         _input.x = Input.GetAxisRaw("Horizontal");
    11.         _input.y = Input.GetAxisRaw("Vertical");
    12.  
    13.         var angle = Mathf.Atan2(_input.y, _input.x) * Mathf.Rad2Deg;
    14.  
    15.         if (Mathf.Abs(_input.y) > 0 && Mathf.Abs(_input.x) > 0)
    16.         {
    17.             playerModel.localRotation = Quaternion.AngleAxis(90 - angle, Vector3.up);
    18.         }
    19.     }
    20. }
     
  10. Hootlook

    Hootlook

    Joined:
    Oct 27, 2018
    Posts:
    15
    That was almost genius :D but the problem is that if i rotate the child and then rotate the parent to the child it wil make a loop and make the player spin...

    i can't believe that unity doesn't have a transform.SetRotation() but has a transform.Rotate