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

Get Local Axis

Discussion in 'Scripting' started by gtorres26, May 13, 2020.

  1. gtorres26

    gtorres26

    Joined:
    Apr 29, 2020
    Posts:
    10
    Hello, this is my script for character movement:

    Code (CSharp):
    1.  
    2.             float horizontal = Input.GetAxis("Horizontal");
    3.             float vertical = Input.GetAxis("Vertical");
    4.  
    5.             m_Movement.Set(horizontal, 0f, vertical);
    6.             m_Movement.Normalize();
    7.  
    8.  
    9.             Vector3 desiredForward = Vector3.RotateTowards(transform.forward, m_Movement, turnSpeed * Time.deltaTime, 0f);
    10.             m_Rotation = Quaternion.LookRotation(desiredForward);
    It works nice and my character can move in any direction smoothly. The problem is that I´m making a game in which the camera angle changes dramatically depending on the areas the character access. And everytime this happens my character keeps moving using the same global axis, so the controls get funny. I have been searching for several days how to access the GameObject local axis instead but I can´t find any answer I can apply to my script.
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Ummm... How did you manage to miss looking into Transform?
     
  3. gtorres26

    gtorres26

    Joined:
    Apr 29, 2020
    Posts:
    10
    I know how to navigate Unity´s documentation but I don´t see how this solves my problem. I started to use Unity one week ago with no previous experience in coding but somehow I´ve managed to create my own scripts for jumping, double jumping, dashing, using elevators and ladders, atacking and been attacked while using custom life bars and even implemented enemies AI so their routes respond to my characters actions.

    So I´m a noob but I learn fast. However, I can´t figure out how to use local axis for my character orientation instead of the global default ones. I appreciate the answer but a little explanation woulb be even better.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Your m_Movement vector is in world coordinates. Try something like this:
    Code (CSharp):
    1. Vector3 localMovement = transform.InverseTransformDirection(m_Movement);
    2. Vector3 desiredForward = Vector3.RotateTowards(transform.forward, localMovement, turnSpeed * Time.deltaTime, 0f);
     
  5. gtorres26

    gtorres26

    Joined:
    Apr 29, 2020
    Posts:
    10
    Thank you PraetorBlue but it doesn´t seem to affect my character´s movement in any way, even when I change it´s rotation manually on runtime, it still moves towards the same blue global axis when pressing my forward key.
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    It's hard to know more without seeing more of your code. I don't know what you're doing with the m_rotation variable for example.
     
  7. gtorres26

    gtorres26

    Joined:
    Apr 29, 2020
    Posts:
    10
    Does this help?

    Code (CSharp):
    1.  private void OnAnimatorMove()
    2.     {
    3.  
    4.         m_Rigidbody.MovePosition(m_Rigidbody.position + m_Movement * 0.05f);
    5.         m_Rigidbody.MoveRotation(m_Rotation);
    6.  
    7.     }
     
  8. gtorres26

    gtorres26

    Joined:
    Apr 29, 2020
    Posts:
    10
    I solved my issue by adding this third line to my OnAnimatorMove() code:

    Code (CSharp):
    1.  m_Rigidbody.MovePosition(currentPosition + m_Movement * 0.05f);
    2.         m_Rigidbody.MoveRotation(m_Rotation);
    3.  
    4.         transform.position += (m_Movement.x * compass.right + m_Movement.z * compass.forward) * Time.deltaTime * 5;
    It takes the references for forward and right from a public object I named compass. So now I can change the compass rotation from script everytime my character enters an area where the camera angle changes. I could have actually used my camera as a compass but that would probably cause a problem since I don't want my character's facing rotation to change as soon as the camera angle changes. If I did that and the camera changes to the opposite angle my character would face the opposite direction I want since the player would be still pressing the key for the direction of the previous camera angle. So what I do instead is changing the compass rotation to the one I need once both my character enters an area with a new camera angle and the key that is currently pressed for movement is not longer pressed or a new one is pressed instead. I don't know if I explaining myself clearly but I consider important to explain this in case anyone else finds the same issue. So if my explanation is not clear enough and someone needs this just add a new comment to this post.

    Thank you for the answers.
     
  9. ccbelan

    ccbelan

    Joined:
    May 7, 2020
    Posts:
    8
    I've been trying to create a script that would rotate my player and somehow i cant rotate it. . . im using the new input system and been watching a ton of youtube vids already still cant make my player rotate

    here is my script. .


    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.IO;
    5. using UnityEngine;
    6. using UnityEngine.InputSystem;
    7.  
    8. public class PlayerCamera : MonoBehaviour
    9. {
    10.     private CcInputActions _inputActions;
    11.  
    12.     private CharacterController myCC;
    13.  
    14.     [SerializeField] private float horizontalRotationSpeed;
    15.  
    16.     [SerializeField] private float Strafingfield;
    17.  
    18.     private Vector2 moveAxis;
    19.  
    20.     private void OnEnable()
    21.     {
    22.         _inputActions.Player.MouseX.performed += RotationAxisY;
    23.         _inputActions.Player.MouseX.Enable();
    24.  
    25.     }
    26.  
    27.     private void OnDisable()
    28.     {
    29.         _inputActions.Player.MouseX.performed -= RotationAxisY;
    30.         _inputActions.Player.MouseX.Disable();
    31.  
    32.     }
    33.  
    34.     private void Start()
    35.     {
    36.         myCC = GetComponent<CharacterController>();
    37.     }
    38.  
    39.     private void Awake()
    40.     {
    41.         _inputActions = new CcInputActions();
    42.     }
    43.  
    44.     private void RotationAxisY(InputAction.CallbackContext context)
    45.     {
    46.         //reads in the local variable
    47.         //local variable "float" called "yRotate"
    48.         float yRotate = context.ReadValue<float>();
    49.         transform.Rotate(new Vector3(0, yRotate, 0) * horizontalRotationSpeed * Time.deltaTime);
    50.     }
    51.  
    52.  
    53. }
    54.  
     
  10. gtorres26

    gtorres26

    Joined:
    Apr 29, 2020
    Posts:
    10
    Hello ccbelan. Sorry, I thought I would receive an email if a new post was added to this thread but I didn't. Like I said, I'm far from beeing an expert in Unity but I can try to help. Are you still struggling with your issue or you found a solution already?
     
  11. ccbelan

    ccbelan

    Joined:
    May 7, 2020
    Posts:
    8
    yes im still struggling with it. please help