Created
July 29, 2014 20:21
-
-
Save takashiski/4617dbee50caba0a2dfc to your computer and use it in GitHub Desktop.
Cubeの子にWheelColliderを自動生成して走らせる的な
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 UnityEngine; | |
using System.Collections; | |
public class Wheel : MonoBehaviour { | |
public GameObject root; | |
public float wheelRadius; | |
public float steerAngle; | |
public float brakeTorque; | |
public float motorTorque; | |
private WheelCollider FR; | |
private WheelCollider FL; | |
private WheelCollider RR; | |
private WheelCollider RL; | |
// Use this for initialization | |
void Awake() { | |
GameObject wheel = new GameObject("Wheel"); | |
wheel.transform.parent = root.transform; | |
wheel.transform.position = root.transform.position; | |
Vector3 scale = wheel.transform.lossyScale; | |
Vector3 ofsX = wheel.transform.right * scale.x/2; | |
Vector3 ofsY = wheel.transform.up * scale.y/2; | |
Vector3 ofsZ = wheel.transform.forward * scale.z/2; | |
Vector3 position = wheel.transform.position; | |
FR = MakeWheel("FR",position+ofsX-ofsY+ofsZ,wheel); | |
FL = MakeWheel("FL",position-ofsX-ofsY+ofsZ,wheel); | |
RR = MakeWheel("RR",position+ofsX-ofsY-ofsZ,wheel); | |
RL = MakeWheel("RL",position-ofsX-ofsY-ofsZ,wheel); | |
} | |
// Update is called once per frame | |
void FixedUpdate () { | |
float h = Input.GetAxisRaw("Horizontal"); | |
float v = Input.GetAxisRaw("Vertical"); | |
RR.motorTorque = v * motorTorque; | |
RL.motorTorque = v * motorTorque; | |
FL.steerAngle = h * steerAngle; | |
FR.steerAngle = h * steerAngle; | |
if(Input.GetButton("Jump")) | |
{ | |
RR.brakeTorque = brakeTorque; | |
RL.brakeTorque = brakeTorque; | |
} | |
else | |
{ | |
RR.brakeTorque = 0; | |
RL.brakeTorque = 0; | |
} | |
} | |
WheelCollider MakeWheel(string name, Vector3 potision, GameObject parent) | |
{ | |
GameObject obj = new GameObject(name); | |
obj.transform.parent = parent.transform; | |
obj.AddComponent<WheelCollider>(); | |
obj.transform.position = potision; | |
WheelCollider wc = obj.GetComponent<WheelCollider>(); | |
wc.radius = wheelRadius; | |
return wc; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment