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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

movement bug

Discussion in '2D' started by 787gabriel777, Feb 1, 2017.

  1. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
    Hello, my char only goes foward, i think there is a bug on script, so, there are my script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class mov_char : MonoBehaviour {
    5.  
    6.  
    7.     public float speed;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.         speed = 2.0f;
    14.    
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update(){
    19.  
    20.         mov ();
    21.  
    22. }
    23.  
    24.     void mov(){
    25.  
    26.         if (Input.GetAxisRaw("Horizontal") > 0) {
    27.    
    28.             transform.Translate (Vector2.right * speed * Time.deltaTime);
    29.             transform.eulerAngles = new Vector2 (0, 0);
    30.         }
    31.         if (Input.GetAxisRaw("Horizontal") < 0) {
    32.  
    33.  
    34.             transform.Translate (-Vector2.right * speed * Time.deltaTime);
    35.             transform.eulerAngles = new Vector2 (0, 180);
    36.  
    37.         }
    38.  
    39.     }
    40. }
    Can anybody help me? whats wrong?
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
  3. 787gabriel777

    787gabriel777

    Joined:
    Jul 20, 2015
    Posts:
    65
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You can use "transform.right" instead of Vector2.right to get the object's 'forward' direction regardless of rotation.