首页 > 解决方案 > 尝试在统一 2d 中移动对象

问题描述

物体没有沿着斜坡向下移动

如何在 2d 中统一移动对象这是我的代码:

public class move : MonoBehaviour
{
    public float moveSpeed = 3f;
    Rigidbody2D rig;
    float yat ;            
    void Start()
    {
        rig = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        float xat = Input.GetAxis("Horizontal");
        yat = rig.velocity.y;
        rig.velocity = new Vector2(xat*moveSpeed,yat);
    }
 }

标签: unity3d

解决方案


我很抱歉,我不明白你想做什么?但是,如果您只是移动对象,您可以尝试“Transfrom.Translate”方法。

顺便说一句,使用速度方法在您自己的版本中尝试此代码:

float speed = 0.5f;

...


void Update () {

      float inputHorizontal = Input.GetAxisRaw("Horizontal");
      player.velocity = Vector2.right * inputHorizontal * speed;
      
 }

Imo 这应该可以,如果您有任何其他疑问,请告诉我。


推荐阅读