Search Unity

Trying to lock the rotation of a game object

Discussion in 'Scripting' started by sootoochristopher, Aug 30, 2019.

  1. sootoochristopher

    sootoochristopher

    Joined:
    Jul 29, 2019
    Posts:
    14
    As the title states, I’m just trying to lock the rotation of an object, but at its current rotation... the player constantly rotates this object, but when a certain condition is met I want to freeze the GameObjects rotation. Here’s what I have so far

    Code (CSharp):
    1. public class RedandBlueCollide : Monobehaviour
    2. {
    3.     private GameObject Purple;
    4.  
    5.     void Start()
    6.     {
    7.         Purple = GameObject.Find(“PurpleGameObject”);
    8.     }
    9.  
    10.     void OnTriggerEnter2D(Collider2D other)
    11.     {
    12.         if(other.CompareTag(“anothergameobject”))
    13.         {
    14.             //Somehow lock rotation
    15.             Purple.transform.rotation = // Previous rotation right before collision and freeze it or something
    16.         }
    17.     }
    18. }
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    I believe you're trying to take control away from the player when the condition is met more so than fix their rotation. You can do that with a private boolean.
    Code (CSharp):
    1.  
    2. private bool freezeRotation;
    3.  
    4. private void Update(){
    5.     if(!freezeRotation){
    6.         //your player controls rotation
    7.     }
    8. }
    9. private void OnTriggerEnter(Collider2D other){
    10.     if(/*your condition*/){
    11.         freezeRotation = true;
    12.     }
    13. }
    Of course, with this setup youll have to have some other event to unfreeze it at some point for the player to be able to rotate again.

    EDIT
    I just realised, you did not mention which object contains your code snipplet, so its hard to give advice on how well your scripts are being segmented and utilized
     
  3. sootoochristopher

    sootoochristopher

    Joined:
    Jul 29, 2019
    Posts:
    14
    Sorry, for some reason I didn’t get notified...
    This code is attached to Red.
    So I’m finding the purple game object and trying to freeze its position from where it is.
    I’m really sorry but I don’t understand what your talking about. If I just have a true or false thing how do I access the purple game object’s current rotation and freeze it? I guess what I’m saying is I understand what your talking about but I still don’t know how to freeze it’s rotation.
    You simply gave me an example of setting a variable true or false according to a condition if met. When it is true, how do I freeze purple’s rotation?

    When Red and Blue collide I also enable the purple’s renderer and collider and disable the Red and Blue’s, which I have completed. Now when they collide I want the purple to appear on screen in a frozen rotation this way when the player rotates the red and it collided with the blue the purple will appear at that rotation, but I just need to freeze it’s rotation at that given time. The player controls red with one knob and blue with another, while secretly the player is controlling the purple with the same controls as red, but it’s collider and tenderer aren’t on so it basically doesn’t interact in any way.
     
    Last edited: Aug 31, 2019
  4. EdGunther

    EdGunther

    Joined:
    Jun 25, 2018
    Posts:
    183
    Can you define what you mean by "freeze" its rotation? If nothing is happening to the gameObject, it will not rotate. What is causing the gameObject to rotate? If you need to disable player controls, make a boolean as @Laperen said


     
  5. sootoochristopher

    sootoochristopher

    Joined:
    Jul 29, 2019
    Posts:
    14
    Sorry about that, what I mean by freeze is basically making it to where the player can no longer control it.
    I was thinking that I should just access it’s rotation and disable it, but I guess I might have to figure something else out
     
  6. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    If you post exact code on how you are rotating your objects (not just code snippets) and tell us what you want it to instead, I'm sure we can help. There's always a solution.
     
  7. sootoochristopher

    sootoochristopher

    Joined:
    Jul 29, 2019
    Posts:
    14
    Ok
    For the red
    Code (CSharp):
    1. public int speed;
    2.  
    3. Void Update()
    4. {
    5.     float Rx = Input.GetAxis (“RS_h”);
    6.     float Ry = Input.GetAxis (“RS_v”);
    7.  
    8.     float Rrotate = (Mathf.Atan2(Rx,Ry) * Mathf.Rad2Deg);
    9.  
    10. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,Rrotate + 180), speed*Time.deltaTime);
    11.  
    12. if (Rx == 0 && Ry == 0)
    13.     {
    14.         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,0),speed * Time.deltaTime);
    15.     }
    16. }
    and the purple has the same script but with variables according to it

    Code (CSharp):
    1. public int speed;
    2.  
    3. Void Update()
    4. {
    5.     float Px = Input.GetAxis (“RS_h”);
    6.     float Py = Input.GetAxis (“RS_v”);
    7.  
    8.     float Protate = (Mathf.Atan2(Px,Py) * Mathf.Rad2Deg);
    9.  
    10. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,Protate + 180), speed*Time.deltaTime);
    11.  
    12. if (Px == 0 && Py == 0)
    13.     {
    14.         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,0),speed * Time.deltaTime);
    15.     }
    16. }
    And another code attached to Blue

    Code (CSharp):
    1. public int speed;
    2.  
    3. Void Update()
    4. {
    5.     float Lx = Input.GetAxis (“LS_h”);
    6.     float Ly = Input.GetAxis (“LS_v”);
    7.  
    8.     float Lrotate = (Mathf.Atan2(Lx,Ly) * Mathf.Rad2Deg);
    9.  
    10. transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,Lrotate), speed*Time.deltaTime);
    11.  
    12. if (Lx == 0 && Ly == 0)
    13.     {
    14.         transform.rotation = Quaternion.RotateTowards(transform.rotation, Quaternion.Euler(0,0,180),speed * Time.deltaTime);
    15.     }
    16. }
    And finally when Red and Blue collide

    Code (CSharp):
    1. SpriteRenderer RenderRed;
    2. SpriteRenderer RenderBlue;
    3. SpriteRenderer RenderPurple;
    4.  
    5. PolygonCollider2D CollideRed;
    6. PolygonCollider2D CollideBlue;
    7. PolygonCollider2D CollidePurple;
    8.  
    9. private GameObject Red;
    10. private GameObject Blue;
    11. private GameObject Purple;
    12.  
    13. void Start()
    14.     {
    15.         Red = GameObject.Find(“RedObject”);
    16.         Blue = GameObject.Find(“BlueObject”);
    17.         Purple = GameObject.Find(“PurpleObject”);
    18.  
    19.         RenderRed = Red.GetComponent<SpriteRenderer>();
    20.         RenderBlue = Blue.GetComponent<SpriteRenderer>();
    21.         RenderPurple = Purple.GetComponent<SpriteRenderer>();
    22.         RenderRed.enabled = true;
    23.         RenderBlue.enabled = true;
    24.         RenderPurple.enabled = false;
    25.  
    26.         CollideRed = Red.GetComponent<PolygonCollider2D>();
    27.         CollideBlue = Blue.GetComponent<PolygonCollider2D>();
    28.         CollidePurple= Purple.GetComponent<PolygonCollider2D>();
    29.         CollideRed.enabled = true;
    30.         CollideBlue.enabled = true;
    31.         CollidePurple.enabled = false;
    32.     }
    33.  
    34. void OnTriggerEnter2D(Collider2D other)
    35.     {
    36.         if(other.CompareTag(“BlueTag”))
    37.             {
    38.                 RenderRed.enabled = false;
    39.                 RenderBlue.enabled = false;
    40.                 RenderPurple.enabled = true;
    41.  
    42.                 CollideRed.enabled = false;
    43.                 CollideBlue.enabled = false;
    44.                 CollidePurple.enabled = true;
    45. //I was trying to add the freeze rotation to the purple here but not sure
    46.             }
    47.     }
     
  8. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Create a:
    public bool canRotate;


    variable in your purple script. Then place an:
    Code (CSharp):
    1. private void Update()
    2. {
    3.    if(canRotate)
    4.    {
    5.       //Your rotation code here
    6.    }
    7. {
    Then, when you want to disable the rotation, do something like this:
    Code (CSharp):
    1. if(other.CompareTag("BlueTag")
    2. {
    3.    /// Replace the <PurpleRotationScript> with actual reference to the rotation script on your purple object
    4.    <PurpleRotationScript>.canRotate = false;
    5. }
     
  9. sootoochristopher

    sootoochristopher

    Joined:
    Jul 29, 2019
    Posts:
    14
    I’m getting Invalid expression term’<‘
    and Invalid expression term ‘.’
    Do I have to do something like
    Code (CSharp):
    1. PurpleRotateScript = Purple.GetComponent<ActualPurpleRotateScript>();
    The controls for purple are on a different script
    And the collision code is on another.
    I see how this could easily work but I don’t know how to get variables from another script.
    I’ve watched videos and tried to figure it out but it doesn’t ever seem to work, and I just get a bunch of errors
     
    Last edited: Sep 1, 2019
  10. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    So you're rotating via an Update loop. Just take that script with the trigger code in your first post and instead of it locking a rotation, just have it reference the script thats doing the rotation and disable it. Simple.
     
  11. sootoochristopher

    sootoochristopher

    Joined:
    Jul 29, 2019
    Posts:
    14
    I know, but how do you reference the bool in the other script? I’m sure it’s simple but I’m still learning the language and it’s pretty difficult figuring it out by trial and error
     
  12. RoastedLemon_

    RoastedLemon_

    Joined:
    Sep 1, 2019
    Posts:
    1
    I'm having a quite similar issue, do you mind elaborating on how to do that?
     
  13. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    Code (CSharp):
    1. PurpleRotateScript = Purple.GetComponent<YourScriptThatsActuallyRotatingThePurpleObject>();
    2.  
    3. // Access a variable like this. Make sure the variable is public
    4. PurpleRotateScript.canRotate = false
     
  14. sootoochristopher

    sootoochristopher

    Joined:
    Jul 29, 2019
    Posts:
    14
    Another error- does not exist in the current context

    Do I also need to make a private game object for this or something of the sort?
    Like
    Code (CSharp):
    1. private GameObject PurpleRotateScript;
    Before that?

    But when I do that I get more errors

    Like the other components I have to reference them in variables at the beginning. Like PolygonCollider and SpriteRenderer. But how do I do that with a script?
     
    Last edited: Sep 4, 2019