Search Unity

Camera Rotation about x-axis

Discussion in 'Scripting' started by brokenseraph, Dec 25, 2019.

  1. brokenseraph

    brokenseraph

    Joined:
    Dec 23, 2019
    Posts:
    1
    So I've imported the standard assets from unity, and they work great, but i want to make a feature where i can reverse gravity. I've got the gravity reversal made and functioning upon keypress, but a small error happens.

    I wrote some script that will rotate the RigidBodyFPSController 180 degrees about its own x axis, along with the child camera, but when I do, either the MainCamera, the RigidBodyFPSController, or both seem to automatically reorient themselves to their original positions. Below is my GravitySwitch script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Gravityswitch : MonoBehaviour
    6. {
    7.     //The strength of the gravity
    8.     public float gravState = -10.0F;
    9.    
    10.    
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        
    21.         if(Input.GetKeyDown("k")){
    22.             //reverse gravity
    23.             gravState *= -1.0F;
    24.             //rotate the RigidBodyFPSController (This is where the whole thing rotates and then reorients itself)
    25.             gameObject.transform.Rotate(180, 0, 0, Space.Self);
    26.            
    27.         }
    28.         //turn gravity the opposite direction
    29.         Physics.gravity = new Vector3(0, gravState, 0);
    30.        
    31.        
    32.     }
    33.    
    34. }
    35.  
    I figure it has something to do with either the FPS Controller script on the rigid body, or some settings i might be overlooking. I've dried unlocking rotation, modifying the Controller script, turning off the headbobbing script, and even just transforming the whole body itself on the inspector fields, but it just reorients itself immediately.

    If anyone is familiar with the standard asset unity scripts, and knows of any possible cause of this, any help would be appreciated, I am new to C# and even newer to Unity so forgive me if this sounds dumber than I am making it up to be.
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    This should work. Try disabling all other scripts and having only this one enabled, then test it out. After that start enabling script by script and testing between them to see which script causes the issue. It shouldn't be too hard to find.