Search Unity

Inverted Movement

Discussion in 'Scripting' started by ogifull9, Jan 19, 2020.

  1. ogifull9

    ogifull9

    Joined:
    Nov 1, 2019
    Posts:
    4
    Help
    My movement is normal going sideways but inverted going forwards and backwards
    Heres The Script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Igrac : MonoBehaviour
    5. {
    6.     public bool controllable = false;
    7.  
    8.     public float speed = 7.0f;
    9.     public float jumpSpeed = 6.0f;
    10.     public float gravity = 20.0f;
    11.  
    12.     private Vector3 moveDirection = Vector3.zero;
    13.     private CharacterController controller;
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         controller = GetComponent<CharacterController>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         if (controller.isGrounded && controllable)
    25.         {
    26.             moveDirection = new Vector3(Input.GetAxis("Vertical"), 0, Input.GetAxis("Horizontal"));
    27.             moveDirection = transform.TransformDirection(moveDirection);
    28.             moveDirection *= speed;
    29.  
    30.             if (Input.GetButton("Jump"))
    31.                 moveDirection.y = jumpSpeed;
    32.  
    33.         }
    34.         moveDirection.y -= gravity * Time.deltaTime;
    35.         controller.Move(moveDirection * Time.deltaTime);
    36.     }
    37. }
    38.  
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,038
    You are setting up movement as absolute values of the world: input on the joystic vertical axis is set to become the x component, horizontal joystick axis becomes translation along the z axis. This may work, but presumes some settimgs.

    First, Unity is usually set up that you are looking down the negative Z axis, so this can be confusing.

    But rather than setting up your movement using absolute values, which will break the moment you turn the player object, you can use the object's forward and rigt vectors:

    Code (CSharp):
    1. moveDirection = new Vector3(0, 0, 0);
    2. moveDirection += Input.GetAxis("Vertical") * transform.right +  Input.GetAxis("Horizontal") * transform.forward;
    3. moveDirection *= speed;
     
unityunity