- Home /
How to use an if statement to make sure an object has the right transform.rotation
I am making a game where the player will move forward at all times and when they press the arrow keys, they should turn sharply 90 degrees in that direction. I am trying to make it so that the player can only turn a certain direction when they are facing another certain direction. for instance, if a player is going forward, I don't want them to be able to go backward by pressing the back key, they can only go left or right. However when I use my code, it won't allow me to turn even when the conditions are met. Why is this? Thank you! Code: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour { public int moveSpeed;
public GameObject player;
public Vector3 direction = Vector3.left;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Moves player forward in the direction it is facing
transform.position += transform.up * moveSpeed * Time.deltaTime;
// The player may only turn in this direction if it has the rotation of (-90, 0, 0)
if(Input.GetKeyDown("up" && player.transform.rotation.x == -90))
{
transform.Rotate(90, 0, 0);
}
}
}
Answer by sacredgeometry · Dec 01, 2021 at 05:42 AM
Would that even build?
if(Input.GetKeyDown("up" && player.transform.rotation.x == -90))
should be
if(Input.GetKeyDown("up") && player.transform.rotation.x == -90)
For a start
Yeah it would build, Thanks for your time trying to help. My script needed way too much alteration. I finally figured it out. Thanks again!
Answer by jcmangold · Dec 01, 2021 at 02:35 PM
Edit: I think I figured out what I was doing wrong.(I did soo much wrong) First, I realized I would need a bool to check if the player was facing right. But when I tried to put that in the if statement, it didn't work: if(Input.GetKeyDown("up" && rotationCheck == true)) This was because unity cannot use both a string and a bool in one if statement. So I converted the bool to a string using rotationCheck.ToString(); For some reason, I had to make the variable static. Anyone know why? Then I created some if statements defining the bool.(if it was true or false) I also had to add a Quaternion variable to hold the transform direction I needed. I am not sure if that all makes sense. sorry bout that. Here is my script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using System;
public class PlayerController : MonoBehaviour { public int moveSpeed; public int lookingRight = -90;
public static bool rotationCheck;
public string rotation = rotationCheck.ToString();
public Vector3 direction = Vector3.left;
public Quaternion right;
public GameObject player;
// Start is called before the first frame update
void Start()
{
right = Quaternion.Euler(lookingRight, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z);
}
// Update is called once per frame
void Update()
{
// Moves player forward in the direction it is facing
transform.position += transform.up * moveSpeed * Time.deltaTime;
// Make sure player is facing right
if(transform.rotation == right)
{
rotation = ("true");
}else
{
rotation = ("false");
}
// The player may only turn in this direction if it has the rotation of (-90, 0, 0)
if(Input.GetKeyDown("up") && rotation == ("true"))
{
transform.Rotate(90, 0, 0);
}
}
}
Your answer
