zoukankan      html  css  js  c++  java
  • 关于站在移动物上的问题

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

    默认情况:

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

    但刨根问底想一下,在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游戏的飞机或者火车顶上的移动比较麻烦,载具的驱动方式很复杂,还是改父物体比较好

  • 相关阅读:
    python基础7
    python基础7
    十大经典预测算法(一)----线性回归
    RNN-循环神经网络
    CNN之经典卷积网络框架原理
    卷积神经网络CNN
    决策树的生成
    欠拟合、过拟合及解决方法
    决策树
    KD树
  • 原文地址:https://www.cnblogs.com/hont/p/5796895.html
Copyright © 2011-2022 走看看