C# unity新手,为什么这个脚本不移动对象?(统一3D)
我不熟悉c#和unity,我正在尝试制作一个3d游戏。这个密码是用来让剑来回移动的,当我按下按钮时,unity确认了,但剑不动。有人知道它为什么这样做吗C# unity新手,为什么这个脚本不移动对象?(统一3D),c#,unity3d,C#,Unity3d,我不熟悉c#和unity,我正在尝试制作一个3d游戏。这个密码是用来让剑来回移动的,当我按下按钮时,unity确认了,但剑不动。有人知道它为什么这样做吗 using System.Collections; using System.Collections.Generic; using UnityEngine; public class slash : MonoBehaviour { // Start is called before the first frame updat
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class slash : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
bool slas = false;
if (Input.GetKeyDown(KeyCode.DownArrow))
{
slas = true;
}
if (slas = true)
{
transform.Rotate(0, 30, 0);
transform.Rotate(0,-30,0);
}
}
}
你现在在代码中所做的是将你的剑移动30度,然后再将它向后移动30度
transform.Rotate(0, 30, 0);
transform.Rotate(0,-30,0);
因为它发生得太快了,你看不到它在动
您需要做的是在这两个动作之间添加一个延迟,以实际看到它移动。可以使用动画,然后在代码中播放该动画,也可以使用IEnumerator延迟第二个动作
使用IEnumerator进行编码:
float delay = 0.5f;
void Update() {
if (Input.GetKeyDown(KeyCode.DownArrow)) {
// Starting IEnumerator
StartCoroutine(SwingSword());
}
}
IEnumerator SwingSword() {
// Swing forwards
transform.Rotate(0, 30, 0);
// Delay
yield return new WaitForSeconds(delay);
// Swing backwards
transform.Rotate(0,-30,0);
}
你现在在代码中所做的是将你的剑移动30度,然后再将它向后移动30度
transform.Rotate(0, 30, 0);
transform.Rotate(0,-30,0);
因为它发生得太快了,你看不到它在动
您需要做的是在这两个动作之间添加一个延迟,以实际看到它移动。可以使用动画,然后在代码中播放该动画,也可以使用IEnumerator延迟第二个动作
使用IEnumerator进行编码:
float delay = 0.5f;
void Update() {
if (Input.GetKeyDown(KeyCode.DownArrow)) {
// Starting IEnumerator
StartCoroutine(SwingSword());
}
}
IEnumerator SwingSword() {
// Swing forwards
transform.Rotate(0, 30, 0);
// Delay
yield return new WaitForSeconds(delay);
// Swing backwards
transform.Rotate(0,-30,0);
}
您可能需要
if(sla==true)
,因为您现在拥有的总是true,即使键没有按下。如果你真的想看到旋转随着时间的推移,你需要做更多的工作。如果您在此处搜索,您可以找到有关以各种方式执行此操作的信息。您可能需要If(sla==true)
,因为即使未按下键,您现在拥有的也始终为true。如果你真的想看到旋转随着时间的推移,你需要做更多的工作。如果您在这里搜索,您可以找到有关以各种方式执行此操作的信息。我收到一个错误-错误CS1955:非可开票成员“WaitForSeconds”不能像方法一样使用。它正常工作了!收益返回WaitForSeconds(延迟);应该返回新的WaitForSeconds(延迟);。谢谢修复了我的答案,如果它有帮助,请将其标记为正确的解决方案(灰色复选标记),提前感谢。我收到一个错误-错误CS1955:非可开票成员“WaitForSeconds”不能像方法一样使用。使其正常工作!收益返回WaitForSeconds(延迟);应该返回新的WaitForSeconds(延迟);。谢谢修复了我的答案,如果它有帮助,请将其标记为正确的解决方案(灰色复选标记),提前感谢。