• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C# rotations for player on Unity

ralke

(҂ ͠❛ ෴ ͡❛)ᕤ
Joined
Dec 17, 2011
Messages
1,517
Solutions
27
Reaction score
870
Location
Santiago - Chile
GitHub
ralke23
Twitch
ralke23
Hi there guys! I don't know where to ask for help with this. 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
C#:
    void Update()
    {
        // Get player data (input)
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        // Save in xyz
        Vector3 direction = new Vector3(h,0,v);
        // Apply the movement
        // Use transform.Position(); if character teleports
        transform.Translate(direction * playerSpeed * Time.deltaTime, Space.World);

        // If player moves, turn direction
        if (direction != Vector3.zero)
        {
            // Generate advanced rotation, rotate where you look
            Quaternion rota = Quaternion.LookRotation(direction, Vector3.up);

            // Apply rotations to character
            transform.rotation = Quaternion.RotateTowards(transform.rotation, rota, playerTurn);

            // Text variable (string) that calls Animator's boolean
            playerAnimator.SetBool("Caminar", true);
        }

And I would like to change it this way:

C#:
if h = -1 ///(this mean is looking to left),
{ playerAnimator.SetBool("Walk_Left", true; }
if h = 1 // this mean is looking right
{ 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

This way I could use:
C#:
 if (myObject.transform.rotation.eulerAngles.y == 45)
 {
    playerAnimator.SetBool("Walk_NorthWest", true);
 }
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

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // Local variables
    public float playerSpeed;
    public float playerTurn;
    public GameObject ballTrigger;
    // Modifify Animator parameters
    public Animator playerAnimator;
    private Vector3 lastMove;

    void Start()
    {
        // Access to Animator component on start
        playerAnimator = GetComponent<Animator>();
        ballTrigger.gameObject.SetActive(false);
        playerSpeed = 1;
    }

    void Update()
    {
        // Get player data (input)
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        // Save in xyz
        Vector3 direction = new Vector3(h,0,v);
        // Apply the movement
        // Use transform.Position(); if character teleports
        transform.Translate(direction * playerSpeed * Time.deltaTime, Space.World);

        // If player moves, turn direction
        if (direction != Vector3.zero)
        {
            // Generate advanced rotation, rotate where you look
            Quaternion rota = Quaternion.LookRotation(direction, Vector3.up);

            // Apply rotations to character
            transform.rotation = Quaternion.RotateTowards(transform.rotation, rota, playerTurn);

            // Text variable (string) that calls Animator's boolean
            playerAnimator.SetBool("Caminar", true);
        }
        else if (Input.GetKey(KeyCode.E))
        {
            Debug.Log("Using E");
            // Text variable (string) that calls Animator's boolean

            // Apply rotations to character
            playerAnimator.SetBool("Shield", true);
            playerAnimator.SetBool("Caminar", false);
            ballTrigger.gameObject.SetActive(true);
            playerSpeed = 0;
        }
        else if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
        {
            transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * playerSpeed * Time.deltaTime, 0f, 0f));
            lastMove = new Vector3(Input.GetAxisRaw("Horizontal"), 0f);
        }
        else
        {
            playerAnimator.SetBool("Caminar", false);
            playerAnimator.SetBool("Shield", false);
            ballTrigger.gameObject.SetActive(false);
            playerSpeed = 3;
        }
    }
}

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

1666408316993.png

1666408303000.png



But it breaks when I rotate
unknown.png


Thanks in advance!
Regards
 
Last edited:
Solved using blending trees


