Skip to content

Instantly share code, notes, and snippets.

@kuetsuhara
Last active December 31, 2015 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuetsuhara/7924430 to your computer and use it in GitHub Desktop.
Save kuetsuhara/7924430 to your computer and use it in GitHub Desktop.
あたり判定を保ったままオブジェクトを移動させる。
#pragma strict
private var center : Vector3;
private var target : GameObject;
private var moving : boolean;
private var fixJoint : FixedJoint;
function Start () {
center = Vector3(Screen.width/2, Screen.height/2, 0);
moving = false;
}
function Update () {
Screen.lockCursor = true;
if (Input.GetButtonDown("Fire1")) {
move();
}
}
function move() {
var ray : Ray;
var hit : RaycastHit;
ray = Camera.main.ScreenPointToRay(center);
// 何かにぶつかったら gameObject を取得
if (Physics.Raycast(ray, hit, 30)) {
print("hit!");
target = hit.collider.gameObject;
Debug.Log(target.tag);
if(target.tag == "Target"){
var camera : GameObject;
camera = GameObject.FindWithTag("MainCamera");
fixJoint = camera.GetComponent("FixedJoint");
if(fixJoint.connectedBody && moving){
moveOut();
}
else{
fixJoint.connectedBody = target.rigidbody;
moving = true;
}
}
else{
moveOut();
}
} else {
print("not hit!");
if(moving){
moveOut();
}
}
}
function moveOut(){
fixJoint.connectedBody = null;
target = null;
moving = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment