Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jcmangold · Nov 29, 2021 at 05:31 PM · transform.rotation

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);
         
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jcmangold · Dec 01, 2021 at 02:37 PM 0
Share

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!

avatar image
0

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);
         
     }
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

transform.Rotate vs. transform.rotation = when use which? 4 Answers

Possible way to improve performance moving large hierarchies? 1 Answer

Adding Rotation to a Gameobject 1 Answer

How to rotate CharacterController on slope 0 Answers

How to make character move in the direction its facing 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges