我有一个字符,如果按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;
}
}
}