Search Unity

Direction of movement issue

Discussion in 'Scripting' started by mjclarke28, Jul 26, 2021.

  1. mjclarke28

    mjclarke28

    Joined:
    Jul 25, 2021
    Posts:
    7
    Hello,

    For some reason the direction my character is running is sideways rather than forward. I think I created it on the wrong axis in Blender and keyed all the animations to that. How do I fix this? Is there a fix in Unity?


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class Player : MonoBehaviour
    8. {
    9.     private CharacterController controller;
    10.     private Animator anim;
    11.     private Vector3 moveDirection = Vector3.zero;
    12.  
    13.     public float gravity = 20.0f;
    14.     public float jumpForce = 10.0f;
    15.     public float speed = 50.0f;
    16.     public float turnSpeed = 50f;
    17.  
    18.     public GameObject runFaceVar;
    19.     public GameObject jumpFaceVar;
    20.     public GameObject idleFaceVar;
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         controller = GetComponent<CharacterController>();
    26.         anim = gameObject.GetComponentInChildren<Animator>();
    27.  
    28.         runFaceVar = GameObject.Find("/Lego_DrWho_IdleAnimation_noscrewdriver/Armature/Root/Spine 1/Spine 2/Neck/Head/Geo_Face.Run");
    29.         jumpFaceVar = GameObject.Find("/Lego_DrWho_IdleAnimation_noscrewdriver/Armature/Root/Spine 1/Spine 2/Neck/Head/Geo_Face.Jump");
    30.         idleFaceVar = GameObject.Find("/Lego_DrWho_IdleAnimation_noscrewdriver/Armature/Root/Spine 1/Spine 2/Neck/Head/Geo_Face.Idle");
    31.  
    32.     }
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.       if (controller.isGrounded && Input.GetKey("up"))
    38.         {
    39.             anim.SetInteger("AnimPar", 1);
    40.             moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
    41.             float turn = Input.GetAxis("Horizontal");
    42.             //we want to turn around the y axis so we'll do that below
    43.             transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
    44.  
    45.             runFaceVar.SetActive(true);
    46.             idleFaceVar.SetActive(false);
    47.             jumpFaceVar.SetActive(false);
    48.  
    49.         }  
    50.         else if (controller.isGrounded)
    51.         {
    52.             anim.SetInteger("AnimPar", 0);
    53.             moveDirection = transform.forward * Input.GetAxis("Vertical") * 0;
    54.             float turn = Input.GetAxis("Horizontal");
    55.             //we want to turn around the y axis so we'll do that below
    56.             transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
    57.  
    58.             runFaceVar.SetActive(false);
    59.             idleFaceVar.SetActive(true);
    60.             jumpFaceVar.SetActive(false);
    61.         }
    62.  
    63.       if (Input.GetButton("Jump") && controller.isGrounded)
    64.         {
    65.             anim.SetInteger("AnimPar", 2);
    66.             moveDirection.y = jumpForce;
    67.  
    68.             runFaceVar.SetActive(false);
    69.             idleFaceVar.SetActive(false);
    70.             jumpFaceVar.SetActive(true);
    71.         }
    72.  
    73.         controller.Move(moveDirection * Time.deltaTime);
    74.         moveDirection.y -= gravity * Time.deltaTime;
    75.  
    76.     }
    77. }
    78.  
    79.  
     
  2. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    474
    Try adding Debug.Log statements throughout your code, it'll show you which parts are working and which aren't
     
  3. mjclarke28

    mjclarke28

    Joined:
    Jul 25, 2021
    Posts:
    7
    That's the thing, it appears to be working. The character is moving, it's just not moving the correct direction. I even changed it to transform.right (to get it on the red axis) and now it runs backwards. (still facing the camera but moving away from it).

    This was one of the messages that came up with the debug:
    (2.3, 0.0, -5.5)
    UnityEngine.Debug:Log(Object)
    Player:Update() (at Assets/Lego Scripts/Player.cs:39)