c# - 如何让角色向左或向右跳到固定位置?
问题描述
我有一个角色,如果我按 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;
}
}
}
解决方案
推荐阅读
- javascript - LeetCode 768:为 Max Chunks 构建一个解决方案的变体以进行排序
- python-imaging-library - 重现 AttnGAN 时 utils.py 出现一些问题
- oauth-2.0 - 允许用户使用 Google 帐户登录我的网站,无需 3d 方 cookie
- docker - 如何让 HWI OAuth Bundle 在反向代理后面的容器化应用程序中表现良好?
- python - 如何使用 Matplotlib 绘制分布式点图?
- nestjs - 反射注入器,允许在nestjs中注入构造函数参数之外的依赖项
- mysql - 我可以在 MySQL Workbench 的模式面板中隐藏模式吗
- c# - List.Reverse() 在 C# 中没有任何效果
- python - bokeh: 检索 BasicTicker 的轴刻度间隔
- oauth - 在使用 Google API 时,credentials.JSON 有什么作用?