Skip to content

Instantly share code, notes, and snippets.

@coding-youtuber
Created March 29, 2021 11:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coding-youtuber/5784f8f8a95767f41d6e1d02881a6b51 to your computer and use it in GitHub Desktop.
Save coding-youtuber/5784f8f8a95767f41d6e1d02881a6b51 to your computer and use it in GitHub Desktop.
Unityで頭文字DのAE86(ハチロク)を動かすスクリプト
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