Search Unity

problem with object rotation

Discussion in 'Scripting' started by Trild123787898, Jan 27, 2020.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    for the auxiliary UI Button, I visit this line of code, how can I make the object click on the button turn 75 degrees and when he press the button again it returns to its original coordinates, that is, 0, I can’t find how to do it
    Code (CSharp):
    1. if (click_detail4_1 == true)
    2.         {
    3.             detail4_1.localRotation = Quaternion.RotateTowards(detail4_1.localRotation, Quaternion.Euler(new Vector3(0, 75, 0)), 40f * Time.deltaTime);
    4.  
    5.         }
    6.         if (click_detail4_2 == true)
    7.         {
    8.             detail4_2.localRotation = Quaternion.RotateTowards(detail4_2.localRotation, Quaternion.Euler(new Vector3(0, -75, 0)), 40f * Time.deltaTime);
    9.         }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You haven't provided very much context. When is this code running? Every Update? What's click_detail4_1? When is it set to true? If you can provide more of the code this will be easier to figure out.
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.EventSystems;
    6.  
    7.  
    8. public class mehanic : MonoBehaviour
    9. {
    10.    
    11.     public Transform detail4_1;
    12.     public Transform detail4_2;
    13.    
    14.     private bool click_detail4_1;
    15.     private bool click_detail4_2;
    16.    
    17.  
    18.    
    19.     public void TaskOnClickDetail4_1()
    20.     {
    21.         click_detail4_1 = true;
    22.  
    23.     }
    24.     public void TaskOnClickDetail4_2()
    25.     {
    26.         click_detail4_2 = true;
    27.  
    28.     }
    29.    
    30.  
    31.  
    32.     void FixedUpdate()
    33.     {
    34.      
    35.         if (click_detail4_1 == true)
    36.         {
    37.             detail4_1.localRotation = Quaternion.RotateTowards(detail4_1.localRotation, Quaternion.Euler(new Vector3(0, 75, 0)), 40f * Time.deltaTime);
    38.  
    39.         }
    40.         if (click_detail4_2 == true)
    41.         {
    42.             detail4_2.localRotation = Quaternion.RotateTowards(detail4_2.localRotation, Quaternion.Euler(new Vector3(0, -75, 0)), 40f * Time.deltaTime);
    43.         }
    44.        
    45.     }
    46.  
    47.  
    48.  
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    (I'm assuming TaskOnClickDetail4_1/2 are being called by buttons.) You don't have anything that sets either click_detail to "false", so there's no way for it to know when you're supposed to be returning to the original rotation. If you want it to toggle each time you press the button:
    Code (csharp):
    1. click_detail4_1 = !clickdetail4_1;
    That will make it stop moving towards the 75 degree rotation, but there's nothing to make it start moving back to the original. You probably want to do something like this:
    Code (csharp):
    1.         if (click_detail4_1 == true)
    2.         {
    3.             detail4_1.localRotation = Quaternion.RotateTowards(detail4_1.localRotation, Quaternion.Euler(new Vector3(0, 75, 0)), 40f * Time.deltaTime);
    4.  
    5.         }
    6.         else  
    7.         {
    8.             detail4_1.localRotation = Quaternion.RotateTowards(detail4_1.localRotation, Quaternion.Euler(new Vector3(0, 0, 0)), 40f * Time.deltaTime);
    9.         }
     
    Trild123787898 likes this.
  5. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    thanks more that everything is clearly explained, I tried to use click_detail4_1 =! clickdetail4_1; earlier, but I just scored that I need to use another else, and this was my mistake