Unity3D Instantiate 在并非所有条件都满足时工作
Unity3D Instantiate works when not all conditions are met
此脚本应在点击比 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;
}
}
}
}
}
我自己也遇到过同样的问题。 Layers 不完全是整数,而是位移。
在图层蒙版上观看这个非常棒的视频:www.youtube.com/watch?v=n8mAjwn-MB8
var blockLayer : int = 8;
应该是
var blockLayer : int = 1 << 8;
此脚本应在点击比 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;
}
}
}
}
}
我自己也遇到过同样的问题。 Layers 不完全是整数,而是位移。
在图层蒙版上观看这个非常棒的视频:www.youtube.com/watch?v=n8mAjwn-MB8
var blockLayer : int = 8;
应该是
var blockLayer : int = 1 << 8;