问题描述
我正在做3D格斗游戏。我有他们的动画的两个字符。我应用字符控制器和字符控制器脚本,我已经定制。我有两个按钮一移动farword和一个移动backword..And四个按钮播放不同的动画,就像打一个拳头,打腿等直到它的工作perfactly罚款。
现在我使用胶囊对象和他们的胶囊对撞机作为不同骨骼的孩子。像我已经放置一个胶囊对象与他们的对撞机的左手骨的孩子。骨骼移动,该对象将移动。
我也放置了capusle在第二个播放器的身体,我放置在头下,在腿上它的意思是它在胸部erea的地方。我也使用没有重力和onTrigger功能的刚性身体点击在所有胶囊obects ..现在我想,当我的第一个玩家手触摸第二个播放器主体在其中胶囊放置它会调用一个函数。但在脚本中不会调用。我不知道是什么问题。可以任何身体指导我。这是我的脚本
public function Start():void {
f_inAirStartTime = Time.time;
}
//检查字符是否撞到地面(碰撞下面)
public function IsGrounded():boolean {
return(c_collisionFlags& CollisionFlags.CollidedBelow);
}
//获取字符是否跳转
public function IsJumping():boolean {
return b_isJumping;
}
//检查字符是否在空中超过最小时间
//这个函数是为了确保我们不落下斜坡
public function IsAir():boolean {
return(f_inAirTime> f_minAirTime);
}
//获取字符是否向后移动
public function IsMoveBackward():boolean {
return b_isBackward;
}
public function Update():void {
//获取主相机变换
var cameraTransform = Camera.main.transform;
//获取字符的前进方向
v3_forward = cameraTransform.TransformDirection(Vector3.forward);
v3_forward.y = 0; //确保垂直方向等于零
//相对于字符的右向量
//始终与前向方向向量正交
v3_right = new Vector3(v3_forward.z,0, v3_forward.x);
//获取水平移动 - 旋转
var f_hor:float; // = Input.GetAxis(Horizontal);
if(backword == true)
{
f_hor = 1;
backword = false;
}
if(farword == true)
{
f_hor = -1;
farword = false;
}
//获取垂直移动 - 向前或向后移动
var f_ver:float = Input.GetAxis(Vertical);
//如果我们向后移动
if(f_ver< 0){
b_isBackward = true;
} else {
b_isBackward = false;
}
//获取目标方向
var v3_targetDirection:Vector3 =(f_hor * v3_right)+(f_ver * v3_forward);
//如果目标方向不为零 - 表示没有按钮按下
if(v3_targetDirection!= Vector3.zero){
//向目标方向旋转
v3_moveDirection = Vector3.Slerp(v3_moveDirection,v3_targetDirection,f_rotateSpeed * Time.deltaTime);
v3_moveDirection = v3_moveDirection.normalized; //通过标准化目标向量获得方向
} else {
v3_moveDirection = Vector3.zero;
}
//检查字符是否在地上
if(!b_isJumping){
//保持Shift以运行
// 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;
}
//按空格跳转
if(Input.GetButton(Jump)){
f_verticalSpeed = jumpSpeed;
b_isJumping = true;
}
}
//Debug.Log(controller.velocity.sqrMagnitude+\"magniture);
//应用重力
if(IsGrounded()){
f_verticalSpeed = 0.0; //如果我们的字符被接地
b_isJumping = false; //检查我们的角色是否在空中
f_inAirTime = 0.0;
f_inAirStartTime = Time.time;
} else {
f_verticalSpeed - = gravity * 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 = controller.Move(v3_movement);
//播放动画
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()){//掉落
动画[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.Cross Fade(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 {//检查字符是否走动或运行
if(b_isRun){
//Debug.Log(在运行动画中);
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)
{
}
//更新旋转的字符
if(v3_moveDirection!= Vector3.zero){
transform.rotation = Quaternion.LookRotation(v3_moveDirection);
}
}
public function OnControllerColliderHit(hit:ControllerColliderHit)
{
// Debug.Log(Collision has 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);
}
我假设你既没有得到OnCollisionEnter也没有得到OnTriggerEnter的调用。
确保这些方法的脚本是在实际的GameObject,有collider-component。 / p>
OnCollisionEnter-Reference:
总结:OnCollisionEnter只有在你真正使用物理学的时候才会被调用
如果你只想通过物理系统注册一个没有引起移动的命中,你可以使用OnTriggerEnter。
在这种情况下,刚体必须在移动对象上,并且至少有一个涉及的对撞机必须设置为isTrigger。
还要确保层设置为在projectSettings-> Physics中碰撞。
希望这有助于你。
I am working on 3D fighting game.I have two character with their animation.I have applied character controller and character controller script which i have customized. I have two button one to move farword and one to move backword..And four button to play different animation just like make a punch, hit a leg etc. Upto that its work perfactly fine.Now I have use capsule object with their capsule collider as a child of different bones. Like i have place one capsule object with their collider the child of left hand bone.SO where the bone move that object will move..I have also place a capusle on the second player body which i place below the head and above the legs its mean it place in the chest erea. I have also used Rigid body having no gravity and onTrigger function clicked in all capsule obects.. Now i want that when my first player hand touch the second player main body in which capsule is place it will call a function.But in the script it will not call..I dont know what is the problem..Can any body guide me. Here is my script
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);
}
Alright. I assume that you get neither the OnCollisionEnter nor the OnTriggerEnter call.
Ensure, that the scripts with these methods are on the actual GameObject, that has the collider-component.
OnCollisionEnter-Reference:
To sum up: OnCollisionEnter is only called if you actually use the physics system for moving the objects around due to forces, collisions etc.
If you want to just register a hit without it causing movement via the physics-system, you can use the OnTriggerEnter.In this case, the rigidbody must be on the moving object, and at least one of the involved colliders must be set to isTrigger.
Also ensure that the layers are set to collide in projectSettings->Physics.
Hope this helps you.
这篇关于碰撞检测在Unity3D的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!