2016-05-12 56 views
1

我寫了一個腳本,其中一個遊戲物體打算移動到從玩家相機拋出的raycast.point。在大多數情況下,這可以很好地工作,但是當物體快速向攝像機移動時(即光線數據源),有多次(大約攝像機距離物體45度)。使用光線投射問題移動物體

我嘗試了很多方法試圖解決這個問題,但是我似乎無法挖掘出這個問題的根源。通過停用附加到正在移動的對象的碰撞器來管理以防止發生這種情況。不過,由於各種原因,我需要碰撞器,所以這種方法並不合適。

如果任何人都可以提供任何指向哪裏我錯了我會非常感激。

注:預先在UJS編碼

非常感謝,瑞安

function FixedUpdate() { 
 

 
    if (modObj != null && !guiMode) { 
 

 
    //Panel Control 
 
    if (!selectObjPanel.activeSelf && !modifySelectObjPanel.activeSelf) //if the selectpanel not open and modSelect not already activated 
 
    { 
 
     activateModSelectObjPanel(true); //activate it 
 
    } else if (selectObjPanel.activeSelf) { 
 
     activateModSelectObjPanel(false); 
 
    } 
 

 

 
    //Move 
 
    if (Input.GetKey(KeyCode.E)) { 
 
     if (Input.GetKeyDown(KeyCode.E)) { 
 
     //    modObj.GetComponent(BoxCollider).enabled = false; 
 
     modObj.GetComponent(Rigidbody).isKinematic = true; 
 
     modObj.GetComponent(Rigidbody).useGravity = false; 
 
     //     
 
     initPos = modObj.transform.position; 
 
     var initRotation = modObj.transform.rotation; 
 
     } 
 

 
     moveObject(modObj, initPos, initRotation); 
 
    } else { 
 
     //   modObj.GetComponent(BoxCollider).enabled = true; 
 
     modObj.GetComponent(Rigidbody).isKinematic = false; 
 
     modObj.GetComponent(Rigidbody).useGravity = true; 
 
    } 
 
    } 
 
} 
 

 
function moveObject(modObj: GameObject, initPos: Vector3, initRotation: Quaternion) { 
 
    //Debug.Log("Moving Object"); 
 

 
    var hit: RaycastHit; 
 
    var foundHit: boolean = false; 
 

 
    foundHit = Physics.Raycast(transform.position, transform.forward, hit); 
 
    //Debug.DrawRay(transform.position, transform.forward, Color.blue); 
 

 
    if (foundHit && hit.transform.tag != "Player") { 
 
    //Debug.Log("Move to Hit Point: " + hit.point); 
 
    modifyObjGUIscript.activateMoveDisplay(initPos, hit.point); 
 

 
    var meshHalfHeight = modObj.GetComponent. <MeshRenderer>().bounds.size.y/2; //helps account for large and small objects 
 
    //  Debug.Log("CurObj Mesh Min: " + meshHalfHeight); 
 

 
    //  modObj.transform.position = hit.point; //***method 01*** 
 
    //  modObj.transform.position = Vector3.Lerp(initPos, hit.point, speed); //***method 02*** 
 
    //  modObj.transform.position = Vector3.SmoothDamp(initPos, hit.point, velocity, smoothTime); //***method 02*** 
 

 
    var rb = modObj.GetComponent. <Rigidbody>(); 
 
    rb.MovePosition(hit.point); //***method 03*** 
 

 
    modObj.transform.position.y = modObj.transform.position.y + meshHalfHeight + hoverHeight; 
 

 
    modObj.transform.rotation = initRotation; 
 
    } 
 
}

+0

相機是否連接到任何東西?檢查raycast是否沒有碰到任何靠近相機的東西。 –

+0

是的相機連接到播放器控制器,不知道這是問題 –

回答

1

原來,問題是由光線投射命中移動物體造成的。通過僅允許來自地形的點擊被用作要移動到的點來解決這個問題。

if(foundHit && hit.transform.tag == "Terrain")