【问题标题】:Error when make the objects both MoveLeft and Spinning in Unity在 Unity 中使对象同时向左移动和旋转时出错
【发布时间】:2022-12-18 16:35:52
【问题描述】:

我正在尝试制作对象旋转时向左飞.

这里是向左移动脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MoveLeft : MonoBehaviour
{
    private float moveLeftSpeed = 10;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.left * Time.deltaTime * moveLeftSpeed);
    }
    
}

还有自旋对象脚本:

using System.Collections.Generic;
using UnityEngine;

public class SpinObjectsX : MonoBehaviour
{
    public float spinSpeed = 50;

    // Update is called once per frame
    void Update()
    {
        transform.Rotate(new Vector3(0, Time.deltaTime * spinSpeed, 0));
    }
}

我希望物体的运动看起来像这样,它只是向左移动并自旋。

但是当我同时使用这两个脚本时,对象的移动非常奇怪,它仍然在自转但不是向左移动,而是绕着某个东西旋转......

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    TranslateRotate 都默认工作在局部空间根据对象,除非您明确传入 Space.World 作为附加的最后一个参数。

    因此,在围绕 Y 轴旋转对象后,它的局部 left 矢量现在也旋转并指向其他地方。

    =>当你做局部空间向物体左侧平移它实际上并没有在世界中向左移动。

    为了在您要使用的绝对世界空间中向左移动

    transform.Translate(Vector3.left * Time.deltaTime * moveLeftSpeed, Space.World);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多