统一设置transform.forward原因transform.position更改

时间:2018-11-11 10:25:27

标签: c# unity3d

我在水平移动中设置了transform.forward完美,但是当我在垂直移动中使用它时,请参阅我的gif,transform.forward = newForward会导致transform.position更改,如何使字符向前移动,但不要更改transform.position

enter image description here

这是我的完整剧本:

public class TestVerticalMoveForward : MonoBehaviour
{
    private GameObject from;

    private GameObject to;
    private GameObject target;

    // Use this for initialization
    void Start()
    {
        from = GameObject.Find("From");
        to = GameObject.Find("To");
        target = from;
    }

    private float speed = 10;

    // Update is called once per frame
    void Update()
    {
        var offset = target.transform.position - transform.position;
        var forward = offset.normalized;
        var offset2 = speed * Time.deltaTime * forward;
        if (offset.magnitude <= offset2.magnitude)
        {
            transform.position = target.transform.position;
            if (target == to)
            {
                target = from;
            }
            else
            {
                target = to;
            }
        }
        else
        {
            transform.position += offset2;
        }

        if (offset != Vector3.zero)
        {
            transform.forward = offset;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我找到了解决方法和原因,

解决方案:

这是固定结果

enter image description here

我使用Quaternion.LookRotation而不是transform.forward=

#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using Script.Ro.Ext;
using UnityEngine;

public class TestVerticalMoveForward : MonoBehaviourExt
{
    private GameObject from;

    private GameObject to;

    private GameObject target;
//    private GameObject center;

    // Use this for initialization
    void Start()
    {
        from = GameObject.Find("From");
        to = GameObject.Find("To");
//        center = GameObject.Find("Character/Center");
        target = from;
    }

    private float speed = 10;

    // Update is called once per frame
    void Update()
    {
        Watch("center.transform.position", transform.position);
        var offset = target.transform.position - transform.position;

        var forward = offset.normalized;
        var offset2 = speed * Time.deltaTime * forward;
        if (offset.magnitude <= offset2.magnitude)
        {
            transform.position += offset;
            if (target == to)
            {
                target = from;
            }
            else
            {
                target = to;
            }
        }
        else
        {
            transform.position += offset2;
        }

        if (offset != Vector3.zero)
        {
           var upAxis =  transform.rotation * Vector3.up;
            transform.rotation =
                Quaternion.LookRotation(target.transform.position - transform.position, upAxis);
        }
    }
}
#endif

原因

团结有

Quaternion LookRotation(Vector3 forward, Vector3 upwards = Vector3.up);

我猜想transform.forward =会调用内部LookRotation代码并始终设置upwards = Vector3.up(y轴),在水平移动中它可以完美地工作,但是在垂直移动中它会表现出意想不到的

因此,假设对象具有中心上轴,例如,人形对象的上轴是身体中心到头部的中心轴,以确保垂直移动像水平移动一样执行,LookRotation的向上应该始终是中心向上轴,请参阅我上面的代码,该中心轴应为:

       var upAxis =  transform.rotation * Vector3.up ;