Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
あたり判定を保ったままオブジェクトを移動させる。
#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
You can’t perform that action at this time.