Unable to add force in side the OnTriggerEnter function

Hi Guys

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.

Please help me out here guys

Thankyou all in advance.

AddForce has to be added inside FixedUpdate() as do all physics based forces. Instead of adding colliders set to is trigger, use OnControllerColliderHit().

it would be really helpful to post a code, its hard to debug your script if we cant see it,

how are you adding the force? velocity?

if you look at the script reference there is no AddForce for character controller, only Rigidbody, so thats likely your issue

heres the script reference for character crontroller, there are some ways in there where you can make it move, try velocity

http://unity3d.com/support/documentation/ScriptReference/CharacterController.html

Hi Guys

this is my code have a look

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

Thanks guys waiting for you reply

I have a problem with this line:

AISumo.rigidbody.AddForce(10,50,0);

the Identifier AISumo does not exist anywhere else inside the script. What is it? Is it the right target that you want to add force to?

Sorry its not AISumo.rigidbody.AddForce(10,50,0);

its suppose to be 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.

yeah, try OnCollisionEnter(hit : Collision)

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

502030--17767--$2.jpg

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.

Your illustration implies that you have a capsule collider AND a charactercontroller on the same object, I’m pretty sure you can’t do that at all?

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.

Thank you all for the support.