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

transform.InverseTransformDirection in Tiny?

Discussion in 'Project Tiny' started by cavehunter, Sep 3, 2020.

  1. cavehunter

    cavehunter

    Joined:
    Jun 2, 2020
    Posts:
    7
    How do you convert keyboard Input to a player's local direction for movement? For example, moveDirection.z needs to line up with a player's local forward direction. I read you can use LocaltoWorld but I need help on how to use it. There aren't much example I can find online. I found this thread, but I need help understanding it to see if it's relevant or not.
    https://forum.unity.com/threads/transform-inversetransformdirection-for-ecs.652543/

    Below is an example of what I want in OOP.
    Code (CSharp):
    1.     // convert the world relative moveInput vector into a local-relative
    2.             // turn amount and forward amount required to head in the desired
    3.             // direction.
    4.             if (moveDirection.magnitude > 1f) moveDirection.Normalize();
    5.  
    6.             moveDirection = transform.InverseTransformDirection(moveDirection);
    7.  
    8.             moveDirection = Vector3.ProjectOnPlane(moveDirection, m_GroundNormal);
    9.             m_TurnAmount = Mathf.Atan2(moveDirection.x, moveDirection.z);
    10.             m_ForwardAmount = moveDirection.z;
     
  2. AbdulAlgharbi

    AbdulAlgharbi

    Unity Technologies

    Joined:
    Jul 27, 2018
    Posts:
    319
  3. cavehunter

    cavehunter

    Joined:
    Jun 2, 2020
    Posts:
    7
    Great! I got the player moving with keyboards by using the Update function linked above. I started off my project by modifying TinyPhysics, which I thought was a closest template for my third person player project. But it turns out that TinyRacing's method works better for what I needed. Thanks:)
     
    AbdulAlgharbi likes this.