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.

Feature Request C# 8 direction (with one bool for each direction) based in Y axis rotation or Vector3

Discussion in 'Animation' started by ralke23, Oct 22, 2022.

  1. ralke23

    ralke23

    Joined:
    Aug 8, 2022
    Posts:
    1
    Hi there guys! This is my first post :D. I'm struggling with Unity, doing a player movement script.
    What I need is to add a bool for every time that the player turns.

    I have this
    Code (csharp):
    1.     void Update()
    2.     {
    3.         // Get player data (input)
    4.         float h = Input.GetAxisRaw("Horizontal");
    5.         float v = Input.GetAxisRaw("Vertical");
    6.  
    7.         // Save in xyz
    8.         Vector3 direction = new Vector3(h,0,v);
    9.         // Apply the movement
    10.         // Use transform.Position(); if character teleports
    11.         transform.Translate(direction * playerSpeed * Time.deltaTime, Space.World);
    12.  
    13.         // If player moves, turn direction
    14.         if (direction != Vector3.zero)
    15.         {
    16.             // Generate advanced rotation, rotate where you look
    17.             Quaternion rota = Quaternion.LookRotation(direction, Vector3.up);
    18.  
    19.             // Apply rotations to character
    20.             transform.rotation = Quaternion.RotateTowards(transform.rotation, rota, playerTurn);
    21.  
    22.             // Text variable (string) that calls Animator's boolean
    23.             playerAnimator.SetBool("Caminar", true);
    24.         }
    And I would like to change it this way:

    Code (csharp):
    1. if h = -1 ///(this mean is looking to left),
    2. { playerAnimator.SetBool("Walk_Left", true; }
    3. if h = 1 // this mean is looking right
    4. { playerAnimator.SetBool("Walk_Right", true; }
    And this for the 8 directions (adding north-east, south-west, etc.) How can I merge this into the code above? I know the player can rotate withouth having more animations than 1 .fbx, but I really need to load the 8 direction animations separately.

    Also I know that h, and v can't access to 45º rotations, for this I though on

    https://answers.unity.com/questions/254798/check-an-objects-rotation.html

    This way I could use:

    Code (csharp):
    1.  if (myObject.transform.rotation.eulerAngles.y == 45)
    2.  {
    3.     playerAnimator.SetBool("Walk_NorthWest", true);
    4.  }
    And load the player gameObject as public variable, but I have no idea how could I merge this with the code above to work properly

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Player : MonoBehaviour
    5. {
    6.     // Local variables
    7.     public float playerSpeed;
    8.     public float playerTurn;
    9.     public GameObject ballTrigger;
    10.     // Modifify Animator parameters
    11.     public Animator playerAnimator;
    12.     private Vector3 lastMove;
    13.  
    14.     void Start()
    15.     {
    16.         // Access to Animator component on start
    17.         playerAnimator = GetComponent<Animator>();
    18.         ballTrigger.gameObject.SetActive(false);
    19.         playerSpeed = 1;
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         // Get player data (input)
    25.         float h = Input.GetAxisRaw("Horizontal");
    26.         float v = Input.GetAxisRaw("Vertical");
    27.         // Save in xyz
    28.         Vector3 direction = new Vector3(h,0,v);
    29.         // Apply the movement
    30.         // Use transform.Position(); if character teleports
    31.        transform.Translate(direction * playerSpeed * Time.deltaTime, Space.World);
    32.         // If player moves, turn direction
    33.         if (direction != Vector3.zero)
    34.         {
    35.             // Generate advanced rotation, rotate where you look
    36.             Quaternion rota = Quaternion.LookRotation(direction, Vector3.up);
    37.  
    38.             // Apply rotations to character
    39.             transform.rotation = Quaternion.RotateTowards(transform.rotation, rota, playerTurn);
    40.  
    41.             // Text variable (string) that calls Animator's boolean
    42.             playerAnimator.SetBool("Caminar", true);
    43.         }
    44.         else if (Input.GetKey(KeyCode.E))
    45.         {
    46.             Debug.Log("Using E");
    47.             // Text variable (string) that calls Animator's boolean
    48.  
    49.             // Apply rotations to character
    50.             playerAnimator.SetBool("Shield", true);
    51.             playerAnimator.SetBool("Caminar", false);
    52.             ballTrigger.gameObject.SetActive(true);
    53.             playerSpeed = 0;
    54.         }
    55.         else if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
    56.         {
    57.             transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * playerSpeed * Time.deltaTime, 0f, 0f));
    58.             lastMove = new Vector3(Input.GetAxisRaw("Horizontal"), 0f);
    59.         }
    60.         else
    61.         {
    62.             playerAnimator.SetBool("Caminar", false);
    63.             playerAnimator.SetBool("Shield", false);
    64.             ballTrigger.gameObject.SetActive(false);
    65.             playerSpeed = 3;
    66.         }
    67.     }
    68. }
    69.  

    The outcome I need to achieve is to import my character animations on .fbx with baked isometry for each direction...

    image.png

    image (2).png

    But it breaks when I rotate


    Thanks in advance!
    Regards