C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    // Local variables
    public float playerSpeed;
    public float playerTurn;
    public GameObject ballTrigger;
    // Modifify Animator parameters
    public Animator playerAnimator;
    private Vector3 lastMove;
    public GameObject playerDirection;
    public float idleBlendValue;
    //Rigidbody playerRigidbody;
    public GameObject shieldGeo;
    bool moving;
    float movementX;
    float movementY = 0;

    void Start()
    {
        // Access to Animator component on start
        playerAnimator = GetComponent<Animator>();
        ballTrigger.gameObject.SetActive(false);
        playerSpeed = 0;
        //playerRigidbody = GetComponent<Rigidbody>();
        //lastMove = new Vector3(0, 5, 0);

    }
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ball_sub3_true") & !shieldGeo.activeSelf)
        {
            //Bool animation death
            //playerAnimator.SetBool("Death", true);
        }
        else if (collision.gameObject.CompareTag("Ball_sub3_true") & shieldGeo.activeSelf)
        {
            //Bool animation death
            //fxAnimator.SetBool("FireBallBlock", true);
        }
    }
    void Update()
    {
        // Get player data (input)
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");



        //In update
            if (x != 0)
            {
                movementX = x;
                movementY = 0;
                moving = true;
            }
            else if (y != 0)
            {
                movementX = 0;
                movementY = y;
                moving = true;
            }
            if (moving)
            {
                if (x == 0 && y == 0)
                {
                    movementX = 0;
                    movementY = 0;
                    moving = false;
                }
                else if (x != 0 & y != 0)
            {
                moving = false;
            }
            }

            // Save in xyz
            Vector3 direction = new Vector3(movementX, 0, movementY);

        // Apply the movement
        // Use transform.Position(); if character teleports
        transform.Translate(direction * playerSpeed * Time.deltaTime, Space.World);

        // If player moves, turn direction
        if (direction != Vector3.zero)
        {
            // Generate advanced rotation, rotate where you look
            Quaternion rota = Quaternion.LookRotation(direction, Vector3.up);

            // Apply rotations to character
            transform.rotation = Quaternion.RotateTowards(transform.rotation, rota, playerTurn);

            // Text variable (string) that calls Animator's boolean
            playerAnimator.SetBool("Caminar", true);
            playerSpeed = 3;
        }

        ///else if (playerDirection.transform.rotation.eulerAngles.y == 0 & playerSpeed < 2)
        else if (Input.GetKey(KeyCode.E))
        {
            Debug.Log("Using E");
            playerSpeed = 0;
            playerAnimator.SetBool("Shield", true);
            playerAnimator.SetBool("Caminar", false);
            ballTrigger.gameObject.SetActive(true);
        }
        else if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
        {
            transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * playerSpeed * Time.deltaTime, 0f, 0f));
            lastMove = new Vector3(Input.GetAxisRaw("Horizontal"), 0f);
        }

        //Angulos de Euler (by ralke)
        // Se utiliza indleBlendValue para fijar direccion a traves de variable float

        // Norte
        else if (playerDirection.transform.rotation.eulerAngles.y == 0 && playerSpeed == 0)
        {
            Debug.Log("Miro Arriba");
            playerAnimator.SetBool("Caminar", false);
            idleBlendValue = 2f;
            playerAnimator.SetFloat("IdleBlend", idleBlendValue);
            playerSpeed = 0;
        }

        // Sur
        else if (playerDirection.transform.rotation.eulerAngles.y == 180 && playerSpeed == 0)
        {
            Debug.Log("Miro Abajo");
            //playerAnimator.SetBool("Caminar", false);
            playerAnimator.SetBool("Shield", false);
            idleBlendValue = 0f;
            playerAnimator.SetFloat("IdleBlend", idleBlendValue);
            playerSpeed = 0;
        }

        // Oeste (izquierda)
        else if (playerDirection.transform.rotation.eulerAngles.y == 270 && playerSpeed == 0)
        {
            Debug.Log("Miro Izquierda");
            //playerAnimator.SetBool("Caminar", false);
            playerAnimator.SetBool("Shield", false);
            idleBlendValue = 3f;
            playerAnimator.SetFloat("IdleBlend", idleBlendValue);
            playerSpeed = 0;
        }
        // Este (derecha)
        else if (playerDirection.transform.rotation.eulerAngles.y == 90 && playerSpeed == 0)
        {
            Debug.Log("Miro Derecha");
            //playerAnimator.SetBool("Caminar", false);
            playerAnimator.SetBool("Shield", false);
            idleBlendValue = 4f;
            playerAnimator.SetFloat("IdleBlend", idleBlendValue);
            playerSpeed = 0;
        }

        // Condicionales restantes (no entran en ninguna variable)
        else
        {
            playerAnimator.SetBool("Caminar", false);
            playerAnimator.SetBool("Shield", false);
            ballTrigger.gameObject.SetActive(false);
            playerSpeed = 0;
        }

    }
}
 
Back
Top