Skip to content

Instantly share code, notes, and snippets.

@Buravo46
Last active January 3, 2016 00:48
Show Gist options
  • Save Buravo46/8384775 to your computer and use it in GitHub Desktop.
Save Buravo46/8384775 to your computer and use it in GitHub Desktop.
【Unity】RigidBodyコンポーネントが付いたオブジェクトを前後に転がし、左右に回転させるスクリプト。
using UnityEngine;
using System.Collections;
public class RigidBody3DObjectControlScript : MonoBehaviour {
private bool isRotateLeft = false;
private bool isRotateRight = false;
private bool isForward = false;
private bool isBack = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// 左キーを押し続けていたら
if(Input.GetKey("left")){
isRotateLeft = true;
} else if(Input.GetKey("right")){ // 右キーを押し続けていたら
isRotateRight = true;
} else if(Input.GetKey("up")){ // 上キーを押し続けていたら
isForward = true;
} else if(Input.GetKey("down")){ // 下キーを押し続けていたら
isBack = true;
} else if(Input.GetKeyUp("left") || Input.GetKeyUp("right") || Input.GetKeyUp("up") || Input.GetKeyUp("down")){
isRotateLeft = false;
isRotateRight = false;
isForward = false;
isBack = false;
}
}
void FixedUpdate(){
if(isRotateLeft){
rigidbody.AddTorque(Vector3.down * 10.0f);
} else if(isRotateRight){
rigidbody.AddTorque(Vector3.up * 10.0f);
} else if(isForward){
rigidbody.AddForce(Vector3.forward * 10.0f);
} else if(isBack){
rigidbody.AddForce(Vector3.back * 10.0f);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment