关于站在移动物上的问题

游戏中经常遇到在移动物上战斗的情况,而直接拖拽的话就会下落了

默认情况:

比较简单的办法是直接设置父物体

但刨根问底想一下,在Unity里拖拽,使用transform移动,其实都不是基于物理的移动

只有改变速率才是,后来用恒力去测了一下,果然可以带动站在上面的物体

所以,我尝试了一下更简单的方式

脚本:

using UnityEngine;
using System.Collections;

public class PhysicsTest : MonoBehaviour
{
    const int MAX_MASS = 10000;

    void Awake()
    {
        GetComponent<Rigidbody>().mass = MAX_MASS;
        GetComponent<Collider>().material = new PhysicMaterial();
        GetComponent<Collider>().material.staticFriction = 99999;
        GetComponent<Collider>().material.dynamicFriction = 99999;
    }

    IEnumerator Start()
    {
        while (true)
        {
            for (int i = 0; i < 100; i++)
            {
                GetComponent<Rigidbody>().velocity = Vector3.right * Time.deltaTime * 1000f;
                yield return new WaitForFixedUpdate();
            }

            yield return new WaitForSeconds(1);

            for (int i = 0; i < 100; i++)
            {
                GetComponent<Rigidbody>().velocity = -Vector3.right * Time.deltaTime * 1000f;
                yield return new WaitForFixedUpdate();
            }

            yield return new WaitForSeconds(1);
        }
    }
}
View Code

这样用在横版游戏中很便捷,可以避开不少bug

但是对于3D游戏的飞机或者火车顶上的移动比较麻烦,载具的驱动方式很复杂,还是改父物体比较好

原文地址:https://www.cnblogs.com/hont/p/5796895.html