当前位置:   article > 正文

Unity学习笔记--赛车的控制代码_function f_set_tuning_lxy_spec_by

function f_set_tuning_lxy_spec_by

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);
        }
      
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/97991
推荐阅读
相关标签
  

闽ICP备14008679号