碰撞检测在Unity3D(Collision Detection In Unity3D)

2019-08-17 08:12发布

我对3D格斗game.I工作有两个性格与他们的animation.I应用了角色管理和角色管理脚本,我已经定制。 我有两个按钮,一个farword移动和一个移动backword..And四粒扣打不同的动画就像做了一拳,打腿等高达其工作perfactly罚款。 现在我已经用胶囊物体与他们的胶囊撞机不同骨头的孩子。 像我有一个地方胶囊物体与他们的对撞机左手bone.SO的子女,该对象的骨骼此举将移动..我也对第二个玩家身体capusle我放在头下面和上面的腿了意味着它放置在胸前7815622。 我也使用不具有重力和onTrigger功能在所有胶囊obects ..现在我想点击的时候,我第一个球员用手触摸第二的球员主体,其中胶囊是地方它会在脚本中调用一个function.But刚体它不会call..I不知道什么是problem..Can任何机构指导我。 这里是我的脚本

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); 

}

Answer 1:

好的。 我假设你既不是OnCollisionEnter也不OnTriggerEnter电话。

确保,即使用这些方法的脚本是实际游戏对象,有对撞机分量。

OnCollisionEnter-参考:

“需要注意的是碰撞事件只会被发送,如果对撞机的一个也有附加的非刚体的运动。”

综上所述:OnCollisionEnter只叫你实际使用的物理系统中移动的对象,由于力,碰撞等。

如果你想只登记一击没有它通过物理系统引起的运动,你可以使用OnTriggerEnter。 在这种情况下,刚体必须是移动物体上,并且所涉及的对撞机中的至少一个必须被设置为isTrigger。

还确保这些层被设置在projectSettings->物理碰撞。

希望这可以帮助你。



文章来源: Collision Detection In Unity3D