【问题标题】:Collision Detection In Unity3DUnity3D中的碰撞检测
【发布时间】:2013-02-20 19:40:29
【问题描述】:

我正在开发 3D 格斗游戏。我有两个角色和他们的动画。我已经应用了我自定义的角色控制器和角色控制器脚本。我有两个按钮,一个用于移动 farword,一个用于移动 backword..还有四个按钮来播放不同的动画,就像打一拳、打腿等。到目前为止,它的工作非常好。 现在我已经使用胶囊对象和他们的胶囊对撞机作为不同骨骼的孩子。就像我将一个胶囊对象与他们的对撞机放在左手骨头的孩子一样。所以骨头移动那个对象会移动.. 我还在第二个玩家身体上放置了一个胶囊,我将它放置在头部下方和腿部上方,这意味着它放置在胸部区域。我还使用了没有重力的刚体,并且在所有胶囊对象中单击了 onTrigger 函数。现在我希望当我的第一个玩家手触摸放置胶囊的第二个玩家主体时,它将调用一个函数。但是在脚本中它不会打电话..我不知道是什么问题..任何人都可以指导我。这是我的脚本

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

}

【问题讨论】:

    标签: unity3d character collision unityscript


    【解决方案1】:

    好的。我假设您既没有收到 OnCollisionEnter 也没有收到 OnTriggerEnter 调用。

    确保具有这些方法的脚本位于具有 collider-component 的实际 GameObject 上。

    OnCollisionEnter-参考:

    “请注意,仅当其中一个碰撞器还附加了非运动学刚体时才会发送碰撞事件。”

    总结一下:OnCollisionEnter 仅在您实际使用物理系统因力、碰撞等原因移动对象时才会调用。

    如果你只想注册一个命中而不让它通过物理系统引起移动,你可以使用 OnTriggerEnter。 在这种情况下,刚体必须在移动的物体上,并且至少有一个涉及的碰撞器必须设置为 isTrigger。

    还要确保在 projectSettings->Physics 中将图层设置为碰撞。

    希望对你有所帮助。

    【讨论】:

    • 感谢 sune 的重播...实际上,在我的角色上,我有适用于整个网格的角色控制器,并且我在不同的身体部位有不同的胶囊对撞机。我没有使用任何我想要的物理当这两个字符发生冲突时,该函数将触发..我使用了 OnTriggerEnter 函数...但它不起作用
    • 我没听懂你的最后一行..."还要确保在 projectSettings->Physics 中将图层设置为碰撞。"
    • 在 Unity 顶部栏中 - 单击编辑->projectSettings->Physics。这将在检查器中向您显示一个矩阵,它允许您选择哪些层与哪些其他层发生碰撞。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多