赞
踩
Unity学习笔记–赛车的控制代码
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CarContraller : MonoBehaviour { private MeshRenderer[] wheelMesh;//存放轮胎的物理属性 private WheelCollider[] wheel;//存放轮胎测WheelColloder private float maxAngle = 30;//最大转向角度 private float maxToque = 220;//单个车轮最大行驶扭矩 private float h; private float v; // Use this for initialization void Start () { wheelMesh = transform.GetChild(5).GetComponentsInChildren<MeshRenderer>();//具有meshrenderer组件的物体是Car的第六个子物体下,所以是Getchild(5); wheel = transform.GetChild(1).GetComponentsInChildren<WheelCollider>();//同理,具有WheelCollider的子物体是Car的第二个子物体下 } // Update is called once per frame void Update () { h = Input.GetAxis("Horizontal");//纵向控制 v = Input.GetAxis("Vertical");//横向控制 if (0 == Mathf.Abs(h) && 0 == Mathf.Abs(v)) return; Move(); } void Move() { for (int i = 0; i < 2; i++)//前轮转向 { wheel[i].steerAngle = h * maxToque; } foreach (var o in wheel)//四驱的动力 { o.motorTorque = maxToque * v; } for (int i = 0; i < 4;i++ )//给四个车轮设置其自身的旋转方式 { wheelMesh[i].transform.localRotation = Quaternion.Euler(wheel[i].rpm*360/60,wheel[i].steerAngle,0); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。