Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# unity新手,为什么这个脚本不移动对象?(统一3D)_C#_Unity3d - Fatal编程技术网

C# unity新手,为什么这个脚本不移动对象?(统一3D)

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

我不熟悉c#和unity,我正在尝试制作一个3d游戏。这个密码是用来让剑来回移动的,当我按下按钮时,unity确认了,但剑不动。有人知道它为什么这样做吗

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(延迟);。谢谢修复了我的答案,如果它有帮助,请将其标记为正确的解决方案(灰色复选标记),提前感谢。