Unity3D中的碰撞检测

Unity3D中的碰撞检测,unity3d,character,collision,unityscript,Unity3d,Character,Collision,Unityscript,我正在做3D格斗游戏。我有两个角色和他们的动画。我已经应用了角色控制器和角色控制器脚本,我已经定制。我有两个按钮,一个移动前字,一个移动后字,还有四个按钮,可以播放不同的动画,比如打一拳,打一条腿等等,直到它的效果非常好。 现在,我已经将胶囊对象与他们的胶囊碰撞器作为不同骨骼的孩子一起使用。就像我把一个胶囊物体和他们的碰撞器放在一起,左手骨头的孩子。所以骨头移动的地方,这个物体会移动。。 我也在第二个球员身上放了一个帽子,我把它放在头下和腿上,意思是放在胸部。我还使用了没有重力的刚体,在所有胶囊

我正在做3D格斗游戏。我有两个角色和他们的动画。我已经应用了角色控制器和角色控制器脚本,我已经定制。我有两个按钮,一个移动前字,一个移动后字,还有四个按钮,可以播放不同的动画,比如打一拳,打一条腿等等,直到它的效果非常好。 现在,我已经将胶囊对象与他们的胶囊碰撞器作为不同骨骼的孩子一起使用。就像我把一个胶囊物体和他们的碰撞器放在一起,左手骨头的孩子。所以骨头移动的地方,这个物体会移动。。 我也在第二个球员身上放了一个帽子,我把它放在头下和腿上,意思是放在胸部。我还使用了没有重力的刚体,在所有胶囊体中单击了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); 

}
public function Start():void{
f_inAirStartTime=Time.Time;
}
//检查角色是否触地(下方碰撞)
公共函数isground():boolean{
返回(c_collisionFlags&collisionFlags.CollidedBlow);
}
//获取角色是否正在跳跃
公共函数IsJumping():布尔值{
返回b_;
}
//检查角色在空中的时间是否超过最短时间
//这个功能是为了确保我们不是走下坡路,而是摔倒
公共函数IsAir():布尔值{
返回(f_inAirTime>f_minAirTime);
}
//获取角色是否向后移动
公共函数IsMoveBackward():布尔值{
返回b_is backward;
}
公共函数更新():void{
//获取主摄影机变换
var camerattransform=Camera.main.transform;
//获取角色的前进方向
v3_forward=摄像机变换变换方向(矢量3向前);
v3_forward.y=0;//确保垂直方向等于零
//相对于角色的右向量
//始终与前进方向向量正交
v3_right=新向量3(v3_forward.z,0,-v3_forward.x);
//获得水平移动-旋转
变量f_hor:float;/=Input.GetAxis(“水平”);
if(backword==true)
{
f_hor=1;
背景词=假;
}
如果(farword==true)
{
f_hor=-1;
farword=false;
}
//获得垂直移动-向前或向后移动
变量f\u ver:float=Input.GetAxis(“垂直”);
//如果我们后退
如果(f_ver<0){
b_isBackward=true;
}否则{
b_isBackward=false;
}
//获得目标方向
var v3_targetDirection:Vector3=(垂直*v3_右)+(垂直*v3_前向);
//如果目标方向不是零-这意味着没有按钮按下
if(v3_targetDirection!=向量3.0){
//向目标方向旋转
v3_moveDirection=Vector3.Slerp(v3_moveDirection,v3_targetDirection,f_rotateSpeed*Time.deltaTime);
v3\u moveDirection=v3\u moveDirection.normalized;//通过对目标向量进行规格化,仅获取方向
}否则{
v3_moveDirection=Vector3.0;
}
//检查字符是否在地面上
如果(!b_正在跳跃){
//轮班
//if(Input.GetKey(KeyCode.LeftShift)| Input.GetKey(KeyCode.RightShift)){
如果(左冲压动画){
b_isRun=真;
f_moveSpeed=运行速度;
//右\u冲压\u动画=假;
}否则{
b_isRun=假;
f_moveSpeed=速度;
}
//按空格键跳转
if(Input.GetButton(“跳转”)){
f_垂直速度=跳跃速度;
b_=true;
}
}
//Debug.Log(controller.velocity.sqrMagnitude+“magniture”);
//施加重力
如果(isground()){
f_verticalSpeed=0.0;//如果我们的角色是固定的
b_isJumping=false;//检查我们的角色是否在空中
f_inAirTime=0.0;
f_inAirStartTime=Time.Time;
}否则{
f_垂直速度-=重力*Time.deltaTime;//如果我们的角色在空中
//计时
f_inAirTime=Time.Time-f_inAirStartTime;
}
//计算实际运动
var v3_movement:Vector3=(v3_moveDirection*f_moveSpeed)+Vector3(0,f_verticalSpeed,0);//如果字符掉落,则应用垂直速度
v3_movement*=Time.deltaTime;
//移动控制器
c_collisionFlags=控制器.Move(v3_移动);
//播放动画
如果(b_正在跳跃){
如果(控制器速度y>0){
动画[jumpPoseAnimation.name]。速度=jumpAnimationSpeed;
动画.CrossFade(jumpPoseAnimation.name,0.1);
}否则{
动画[fallPoseAnimation.name]。速度=fallAnimationSpeed;
动画.CrossFade(fallPoseAnimation.name,0.1);
}
}否则{
如果(IsAir()){//掉下来
动画[fallPoseAnimation.name]。速度=fallAnimationSpeed;
动画.CrossFade(fallPoseAnimation.name,0.1);
}否则{
if(控制器速度sqrMagnitude<0.1){
如果(左冲压动画)
{
动画[LeftAnimationAnimation.name]。速度=runAnimationSpeed;
animation.CrossFade(leftAnimation.name,0.5);
左冲压动画=假;
idlemode=true;
}
其他的
如果(右冲压动画)
{
动画[RightAnimationAnimation.name].speed=runAnimationSpeed;
动画.CrossFade(RightAnimation.name,0.5);
右\u冲压\u动画=假;
idlemode=true;
}
其他的
{ 
//Debug.Log(controller.velocity.sqrMagnitude+“hjgkjgkjg”);
动画[idleAnimation.name].speed=idleAnimationSpeed;
动画.CrossFade(idleAnimation.name,0.1);
}
}else{//检查角色是否行走或奔跑
如果(b_isRun){
//Log(“在运行动画中”);
动画[LeftAnimationAnimation.name]。速度=runAnimationSpeed;
animation.CrossFade(leftAnimation.name,0.1);
}否则{
动画[walkAnimation.name]。速度=walkAnimationSpeed;
动画.CrossFade(walkAnimation.name,0.1);
}
}
}
}
if(idlemode)
{
}
//更新角色的旋转
if(v3_moveDirection!=Vector3.zero){
transform.rotation=Quaternion.LookRotation(v3\u移动方向);
}
}
ControllerColliderHit的公共函数(hit:ControllerColliderHit)
{
//Log(“已输入冲突”);
}   
/*公共功能对撞机(其他:对撞机)
{
Log(other.gameObject.name);
}*/
公共功能OnCollisionCenter(其他:冲突)
{
扩散系数