Last active
December 31, 2015 02:59
-
-
Save kuetsuhara/7924430 to your computer and use it in GitHub Desktop.
あたり判定を保ったままオブジェクトを移動させる。
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
#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