简体   繁体   中英

Unity C# script for move is not working

I'm learning OOP (I don't know how does it called, it is maybe wrong). And today I want to make a very basic game but my codes doesn't work. I want to move the object left and right when user press left arrow or right arrow. But it doesn't work. It's a 2D project. Here is the code, sorry for my bad English.

using UnityEngine;
using System.Collections;

public class p1 : MonoBehaviour {

    public float hiz = 0.1f;
    Vector2 v2Pozisyon;

    void Start () {

    }

    void Update () {
        yuru ();
    }

    void yuru (){
        if (Input.GetKey (KeyCode.LeftArrow)) {
            v2Pozisyon.x = this.transform.position.x - hiz;
        }
        if (Input.GetKey (KeyCode.RightArrow)) {
            v2Pozisyon.x = this.transform.position.x + hiz;
        }
    }
}

You have to assign new position to transform component.

void yuru (){
    v2Pozisyon = this.transform.position;
    if (Input.GetKey (KeyCode.LeftArrow)) {
        v2Pozisyon.x = this.transform.position.x - hiz;
    }
    if (Input.GetKey (KeyCode.RightArrow)) {
        v2Pozisyon.x = this.transform.position.x + hiz;
    }
    this.transform.position = v2Pozisyon;
}

You can also multiply hiz by Time.deltaTime . Right now your movement speed will be framerate dependant. Faster when your FPS will be higher, and slower with lower FPS.

If you want to move the object, you have to assign to the transform's position variable.

void yuru (){
    if (Input.GetKey (KeyCode.LeftArrow)) {
        v2Pozisyon.x = this.transform.position.x - hiz;
    }
    if (Input.GetKey (KeyCode.RightArrow)) {
        v2Pozisyon.x = this.transform.position.x + hiz;
    }
    this.transform.position = v2Pozisyon;
}

You should probably change transform.localPosition . The reason for changing localPosition instead of position is the fact that it's very clear that you still want to make it clear that you're moving the object in respect to it's parent coord system, which you probably are doing anyway.

Another thing you should do is to make your movement frame independent, to do this instead of setting a speed per frame you set a speed per second, or any other time unit you feel is appropriate. Then you need to find out how much time has passed since the last frame and use that as the delta in movement. You code will end up looking something like this:

using UnityEngine;
using System.Collections;

public class p1 : MonoBehaviour {

  public float hiz = 2.0f; //movement per second
  Vector2 v2Pozisyon;

  void Start () {

  }

  void Update () {
    yuru ();
  }

  void yuru (){
    if (Input.GetKey (KeyCode.LeftArrow)) {
        v2Pozisyon.x = this.transform.localPosition.x - hiz * Time.deltaTime;
    }
    if (Input.GetKey (KeyCode.RightArrow)) {
        v2Pozisyon.x = this.transform.localPosition.x + hiz * Time.deltaTime;
    }
  }
}

You can read some more about Time.deltaTime here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM