Search Unity

PlayerController acting weird.

Discussion in 'Scripting' started by astraalii, May 12, 2019.

  1. astraalii

    astraalii

    Joined:
    Jan 24, 2017
    Posts:
    4
    Hey! Im making simple multiplayer test here.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.    
    6.     public float Speed = 10;
    7.    
    8.     // Dirty flag for checking if movement was made or not
    9.     public bool MovementDirty {get; set;}
    10.  
    11.     void Start() {
    12.         MovementDirty = false;
    13.     }
    14.    
    15.     void Update () {
    16.         // left right
    17.         float translation = Input.GetAxis("Horizontal");
    18.         if (translation != 0) {
    19.             this.transform.Translate(translation * Time.deltaTime * Speed, 0, 0);
    20.             MovementDirty = true;
    21.         }
    22.    
    23.         // up down
    24.          float translation2 = Input.GetAxis("Vertical");
    25.         if (translation2 != 0) {
    26.             this.transform.Translate(0, translation * Time.deltaTime * Speed,0);
    27.             MovementDirty = true;
    28.         }
    29.     }
    30. }
    Left and right works fine.
    Diagonal movement works fine too.
    Up and down and sprite just stays in place no movement?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    2,147
    Your second Translate() uses the wrong translation float.
     
  3. astraalii

    astraalii

    Joined:
    Jan 24, 2017
    Posts:
    4
    thanks !