[英]How do I add Momentum to my Character in Unity C#?
我是Unity 5的新手,并且对接口有基本的了解。 我从未编码过特别大的东西。 没有教程,我什么也做不了。 但是我不久前找到了一个适合我需求的教程。
基本上是,我想为玩家提供的点击框(多维数据集)增加动力。 当您按W键时,将加快速度,然后达到最终速度。 我将如何完成?
我已经尝试做一些事情来使其工作,但我想我已经很接近了(就像觉得这是一个工作块),但是我不知道该放在哪里或错误在说什么,所以我来到这个网站。
这是代码: https : //pastebin.com/dmSuJR7m
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterControls : MonoBehaviour {
public float speed = 3.0F;
// Use this for initialization
void Start () {
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown("w")) for > (deltaTime * 4) {
public float speed = 6.0F
}
float translation = Input.GetAxis("Vertical") * speed;
float strafe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
strafe *= Time.deltaTime;
transform.Translate(strafe, 0, translation);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
我不需要立即回答,但是看到此问题时您应该考虑回答。
试试这个代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterControls : MonoBehaviour {
public float base_speed = 3.0f;
public float max_speed = 6.0f;
private float current_speed;
// Use this for initialization
void Start () {
current_speed = base_speed;
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.W) && current_speed < max_speed) {
current_speed += 1 * Time.deltaTime;
} else if (!Input.GetKey(KeyCode.W) && current_speed > base_speed) {
current_speed -= 1 * Time.deltaTime;
}
float translation = Input.GetAxis("Vertical") * current_speed;
float strafe = Input.GetAxis("Horizontal") * current_speed;
translation *= Time.deltaTime;
strafe *= Time.deltaTime;
transform.Translate(strafe, 0, translation);
if (Input.GetKeyDown("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
此代码将采用base_speed(起始量,在这种情况下,我也将其用作常规量)并将其设置为current_speed,该值用于在不损失基本速度的情况下操纵当前速度(请保留对起始数量是,如果我们没有这个数量,我们将不知道何时停止加速,而不得不对其进行硬编码(这永远都不好)。
按住W而没有完全加速时,您将开始加速,如果不按W并加速,则开始减速直到达到基本速度。
由于以下原因,您的代码甚至无法编译
if (Input.GetKeyDown("w")) for > (deltaTime * 4)
一个适当的for循环如下所示:
for (int i = 0; i < 10; i++) { }
并且您不能在if语句中使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.