Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Forcing gameobject to rotate to a certain degree

Discussion in 'Scripting' started by Farooqui, Feb 3, 2016.

  1. Farooqui

    Farooqui

    Joined:
    Mar 9, 2014
    Posts:
    23
    Hello Folks,
    I am working on this endless runner, player can tilt phone left and right to make the "gameobject" move and use swipe "up" and "down" to jump and duck, problem right now i am facing is that i assigned a "back flip" animation for jump, When the person jumps she lands with different rotations which is causing problems. For example, player is at (0,0,0) rotation, after back flip, (0,14,0) etc. And I cannot figure out how to force her to face (0,0,0) again. Please help!

    Code (CSharp):
    1.     void FixedUpdate(){
    2.  
    3.         Vector3 movement = new Vector3 (Input.acceleration.x, 0.0f, 0.0f);
    4.         GetComponent<Rigidbody>().velocity = movement * speed;
    5.         if (Input.touchCount > 0 && Time.timeScale > 0.0f)
    6.         {
    7.            
    8.             foreach (Touch touch in Input.touches)
    9.             {
    10.                
    11.                 switch (touch.phase)
    12.                 {
    13.                    
    14.                 case TouchPhase.Began:
    15.                    
    16.                     isSwipe = true;
    17.                     fingerStartTime = Time.time;
    18.                     fingerStartPos = touch.position;
    19.                     break;
    20.                    
    21.                 case TouchPhase.Canceled:
    22.                    
    23.                     isSwipe = false;
    24.                     break;
    25.                    
    26.                 case TouchPhase.Ended:
    27.                    
    28.                     float gestureTime = Time.time - fingerStartTime;
    29.                     float gestureDist = (touch.position - fingerStartPos).magnitude;
    30.                    
    31.                     if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist)
    32.                     {
    33.                        
    34.                         Vector3 direction = touch.position - fingerStartPos;
    35.                         //Vector2 swipeType = Vector2.zero;
    36.                         int swipeType = -1;
    37.                         if (Mathf.Abs(direction.normalized.x) > 0.9)
    38.                         {
    39.                            
    40.                             if (Mathf.Sign(direction.x) > 0) swipeType = 0; // swipe right
    41.                             else swipeType = 1; // swipe left
    42.                            
    43.                         }
    44.                         else if (Mathf.Abs(direction.normalized.y) > 0.9 && grounded ==true)
    45.                         {
    46.                             if (Mathf.Sign(direction.y) > 0) swipeType = 2; // swipe up
    47.                             else swipeType = 3; // swipe down
    48.                         }
    49.                         else
    50.                         {
    51.                             // diagonal:
    52.                             if (Mathf.Sign(direction.x) > 0)
    53.                             {
    54.                                
    55.                                 if (Mathf.Sign(direction.y) > 0) swipeType = 4; // swipe diagonal up-right
    56.                                 else swipeType = 5; // swipe diagonal down-right
    57.                                
    58.                             }
    59.                             else
    60.                             {
    61.                                
    62.                                 if (Mathf.Sign(direction.y) > 0) swipeType = 6; // swipe diagonal up-left
    63.                                 else swipeType = 7; // swipe diagonal down-left
    64.                                
    65.                             }
    66.                            
    67.                         }
    68.                        
    69.                         switch(swipeType)
    70.                         {
    71.                            
    72.                         case 0: //right
    73.                    
    74.                             break;
    75.                            
    76.                            
    77.                         case 1: //left
    78.                  
    79.                             break;
    80.                            
    81.                         case 2: //up
    82.  
    83.                             float thrust=1000;
    84.                             GetComponent<Rigidbody>().AddForce(transform.up * thrust);
    85.                            
    86.                             print (transform.rotation = transform.rotation * Quaternion.Euler(0, 0, 0));
    87.                             player.SendMessage("jumpTrueDelay");
    88.                             grounded=false;
    89.                             break;
    90.                            
    91.                         case 3: //down
    92.                             player.SendMessage("crouchNow");
    93.                            
    94.                             break;
    95.                         case 4: //up right
    96.                            
    97.                             break;
    98.                         case 5: //down right
    99.                            
    100.                             break;
    101.                            
    102.                         case 6: //up left
    103.                            
    104.                             break;
    105.                            
    106.                         case 7: //down left
    107.                            
    108.                            
    109.                             break;
    110.                            
    111.                         }
    112.                        
    113.                     }
    114.                    
    115.                     break;
    116.                    
    117.                 }
    118.                
    119.             }
    120.            
    121.         }
    122.  
    123.     }
    124.    
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Are you unable to simply detect when the character is grounded and then force the rotation to (0,0,0)?
     
  3. Farooqui

    Farooqui

    Joined:
    Mar 9, 2014
    Posts:
    23
    Ground detection works fine, I am using OnCollision to detect, using a capsule collider and rigidbody with gravity on
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Are you using OnCollisionStay or OnCollisionEnter? In either case what's stopping you from resetting rotation there?
     
    Last edited: Feb 3, 2016
  5. Farooqui

    Farooqui

    Joined:
    Mar 9, 2014
    Posts:
    23
    OnCollisionEnter, so I tried something:
    transform.Rotate(Vector3.up*50*time.deltaTime);
    and this seems to actually work

    I wish i knew why this worked but trying something like this didnt:
    rotation= Quaternion.Eular(0,0,0);

    Both in update function
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Both "transform.Rotate" and "Quaternion.Euler" take in Euler angle, which is the one most people associate angles with, being the 0 to 360 representing a complete round.
     
  7. Farooqui

    Farooqui

    Joined:
    Mar 9, 2014
    Posts:
    23
    oh, that makes sense, thanks Laperen!!
     
  8. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Usually when i want to "reset" the rotation of a character, I use Quaternion.LookRotation() instead. what it does is map a Vector3 direction representing the forward direction, to another Vector3 representing the up axis.