Search Unity

Gravity Seems to be pulling character backwards

Discussion in 'Scripting' started by LugburzRD, Jun 21, 2018.

  1. LugburzRD

    LugburzRD

    Joined:
    Jun 21, 2018
    Posts:
    3
    Well basically I did two scripts for looking with mouse and moving with keyboard and whenever I run the script the character gets moved back along the z axis. However when I set gravity to zero it doesnt. When gravity is a positive number character is moved forward. What is causing this and how can i fix it?

    This is the mouselook script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour {
    6.     public enum RotationAxes{
    7.         MouseXandY= 0,
    8.         MouseX= 1,
    9.         MouseY= 2
    10.    
    11.     }
    12.     public RotationAxes axes = RotationAxes.MouseXandY;
    13.  
    14.     public float sensitiviyHor = 9.0f;
    15.     public float sensitiviyVert = 9.0f;
    16.  
    17.     public float minimumVert = -45.0f;
    18.     public float maximumVert = 45.0f;
    19.  
    20.     private float _rotationX = 0;
    21.  
    22.     void Start() {
    23.         Rigidbody body = GetComponent<Rigidbody>();
    24.         if (body != null)
    25.             body.freezeRotation = true;
    26.        
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update () {
    31.         if (axes == RotationAxes.MouseX) {
    32.             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitiviyHor, 0);
    33.             // horizontal rotation here
    34.         }
    35.         else if (axes == RotationAxes.MouseY) {
    36.             _rotationX -= Input.GetAxis("Mouse Y") * sensitiviyVert;
    37.             _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
    38.  
    39.             float rotationY = transform.localEulerAngles.y;
    40.  
    41.             transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
    42.         }
    43.       else {
    44.             _rotationX -= Input.GetAxis("Mouse Y") * sensitiviyVert;
    45.             _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);
    46.  
    47.             float delta = Input.GetAxis("Mouse X") * sensitiviyHor;
    48.             float rotationY = transform.localEulerAngles.y + delta;
    49.  
    50.             transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);
    51.  
    52.         }
    53.     }
    54. }
    55.  
    And this is the keyboard script to move
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(CharacterController))]
    6. [AddComponentMenu("Control Script/Keyboard")]
    7. public class Keyboard : MonoBehaviour {
    8.     public float speed = 6.0f;
    9.     public float gravity = -9.8f;
    10.  
    11.     private CharacterController _charController;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         _charController = GetComponent<CharacterController>();
    16.        
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         float deltaX = Input.GetAxis("Horizontal") * speed;
    22.         float deltaZ = Input.GetAxis("Vertical") * speed;
    23.         Vector3 movement = new Vector3(deltaX, 0, deltaZ);
    24.         movement = Vector3.ClampMagnitude(movement, speed);
    25.  
    26.         movement.y = gravity;
    27.  
    28.         movement *= Time.deltaTime;
    29.         movement = transform.TransformDirection(movement);
    30.         _charController.Move(movement);
    31.        
    32.     }
    33. }
    34.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I assume your Physics.gravity is at its default, pointing straight down.

    Therefore it is probably physics engine "creep" because you are driving the transform.localEulerAngles directly. Any direct access to the transform when you have a Rigidbody in place will effectively bypass the physics engine, so instead you want to make all your actions against the instance of the rigidbody connected, such as MovePosition and MoveRotation, which will properly percolate through the physics engine.
     
  3. LugburzRD

    LugburzRD

    Joined:
    Jun 21, 2018
    Posts:
    3
    sorry but what would i need to change exactly?