Created
March 29, 2021 11:26
-
-
Save coding-youtuber/5784f8f8a95767f41d6e1d02881a6b51 to your computer and use it in GitHub Desktop.
Unityで頭文字DのAE86(ハチロク)を動かすスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[System.Serializable] | |
public class MyAxleInfo { | |
public WheelCollider leftWheel; | |
public WheelCollider rightWheel; | |
public GameObject visualLeftWheel; | |
public GameObject visualRightWheel; | |
public bool motor; | |
public bool steering; | |
} | |
public class AECarController : MonoBehaviour | |
{ | |
public List<MyAxleInfo> axleInfos; | |
public float maxMotorTorque; | |
public float maxSteeringAngle; | |
public void ApplyLocalPositionToVisuals(GameObject gm, WheelCollider collider) | |
{ | |
Transform visualWheel = gm.transform; | |
Vector3 position; | |
Quaternion rotation; | |
collider.GetWorldPose(out position, out rotation); | |
visualWheel.transform.position = position; | |
visualWheel.transform.rotation = rotation; | |
// visualWheel.transform.rotation = rotation * Quaternion.Euler(0f, 0f, 90f); | |
} | |
public void FixedUpdate() | |
{ | |
float motor = maxMotorTorque * Input.GetAxis("Vertical"); | |
float steering = maxSteeringAngle * Input.GetAxis("Horizontal"); | |
foreach(MyAxleInfo axleInfo in axleInfos) { | |
if(axleInfo.steering) { | |
axleInfo.leftWheel.steerAngle = steering; | |
axleInfo.rightWheel.steerAngle = steering; | |
} | |
if(axleInfo.motor) { | |
axleInfo.leftWheel.motorTorque = motor; | |
axleInfo.rightWheel.motorTorque = motor; | |
} | |
ApplyLocalPositionToVisuals(axleInfo.visualLeftWheel, axleInfo.leftWheel); | |
ApplyLocalPositionToVisuals(axleInfo.visualRightWheel, axleInfo.rightWheel); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment