Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Variable not changing

Discussion in 'Scripting' started by vukvukanovic, May 4, 2024.

  1. vukvukanovic

    vukvukanovic

    Joined:
    May 10, 2023
    Posts:
    1
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class playerMovement : MonoBehaviour
    6. {
    7.     public int playerDirection; //0 = bottom right 1 = top right 2 = top left 3 = bottom left
    8.     public GameObject directionPointer;
    9.     public Vector2 currentMovementDirection;
    10.     public float moveSpeed = 2f;
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if(directionPointer.transform.rotation.z == -45)
    19.         {
    20.             playerDirection = 0;
    21.         }
    22.         else if(directionPointer.transform.rotation.z == 45)
    23.         {
    24.             playerDirection = 1;
    25.         }
    26.         else if (directionPointer.transform.rotation.z == 135)
    27.         {
    28.             playerDirection = 2;
    29.         }
    30.         else if (directionPointer.transform.rotation.z == 225)
    31.         {
    32.             playerDirection = 3;
    33.         }
    34.     }
    35.     private void FixedUpdate()
    36.     {
    37.         if(playerDirection == 0)
    38.         {
    39.             Vector2 currentMovementDirection = new Vector2(1, -1);
    40.             transform.Translate(currentMovementDirection * moveSpeed * Time.deltaTime);
    41.         }
    42.         else if(playerDirection == 1)
    43.         {
    44.             Vector2 currentMovementDirection = new Vector2(1, 1);
    45.             transform.Translate(currentMovementDirection * moveSpeed * Time.deltaTime);
    46.         }
    47.         else if (playerDirection == 2)
    48.         {
    49.             Vector2 currentMovementDirection = new Vector2(-1, 1);
    50.             transform.Translate(currentMovementDirection * moveSpeed * Time.deltaTime);
    51.         }
    52.         else if (playerDirection == 3)
    53.         {
    54.             Vector2 currentMovementDirection = new Vector2(-1, -1);
    55.             transform.Translate(currentMovementDirection * moveSpeed * Time.deltaTime);
    56.         }
    57.     }
    58. }
    What I'm trying to do here is make the player go in the direction of the direction pointer. However, the variable always defaults to 0 and the player goes down-right even if the direction pointer is pointing somewhere else. I dragged the direction pointer into the script, I dragged the script into the player object and yet it still doesn't work. (don't mind my bad code, im just starting out!)
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,931
    Code (CSharp):
    1. if(directionPointer.transform.rotation.z == -45)
    2.  
    This code and all similar code in your script is nonsense. You are trying to treat a quaternion as if it is a set of Euler Angles. It is not.

    Do yourself a favor and do this:
    Code (CSharp):
    1. Debug.Log("The number I'm looking at is: " + transform.rotation.z);
     
    Bunny83 and Ryiah like this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,448
    Code (csharp):
    1. if(directionPointer.transform.rotation.z == -45)
    In addition to the above checking for equality against a floating point number won't work consistently because of the inherent inaccuracies in floating point numbers. Instead you need to use
    Mathf.Approximately
    which takes into account the minor inaccuracies that build up.

    https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html
     
    Bunny83 and PraetorBlue like this.
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    If you're planning on making an isometric game or similar then you may find it easier to just rotate the camera. This way you won't have to write strange code that moves everything in a 45 degree direction.
     
    Bunny83 and PraetorBlue like this.