2013-03-06 180 views
0

我正在研究3D格鬥遊戲。我有兩個角色與他們的動畫。我已經應用了我定製的角色控制器和角色控制器腳本。我有兩個按鈕之一移動魔鬼,一個移動後腦勺。四個按鈕可以播放不同的動畫,就像做一個拳頭,打一條腿等等。它的工作表現很好。 現在我已經使用膠囊對象與他們的膠囊對撞機作爲不同骨頭的孩子。就像我已經把一個膠囊物體與他們的對撞機放在左手骨骼的孩子之中.SO骨骼移動那個物體將會移動.. 我還在第二個球員身體上放置了一個帽子,我把它放在頭下方和腿上方它的意思是它放置在胸部。我也使用了沒有重力的剛體,並且在所有膠囊中都點擊了觸發器功能。現在我想要當我的第一個玩家的手碰到放置膠囊的第二個玩家主體時,它會調用一個函數。但是在腳本中它不會打電話..我不知道什麼是問題。可以任何身體指導我。這是我的腳本Unity3D中的碰撞檢測

public function Start() : void { 
f_inAirStartTime = Time.time; 
} 
//Checking if the character hit the ground (collide Below) 
public function IsGrounded() : boolean { 
return (c_collisionFlags & CollisionFlags.CollidedBelow); 
} 
//Getting if the character is jumping or not 
public function IsJumping() : boolean { 
return b_isJumping; 
} 
//Checking if the character is in the air more than the minimum time 
//This function is to make sure that we are falling not walking down slope 
public function IsAir() : boolean { 
return (f_inAirTime > f_minAirTime); 
} 
//Geting if the character is moving backward 
public function IsMoveBackward() : boolean { 
return b_isBackward; 
} 
public function Update() : void { 
//Get Main Camera Transform 
var cameraTransform = Camera.main.transform; 
//Get forward direction of the character 
v3_forward = cameraTransform.TransformDirection(Vector3.forward); 
v3_forward.y = 0; //Make sure that vertical direction equals zero 
// Right vector relative to the character 
// Always orthogonal to the forward direction vector 
v3_right = new Vector3(v3_forward.z, 0, -v3_forward.x); 

//Get Horizontal move - rotation 
var f_hor : float ;//= Input.GetAxis("Horizontal"); 
if(backword==true) 
{ 
f_hor=1; 
backword=false; 
} 
if(farword==true) 
{ 
f_hor=-1; 
farword=false; 
} 











//Get Vertical move - move forward or backward 
var f_ver : float = Input.GetAxis("Vertical"); 
//If we are moving backward 
if (f_ver < 0) { 
b_isBackward = true; 
} else { 
b_isBackward = false; 
} 
//Get target direction 
var v3_targetDirection : Vector3 = (f_hor * v3_right) + (f_ver * v3_forward); 
//If the target direction is not zero - that means there is no button pressing 
if (v3_targetDirection != Vector3.zero) { 
//Rotate toward the target direction 
v3_moveDirection = Vector3.Slerp(v3_moveDirection, v3_targetDirection, f_rotateSpeed * Time.deltaTime); 
v3_moveDirection = v3_moveDirection.normalized; //Get only direction by normalizing our target vector 
} else { 
v3_moveDirection = Vector3.zero; 
} 
//Checking if character is on the ground 
if (!b_isJumping) { 
//Holding Shift to run 
//if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey (KeyCode.RightShift)) { 
if(Left_punch_anim){ 


b_isRun = true; 

f_moveSpeed = runSpeed; 
//Right_punch_anim=false; 
} else { 
b_isRun = false; 
f_moveSpeed = speed; 
} 
//Press Space to Jump 
if (Input.GetButton ("Jump")) { 
f_verticalSpeed = jumpSpeed; 
b_isJumping = true; 
} 
} 
//Debug.Log(controller.velocity.sqrMagnitude+"magniture"); 

// Apply gravity 
if (IsGrounded()) { 
f_verticalSpeed = 0.0; //if our character is grounded 
b_isJumping = false; //Checking if our character is in the air or not 
f_inAirTime = 0.0; 
f_inAirStartTime = Time.time; 
} else { 
f_verticalSpeed -= gravity * Time.deltaTime; //if our character in the air 
//Count Time 
f_inAirTime = Time.time - f_inAirStartTime; 
} 
// Calculate actual motion 
var v3_movement : Vector3 = (v3_moveDirection * f_moveSpeed) + Vector3 (0, f_verticalSpeed, 0); // Apply the vertical speed if character fall down 
v3_movement *= Time.deltaTime; 
// Move the controller 
c_collisionFlags = controller.Move(v3_movement); 
//Play animation 
if (b_isJumping) { 
if (controller.velocity.y > 0) { 
animation[jumpPoseAnimation.name].speed = jumpAnimationSpeed; 
animation.CrossFade(jumpPoseAnimation.name, 0.1); 
} else { 
animation[fallPoseAnimation.name].speed = fallAnimationSpeed; 
animation.CrossFade(fallPoseAnimation.name, 0.1); 
} 
} else { 
if (IsAir()) { // Fall down 
animation[fallPoseAnimation.name].speed = fallAnimationSpeed; 
animation.CrossFade(fallPoseAnimation.name, 0.1); 
} else { 
if(controller.velocity.sqrMagnitude < 0.1) { 
    if(Left_punch_anim) 
    { 

    animation[leftanimAnimation.name].speed = runAnimationSpeed; 
animation.CrossFade(leftanimAnimation.name, 0.5); 
Left_punch_anim=false; 
idlemode=true; 

    } 
    else 
    if(Right_punch_anim) 
    { 

    animation[rightanimAnimation.name].speed = runAnimationSpeed; 
animation.CrossFade(rightanimAnimation.name, 0.5); 
Right_punch_anim=false; 
idlemode=true; 

    } 
    else 
    { 
//Debug.Log(controller.velocity.sqrMagnitude+"hjgkjgkjg"); 
animation[idleAnimation.name].speed = idleAnimationSpeed; 
animation.CrossFade(idleAnimation.name, 0.1); 
} 
} else { //Checking if the character walks or runs 
if (b_isRun) { 
//Debug.Log("In the run animation"); 
animation[leftanimAnimation.name].speed = runAnimationSpeed; 
animation.CrossFade(leftanimAnimation.name, 0.1); 
} else { 
animation[walkAnimation.name].speed = walkAnimationSpeed; 
animation.CrossFade(walkAnimation.name, 0.1); 
} 
} 
} 
} 
if(idlemode) 
{ 

} 
//Update rotation of the character 
if (v3_moveDirection != Vector3.zero) { 
transform.rotation = Quaternion.LookRotation(v3_moveDirection); 
} 
} 
public function OnControllerColliderHit(hit:ControllerColliderHit) 
{ 
    //  Debug.Log("Collision have been enter"); 
    } 

