首页 > 解决方案 > 如何让角色向左或向右跳到固定位置?

问题描述

我有一个角色,如果我按 A 或 D,它会向左或向右跳,但问题是它不会一直跳到同一个位置。

如果我按 D 它会向右跳到某个位置,然后重新启动场景并再次按 D,它会向右跳但最终在不同的位置,即使我已经给它提供了 x 和 y 值。

下面是我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{


  public float jumpSpeed;
  public int jumpHeight;
  public float Xincrement;
  public float Zdecrement;

  void Awake()
  {
    rb = GetComponent<Rigidbody>();
  }

  // Start is called before the first frame update
  void Start()
  {

  }

  // Update is called once per frame
  void Update()
  {
    if (Input.GetKeyDown(KeyCode.D))
    {
      GetComponent<Rigidbody>().velocity = new Vector3(Xincrement, jumpHeight, 0) * jumpSpeed * Time.deltaTime;
    }
    else if (Input.GetKeyDown(KeyCode.A))
    {
      GetComponent<Rigidbody>().velocity = new Vector3(0, jumpHeight, Zdecrement) * jumpSpeed * Time.deltaTime;
    }
  }
}

标签: c#unity3dgame-physics

解决方案


推荐阅读