I have two character each with character controllers additional capsule colliders added to them with there “IsTrigger” property checked.
Now in my code im trying to add force to one of the character once its collider starts touching the other character capsule collider that is triggered however that is not working.
Can any one please tell me why this could be happening, If I transform its position in the “OnTriggerEnter” function it works but no go for the AddFroce.
AddForce has to be added inside FixedUpdate() as do all physics based forces. Instead of adding colliders set to is trigger, use OnControllerColliderHit().
//I have used these variables as it is with out any modification
private var speed = 3.0;
private var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function Update() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(0, 0, Input.GetAxis("Horizontal"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
Animations(); // This function is were i have played all my animations on keyboard
}
function Animations() {
if(Input.GetKey(KeyCode.RightArrow)) {
if(Input.GetKey(KeyCode.F)) {
animation.CrossFade("FastBang");
}
else if(Input.GetKey(KeyCode.B)) {
animation.CrossFade("BangBang");
}
else if(Input.GetKey(KeyCode.Space)) {
animation.CrossFade("Dodge");
}
else
animation.Play("Walk");
}
else
animation.Play("Ideal");
}
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject.tag == "AiCharacter")
{
var AiCharacter: GameObject = hit.gameObject;
if(animation.IsPlaying("FastBang"))
{
print("Hellos");
AiCharacter.rigidbody.AddForce(10,50,0);
}
}
}
From your original post you mention that the enemy is a CharacterController, but in your script you’re trying to add force to his rigidbody; I’m not sure that you could do both, at least in the manner that you’re attempting here.
In fact I’m not even sure how your getting OnTriggerEnter to fire in the first place, if the enemy has a CharacterController.
That probably won’t work either, as far as the AddForce is concerned at least.
You can’t have a CharacterController AND a rigidbody on the same object and move them around using both methods, you pretty much have to pick one or the other, or switch between the two.
As long as you have a CharacterController on the object instead of a collider + rigidbody, even if you add a rigidbody component, you won’t be able to add forces, you’ll only be able to move the object with either translation or the CharacterController movement commands.
oks Iam getting the point i.e I am suppose to use either one of them to control my characters movements. ok with that said il like you to see the attached image.
Here the script that I posted is applied on the Player character in the function “OnTriggerEnter” Iam actually checking if the player characters collider colliders with Ai players. then trying to Add force.
I dont know if I am doing this in the right way or not but one thing is that the “print(“Hellos”);” gets displayed in the logs when the colliders collide.
also the “OnCollisionEnter” wont work as my colliders are triggered
Not really understanding why you’re using two character controllers. The AI character isn’t player controllable anyway, is it? Seems like you’d be better off using raycasting to detect proximity to the player character rather than the large colliders pictured above. You can just set the raycast to only detect objects that are within a short distance.
yes tool55 my Ai character is not user controllable, I will try to use the raycasting method for my Ai character n only play the animation when the player character is close enough. I guess if the player is not close il simply have to play the walk animation till Ai is close enough. Thanks for the guidance.
legend411 I think your correct because the character controller itself is kind of a collider il try to stick to the method explained by “tools” have 1 character controller for my player character one only capsule collider for my Ai character.