/*public function OnTriggerEnter(other:Collider) 
{ 

Debug.Log(other.gameObject.name); 
}*/ 
public function OnCollisionEnter(other:Collision) 
{ 
Debug.Log("collision is enter"); 
} 
function OnTriggerEnter(col:Collider) 
{ 
Debug.Log(col.gameObject.name); 

} 

回答

4

好的。我假設你既沒有得到OnCollisionEnter也沒有得到OnTriggerEnter調用。

確保具有這些方法的腳本位於實際的GameObject上,該對象具有碰撞器組件。

OnCollisionEnter引用:

「請注意,如果對撞機的一個也有附加的非運動剛體碰撞事件僅發送。」

綜上所述:如果你實際使用的物理系統中移動的對象,由於力OnCollisionEnter只叫,碰撞等

如果你想只登記一擊,沒有它通過引起運動物理系統,你可以使用OnTriggerEnter。 在這種情況下,剛體必須位於移動物體上,並且至少有一個碰撞體必須設置爲isTrigger。

還要確保圖層設置爲在projectSettings-> Physics中發生碰撞。

希望這可以幫助你。

+0

感謝您的回放...實際上我的角色我有字符控制器適用於整個網格,我有不同的膠囊對撞機在不同的身體parts.I沒有使用任何phsyics我只是想當這兩個字符相撞那麼函數將觸發up..I使用OnTriggerEnter函數...但它不工作 – 2013-03-06 10:13:00

+0

我沒有理解你的最後一行......「還要確保圖層設置爲在projectSettings-> Physics中碰撞。」 – 2013-03-06 10:15:53

+1

在Unity頂部欄中 - 單擊edit-> projectSettings-> Physics。這會在檢查器中顯示一個矩陣,允許您選擇哪些圖層與其他圖層相沖突。 – sune 2013-03-07 12:33:41