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

Question Advanced Math Problem

Discussion in 'Scripting' started by r31o, Dec 8, 2021.

  1. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Hi,
    Im currently making a 3d top down shooter.
    The game itself is good, but there s a problem with vectors:
    In my game you move pressing the WASD keys (W upward, D downward, A left and D right) and the chatacters looks torwards the mouse in order to shoot where you aim. The problem here is that I want to add a model to my character, I added it and it works fine. Later on I dowloaded some animations from mixamo, one for idle and eight for walking (One in each direction) i created a 2d blend tree to store the anmations, but, and here the problem:
    If my character is looking upwards, and you press the W key, it should play the animation walk forward (Since is walking in the same direction is looking)
    But if is looking downwards and you press the W key, it should play the move backwards animation.
    I need some sort of formula to solve this.
    (If you dont understand what I mean, look in Karnage.io how the walk animations are played)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    Transforms have a .forward property.

    You can observe the .y element of that property to decide look up / look down.

    Code (csharp):
    1. float yLook = myLookTransform.forward.y;
    2.  
    3. if (yLook > 0) Debug.Log( "I am looking upward");
    4.  
    5. if (yLook < 0) Debug.Log( "I am looking downward");
     
  3. ubbelito

    ubbelito

    Joined:
    Sep 24, 2018
    Posts:
    23
    It sounds like you want to inverse transform the walking direction. https://docs.unity3d.com/ScriptReference/Transform.InverseTransformDirection.html

    This answers the question "How would a vector be relative to the character facing.", if the character was the center of the world and everything revolved around the character.

    Code (CSharp):
    1. // Assume walking direction is fetched from Input. This is in world coordinates, so up is "up relative to the screen".
    2. Vector3 walkingDirection = ....;
    3.  
    4. // Transform this direction into local coordinates, that is, relative to the character
    5. Vector3 localWalkingDirection = transform.InverseTransformDirection(walkingDirection);
    6.  
    7. // Now you can relate walking direction to animation, if facing down, then W will show as backwards. (I don't know how animation works, so I can just help with the vector.)
    8. ApplyAnimation(localWalkingDirection);
    9.  
    10. // Finally, apply the direction to the character
    11. transform.position += walkingDirection * speed * Time.deltaTime;
    Code written from the top of my head. Sorry for any errors.
     
    alexeu and Bunny83 like this.
  4. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    Thanks for your reply.
    Even if my code is slightly different than yours, the transform.inverseTransformDirection worked. Thanks!
     
    ubbelito likes this.