赞
踩
public void RotWheel(bool isForward)
{
transform.Rotate(Vector3.up * 360 * Time.deltaTime * (isForward ? -1 : 1));
}
if (Input.GetKey(KeyCode.W))
{
RotWheel(true);
}
if (Input.GetKey(KeyCode.S))
{
RotWheel(false);
}
为什么是Vector3.up:刚开始创建Cylinder时,Vector3.up指的方向是刚开始时的y轴,然后作为车轮以z轴旋转了90度,但是Cylinder物体的y轴也会随着转动,所以如果车轮要向前转的话,不能看现在的车轮方向轴,所以写Vector3.up
360 * Time.deltaTime :每秒转360度
(isForward ? -1 : 1)正数向前转,负数向后转,按下w,true,向前转,但是因为Cylinder旋转过,所以要注意
回到unity运行,轮子可以转动了,但是发现轮子"变异"了
这是因为轮子作为CarBody的子物体,子物体会受到父物体的位置变化形状变化影响所以我们要创建一个空物体名为Control,CarBody和四个轮子都挂在Control下,这样就正常了
public Transform LF;
public Transform RF;
public void FowardWheelLF(Transform Wheel, bool isRight)
{
Wheel.Rotate(Vector3.up * 30 * Time.deltaTime * (isRight ? 1 : -1),Space.World);
}
第二个参数Space.World说明Vector3.up是沿着世界坐标的up方向转动车轮
if (Input.GetKey(KeyCode.A))
{
FowardWheelLF(LF, false);
FowardWheelLF(RF, false);
}
if (Input.GetKey(KeyCode.D))
{
FowardWheelLF(LF, true);
FowardWheelLF(RF, true);
}
if (Input.GetKey(KeyCode.W))
{
transform.Translate(Vector3.forward * 5 * Time.deltaTime);//每秒向前走5m
}
if (Input.GetKey(KeyCode.S))
{
transform.Translate(Vector3.forward * -1 * 5 * Time.deltaTime);
}
transform.Rotate(Vector3.up * -1 * Time.deltaTime * 60);
transform.Rotate(Vector3.up * Time.deltaTime * 60);
if (Input.GetKey(KeyCode.W))
{
transform.Rotate(Vector3.up * -1 * Time.deltaTime * 60);
}
if (Input.GetKey(KeyCode.W))
{
transform.Rotate(Vector3.up * Time.deltaTime * 60);
}
if (RF.localEulerAngles.y >= 60 )
{
FowardWheelLF(LF, false);
FowardWheelLF(RF, false);
}
if (LF.localEulerAngles.y<=120)
{
FowardWheelLF(LF, true);
FowardWheelLF(RF, true);
}
if (Input.GetKey(KeyCode.W))
{
transform.Rotate(Vector3.up * -1 * Time.deltaTime * 60);
}
else if (Input.GetKey(KeyCode.S))
{
transform.Rotate(Vector3.up * 1 * Time.deltaTime * 60);
}
if (Input.GetKey(KeyCode.W))
{
transform.Rotate(Vector3.up * Time.deltaTime * 60);
}
else if (Input.GetKey(KeyCode.S))
{
transform.Rotate(Vector3.up * -1 * Time.deltaTime * 60);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.A))
{
IsComeback = true;
}
if (IsComeback)
{
LF.localEulerAngles = new Vector3(0, Mathf.SmoothDamp(LF.localEulerAngles.y, 90, ref num, 0.5f), 0);
RF.localEulerAngles = new Vector3(0, Mathf.SmoothDamp(LF.localEulerAngles.y, 90, ref num, 0.5f), 0);
if (LF.localEulerAngles.y==90)
{
IsComeback = false;
}
}
float num;
bool IsComeback = false;
最后,差不多是一个完美的汽车了,可以自己给材质球上颜色,或者直接在asset story里下载免费的跑车,就很拉风.
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。