Unity3D Instantiate在不满足所有条件时工作

时间:2015-09-22 17:14:00

标签: object unity3d instantiation

这个脚本应该在点击比hitRange更近的其他对象后产生对象,并且标记为" Block"。问题是,在推送LMB之后,它总是会产生新的对象,也就是在没有满足所需的条件时。

#pragma strict

var blockName : String;
private static var blockToPlace : Rigidbody;
var hitRange : float;
private var hit : RaycastHit;
var blockLayer : int = 8;
private var hitBlock : boolean;


function Update(){
    if(Input.GetMouseButtonDown(0)){
        blockToPlace = EditorUtility.InstantiatePrefab(AssetDatabase.LoadAssetAtPath("Assets/Models/Blocks/"+blockName+".prefab", Rigidbody)) as Rigidbody;
        hitBlock = Physics.Raycast(transform.position, transform.forward, hit, Mathf.Infinity, blockLayer);
        if(hitBlock == true){
            if(hit.collider.tag == "Block"){
                if(hit.distance < hitRange){
                    var block : Rigidbody;
                    block = Instantiate(blockToPlace,hit.normal+hit.transform.position,Quaternion.identity) as Rigidbody;
                    hitBlock = false;
                }
            }
        }
    }
}

0 个答案:

没有答案