Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

4 way player movement not recognized

Discussion in 'Scripting' started by Tengs-chan, Jan 17, 2021.

  1. Tengs-chan

    Tengs-chan

    Joined:
    Jan 16, 2021
    Posts:
    1
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class playerController : MonoBehaviour
    4. {
    5.     public float speed;
    6.     private Animator animator;
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         animator = this.GetComponent<Animator>();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.        Vector2 moveDirection = Vector2.Zero
    18.      
    19.         var vertical = Input.GetAxisRaw("Vertical");
    20.         var horizontal = Input.GetAxisRaw("Horizontal");
    21.  
    22.         if (horizontal > 0)
    23.         {
    24.             moveDirection.x = 1;
    25.             animator.SetInteger("Direction", 2);
    26.         }
    27.         else if (horizontal < 0)
    28.         {
    29.             moveDirection.x = -1;
    30.             animator.SetInteger("Direction", 0);
    31.         }
    32.         else if (vertical > 0)
    33.         {
    34.             moveDirection.y = 1;
    35.             animator.SetInteger("Direction", 1);
    36.         }
    37.         else if (vertical < 0)
    38.         {
    39.             moveDirection.y = -1;
    40.             animator.SetInteger("Direction", 3);
    41.         }
    42.         transform.Translate(moveDirection * speed* Time.deltaTime, Space.World);
    43.     }
    44. }
    45.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,832
    A request for debugging help should include:
    • What you want to have happen
    • Your strategy for making that happen
    • What actually happened instead
     
    Vryken and PraetorBlue like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    *points finger*
    The problem is over there.
     
  5. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Vector2 moveDirection = Vector2.Zero
    is missing a ;