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.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How do I stop the constant rotation from the same key?

Discussion in '2D' started by CptBubbleButt, Nov 26, 2018.

  1. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    Hey everyone, so I am currently building a Roguelike style game for a college project; and an issue popped up to do with the looking around of my player (as I intend using WASD for movement and the ARROW keys for shooting - inspired by The Binding of Isaac.

    Here is the code that I got for the rotation of my player:
    Code (CSharp):
    1. public class RotateAround : MonoBehaviour {
    2.  
    3.     public Transform pointOfRotation;
    4.     // public float RotSpeed = 5f;
    5.  
    6.  
    7.     void Update()
    8.     {
    9.         if(Input.GetKeyDown(KeyCode.UpArrow))
    10.             {
    11.             transform.RotateAround (pointOfRotation.transform.position, Vector3.forward, 90);
    12.  
    13.             }
    14.         if(Input.GetKeyDown(KeyCode.DownArrow))
    15.         {
    16.             transform.RotateAround (pointOfRotation.transform.position, Vector3.forward, 90);
    17.  
    18.         }
    19.         if(Input.GetKeyDown(KeyCode.RightArrow))
    20.         {
    21.             transform.RotateAround (pointOfRotation.transform.position, Vector3.forward, 90);
    22.  
    23.         }
    24.         if(Input.GetKeyDown(KeyCode.LeftArrow))
    25.         {
    26.             transform.RotateAround (pointOfRotation.transform.position, Vector3.forward, 90);
    27.  
    28.         }
    29.  
    30.        
    31.  
    32.     }
    33. }
    What I aim for the code to do, is when I press the Up Arrow I want the player to look up but when I press it again I want it to stay looking up (it is currently moving an extra 90 degrees than it should be). I was thinking about messing about with a LookAt() function but I don't know if that is the correct function to use.

    P.S. I aim to have the object I want to rotate around the pivot in the middle of the player.
     
  2. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    You could add 4 booleans to each of the directions
    Code (CSharp):
    1. public class RotateAround : MonoBehaviour {
    2.     public Transform pointOfRotation;
    3.     // public float RotSpeed = 5f;
    4.  
    5. private bool up = false;
    6. private bool left = false;
    7.  
    8. private bool right = false;
    9.  
    10. private bool down = false;
    11.     void Update()
    12.     {
    13.         if(Input.GetKeyDown(KeyCode.UpArrow) && up = true)
    14.             {
    15.            
    16. up = true;
    17. down = flase;
    18. left = false;
    19. right = false;
    20.  
    21. transform.RotateAround (pointOfRotation.transform.position, Vector3.forward, 90);
    22.             }
    23.         if(Input.GetKeyDown(KeyCode.DownArrow))
    24.         {
    25.             transform.RotateAround (pointOfRotation.transform.position, Vector3.forward, 90);
    26.         }
    27.         if(Input.GetKeyDown(KeyCode.RightArrow))
    28.         {
    29.             transform.RotateAround (pointOfRotation.transform.position, Vector3.forward, 90);
    30.         }
    31.         if(Input.GetKeyDown(KeyCode.LeftArrow))
    32.         {
    33.             transform.RotateAround (pointOfRotation.transform.position, Vector3.forward, 90);
    34.         }
    35.      
    36.     }
    37. }
     
    vakabaka likes this.
  3. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    Ooooo I completely messed up the title, I meant to say "How do I stop the constant rotation using the same key?" oops!
     
    Helladah likes this.
  4. Helladah

    Helladah

    Joined:
    May 5, 2018
    Posts:
    254
    thats cause i say, if there is a boolean there, it wont activate the same twice
     
  5. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    Booleans won't work as I am updating it every frame meaning it will rotate every 90 degrees (which is what I don't want it to do)
     
  6. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    boolean works always :D
    Code (CSharp):
    1. //dont execute if bool is true
    2. if(Input.GetKeyDown(KeyCode.UpArrow) &&  !up)
    3.             {
    4.        
    5. up = true;
    6. down = flase;
    7. left = false;
    8. right = false;
    I would, perhaps, use transform.up (right, -up,...)
    Code (CSharp):
    1.  if(Input.GetKeyDown(KeyCode.UpArrow))
    2.             {
    3.             transform.up = Vector3.up;
    4.             //depended where is the forward from the sprite
    5.             //it can be transform.right = Vector3.up;
    6.             }
     
    Last edited: Nov 27, 2018
    Helladah likes this.
  7. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    How do I apply movement to the transform example? As it only rotates on a single point, not around the rotation point like what the "transform.RotateAround" does, feeling on giving up this style of controls but giving it one more try.
     
  8. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    I would go for either LookAt or RotateTowards.
     
  9. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    sorry, I didn't have seen that you are using transform.RotateAround, thought about sprite with 4 directions.
     
  10. CptBubbleButt

    CptBubbleButt

    Joined:
    Apr 24, 2018
    Posts:
    27
    Figuring out how to use RotateTowards() but I don't really understand it, is there a proper way into setting it up?