C# 为什么带有脚本的对象在被引用对象完成移动后开始向右移动? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类视频设置:单一行为 { 公共游戏对象视频音频立方体; 公共浮子速度=5f; 公共浮动距离; private bool keepMoving=true; 私人Vector3 startPos; 私有向量3 endPos; private bool moveBack=false; 私有void Start() { startPos=VideoAudioCube.transform.position; } 私有void OnMouseDown() { if(keepMoving==true) { if(transform.getComponentChildren().text==“视频”) { start例程(MoveVideo()); } keepMoving=false; } } 私有IEnumerator MoveVideo() { endPos=VideoAudioCube.transform.position-VideoAudioCube.transform.right*距离移动; if(向后移动==false) { while(VideoAudioCube.transform.position!=endPos) { VideoAudioCube.transform.position= 矢量3.移动方向(VideoAudioCube.transform.position、endPos、Time.deltaTime*速度); 收益返回空; } } keepMoving=true; moveBack=true; } }

C# 为什么带有脚本的对象在被引用对象完成移动后开始向右移动? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 公共类视频设置:单一行为 { 公共游戏对象视频音频立方体; 公共浮子速度=5f; 公共浮动距离; private bool keepMoving=true; 私人Vector3 startPos; 私有向量3 endPos; private bool moveBack=false; 私有void Start() { startPos=VideoAudioCube.transform.position; } 私有void OnMouseDown() { if(keepMoving==true) { if(transform.getComponentChildren().text==“视频”) { start例程(MoveVideo()); } keepMoving=false; } } 私有IEnumerator MoveVideo() { endPos=VideoAudioCube.transform.position-VideoAudioCube.transform.right*距离移动; if(向后移动==false) { while(VideoAudioCube.transform.position!=endPos) { VideoAudioCube.transform.position= 矢量3.移动方向(VideoAudioCube.transform.position、endPos、Time.deltaTime*速度); 收益返回空; } } keepMoving=true; moveBack=true; } },c#,unity3d,C#,Unity3d,在屏幕截图中,脚本被附加到新的游戏立方体上。脚本正在移动空的游戏对象父对象: 摄像机正对着按钮: 这是在游戏对象由于某种原因移动之后,我看到按钮下的所有对象开始向右移动: 看起来整个Buttons对象都在向右移动,但不是只有Buttons下的子对象在移动: 如果我不使用脚本,按钮下的所有子对象都不会移动。我不知道是什么让对象在游戏对象完成移动后开始移动。我也看不到你发布的脚本中有任何原因。。也许它发生在某个地方else@derHugo我会再次检查。@derHugo他说,当他不使用脚本时,按钮

在屏幕截图中,脚本被附加到新的游戏立方体上。脚本正在移动空的游戏对象父对象:

摄像机正对着按钮:

这是在游戏对象由于某种原因移动之后,我看到按钮下的所有对象开始向右移动:

看起来整个Buttons对象都在向右移动,但不是只有Buttons下的子对象在移动:


如果我不使用脚本,按钮下的所有子对象都不会移动。我不知道是什么让对象在游戏对象完成移动后开始移动。

我也看不到你发布的脚本中有任何原因。。也许它发生在某个地方else@derHugo我会再次检查。@derHugo他说,当他不使用脚本时,按钮不会移动。他可能弄错了,或者我可能遗漏了什么,但这意味着剧本是原因。@DubDub我知道。。但是,如果你看代码,唯一移动任何东西的行是
VideoAudioCube.transform.position=…
。。那么,为什么还要用这个脚本移动其他内容呢?除了OP没有在这里发布整个脚本,还有更多的事情发生在这里或者@DanielLip是否可能这个脚本在你的场景中多次出现,而你只是在
VideoAudioCube
中引用了错误的内容?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class VideoSettings : MonoBehaviour
{
    public GameObject VideoAudioCube;
    public float speed = 5f;
    public float distanceToMove;

    private bool keepMoving = true;
    private Vector3 startPos;
    private Vector3 endPos;
    private bool moveBack = false;

    private void Start()
    {
        startPos = VideoAudioCube.transform.position;
    }

    private void OnMouseDown()
    {
        if (keepMoving == true)
        { 
            if (transform.GetComponentInChildren<TextMesh>().text == "Video")
            {
                StartCoroutine(MoveVideo());
            }

            keepMoving = false;
        }
    }

    private IEnumerator MoveVideo()
    {
        endPos = VideoAudioCube.transform.position - VideoAudioCube.transform.right * distanceToMove;

        if (moveBack == false)
        {
            while (VideoAudioCube.transform.position != endPos)
            {
                VideoAudioCube.transform.position =
                  Vector3.MoveTowards(VideoAudioCube.transform.position, endPos, Time.deltaTime * speed);
                yield return null;
            }
        }

        keepMoving = true;
        moveBack = true;
    }
}