Search Unity

Direction of movement issue

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

  1. mjclarke28

    mjclarke28

    Joined:
    Jul 25, 2021
    Posts:
    7
    Hello I've been following a tutorial on Udemy to create a lego character that moves across the scene and jumps, etc.

    Anyways, I've imported the character and obviously I did the blend file associated with the wrong axis or something because the character is backwards. Nevertheless, I went through the tutorial and everything seemed okay until I tested the movement and it seems to move sideways rather than forwards. How can I fix this?

    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.  
     
  2. IllTemperedTunas

    IllTemperedTunas

    Joined:
    Aug 31, 2012
    Posts:
    782
    try changing tranform.forward to transform.right, or simply rotate the root bone 90 degree.
     
    Last edited: Jul 26, 2021