Search Unity

Transform.rotate does not work in a new scene.

Discussion in 'Scripting' started by Jubcow, Jul 27, 2018.

  1. Jubcow

    Jubcow

    Joined:
    May 28, 2018
    Posts:
    4
    I created a script to manually rotate a player's camera at a tilt when they enter a trigger and hold space. The script works fine in two of my previous scenes, however now that I have made a new scene, it no longer functions in the same manner.
    Here is my script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. /*
    6. * Wallrunning script, meant to be attached to player object (with FirstPersonController & a camera) that will be running into an invisible trigger box along a wall
    7. */
    8.  
    9. public class CameraTrigger : MonoBehaviour
    10. {
    11.     //public GameObject player; //this would allow you to select the player object in inspector, but not necessary if you just use transform.rotate instead of player.transform.rotate
    12.     Quaternion rota = Quaternion.Euler(0.0f, 0.0f, 30f); //placeholder quaternion
    13.     bool inTrig = false;
    14.     bool inLadder = false;
    15.     //bool waitDone = false;
    16.     bool onRight = false;
    17.     bool onLeft = false;
    18.     bool forward = false;
    19.     bool pressedSpace = false;
    20.     bool letGoOfSpace = false;
    21.     private RaycastHit hr;
    22.     private RaycastHit lr;
    23.     int i; //used to animate wallrunning head tilt
    24.     int p; //used to animate tilt back to 0
    25.     int f; //used to animate tilt back after a forward wallrun
    26.    
    27.     void OnTriggerEnter(Collider other) //when they player enters the trigger, they set the boolean to true
    28.     {
    29.         if (other.tag == "WallRun") {
    30.             inTrig = true;
    31.             letGoOfSpace = false;
    32.             i = 0;
    33.             p = 0;
    34.             f = 0;
    35.         }
    36.        
    37.         if(other.tag == "Climb")
    38.         {
    39.             inLadder = true;
    40.         }
    41.     }
    42.    
    43.     void OnTriggerExit(Collider other) //exit will set trigger to false
    44.     {  
    45.         if(other.tag == "WallRun") {
    46.             inTrig = false;
    47.             i = 0;
    48.             p = 0;
    49.             f = 0;
    50.         }
    51.        
    52.         if (other.tag == "Climb")
    53.         {
    54.             inLadder = false;
    55.         }
    56.     }
    57.  
    58.    
    59.     private void Update()
    60.     {      
    61.         if (inTrig == true) {// After delay, for every frame in the trigger, while you press either space, A, or D : you will have lowered gravity (in this case -5F)  //waitDone == true &&
    62.             if (Input.GetKey(KeyCode.Space) || Input.GetKey(KeyCode.Joystick1Button4))
    63.             {
    64.                 pressedSpace = true;
    65.                 letGoOfSpace = false;
    66.                 Physics.gravity = new Vector3(0, -5F, 0); //this determines the new gravity while in the trigger
    67.                 //Debug.Log("should be low grav now");
    68.                 if (Physics.Raycast(transform.position, transform.right, out hr, 1)) //this is how we determine which side the wall is on, and thus the player tilt (player position, direction, out , distance from wall)
    69.                 {
    70.                     onRight = true;
    71.                     if (i < 30) //this sequence effectively animates the camera tilt to the RIGHT
    72.                     {
    73.                         transform.Rotate(0, 0, i);
    74.                         i += 2;
    75.                     }
    76.                     else
    77.                         transform.Rotate(0, 0, 30);
    78.                 }
    79.                 else if (Physics.Raycast(transform.position, -transform.right, out hr, 1)) //if the player has a wall to the left (-transform.right means left) ALSO KEEP THIS ORDER OF ELSE IFS, having the forward come before left makes the left wallruns camera buggy
    80.                 {
    81.                     onLeft = true;
    82.                     if (i < 30) //this sequence effectively animates the camera tilt to the LEFT
    83.                     {
    84.                         transform.Rotate(0, 0, -i);
    85.                         i += 2;
    86.                     }
    87.                     else
    88.                         transform.Rotate(0, 0, -30);
    89.                 }
    90.                 else if (Physics.Raycast(transform.position, transform.forward, out hr, 0.8f)) //if the player has a wall directly in front of them (.8f distance for this so if they look around it's not as jerky)
    91.                 {
    92.                     forward = true;
    93.                     if (onLeft == false && onRight == false) //may seem redundant, but it prevents bugginess when raycasts are getting forward as well as a left or right
    94.                     {
    95.                         if (i < 30) //this sequence effectively animates the camera tilt UP
    96.                         {
    97.                             transform.Rotate(-i, 0, 0);
    98.                             i += 2;
    99.                         }
    100.                         else
    101.                             transform.Rotate(-30, 0, 0);
    102.                     }
    103.                 }
    104.             }
    105.             else
    106.             {
    107.                 if (pressedSpace) //this means they let go of space while in the trigger
    108.                 {
    109.                     Physics.gravity = new Vector3(0, -15F, 0); //returns gravity to -15F, change to whatever your project's default gravity value is
    110.                     letGoOfSpace = true;
    111.                 }
    112.             }
    113.         }
    114.         else
    115.         {
    116.             if (!letGoOfSpace)
    117.             {
    118.                 if (inLadder == false)
    119.                 {
    120.                     Physics.gravity = new Vector3(0, -15F, 0); //returns gravity to -15F, change to whatever your project's default gravity value is
    121.                 }
    122.                 //starting leave trigger animation (animates tilt of camera back to 0,0,0)
    123.                 if (onRight && i <= 30)
    124.                 {
    125.                     i += 2;
    126.                     //Debug.Log(i);
    127.                     transform.Rotate(0, 0, (-i + 30));
    128.                 }
    129.                 else
    130.                 {
    131.                     transform.Rotate(0, 0, 0);
    132.                     i = 0;
    133.                     onRight = false;
    134.                 }
    135.  
    136.                 if (onLeft && p <= 30)
    137.                 {
    138.                     p += 2;
    139.                     //Debug.Log(p);
    140.                     transform.Rotate(0, 0, (p - 30));
    141.                 }
    142.                 else
    143.                 {
    144.                     transform.Rotate(0, 0, 0);
    145.                     p = 0;
    146.                     onLeft = false;
    147.                 }
    148.  
    149.                 if (forward && f >= -30)
    150.                 {
    151.                     f -= 2;
    152.                     //Debug.Log(f);
    153.                     transform.Rotate((-f - 30), 0, 0);
    154.                 }
    155.                 else
    156.                 {
    157.                     transform.Rotate(0, 0, 0);
    158.                     f = 0;
    159.                     forward = false;
    160.                 }
    161.             }
    162.             //ending leaving trigger anim
    163.         }
    164.     }
    165. }
    Some things to note:
    I have the script attached to a player object that also has a FirstPersonController, character controller, rigidbody, and capsule collider. That player object also has a camera object as a child.

    I copied and pasted my player object from a previous scene in which it worked. The player object has the same tag and other values.

    The wall (with accompanying trigger) is a prefab from the same previous scene.

    I have tested to see that the IF statements containing the "transform.rotate()" functions are being reached, -they are.

    The gravity changes as it should.

    I have tried rebuilding my player object from scratch (and yes I checked object tags), to get the same results.
    ___________________________________________________________________
    Does anyone know of why this would occur, or what I could do to resolve the issue? Any insights would be appreciated. Alternatively, if you need more info, let me know.

    WAIT HOLD UP!
    In the middle of writing this post, with the same made from scratch player that did not work a few minutes ago - it worked. I changed literally nothing.

    Going in and pasting in my old, not made from scratch, player object reintroduces the error however. (Maybe I just need to wait a long time with it in the scene like the made from scratch object?)

    I still don't know why loading into a new scene causes this behavior, or why it is suddenly working on one player object with identical component values, but not the other. Well, any insights would still be appreciated. (The scene did not need baking, or at least didn't say so)
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Try using GetKeyDown and GetKeyUp instead of manually calculating the same data. It will carry through scenes a lot more accurately than GetKey will, which can give incorrect results during / after a scene transition.
     
  3. Jubcow

    Jubcow

    Joined:
    May 28, 2018
    Posts:
    4
    Thanks for the input, I have actually started using GetKeyDown and GetKeyUp in other scripts. However, in this script's case I know the GetKey functions are working across scenes as the gravity does change. After using debug.log statements to see what executions were being reached, I knew that it was reaching the rotate functions but simply wasn't rotating the object.
     
  4. Jubcow

    Jubcow

    Joined:
    May 28, 2018
    Posts:
    4
    SOLUTION: Turns out, the transform.rotate just doesn't work when you first try it in a new scene, but you can remedy this by saving the scene and loading up a different scene, then loading the previous scene back. The quickest way to do this is to simply create a new scene so there is nothing to load, then load your previous scene.