- Home /
Apply force to character controller?
I know that you can't really apply force to a character controller directly but can you simulate this? Velocity is read-only apparently, and I'm not sure how to produce the same effect. In my script I have a grapple ability that is basically a prefab being fired and whenever it collides, it turns kinematic and freezes in place. I want to know how to change jelly's velocity so that on collision he "hops" so far to the right on the x axis to simulate pulling on the grapple. Below is the script I currently have, but it functions only on rigidbodies.
function OnCollisionEnter(collision : Collision)
{
// when the spawned grappling hook collides with another object, it is made kinematic, thus freezing it in place and making it appear as if it was stuck
rigidbody.isKinematic = true;
//This gets the shooter, or jelly in this case...
jelly = GameObject.Find("jelly");
//this makes jelly jump
jelly.rigidbody.AddForce(250,250,0);
//after force is added the now "stuck" grapple will wait for 1 and a half seconds until it destroys itself
yield WaitForSeconds (1.5);
Destroy (gameObject);
}
//the coordinates of the grapples object against the coordinates of jelly
function Update()
{
jelly = GameObject.Find("jelly");
//If they are at the same Y height or less than jelly, they are destroyed
if(this.transform.position.y<=jelly.transform.position.y)
Destroy (gameObject);
//If they are greater than 10 units over the height of jelly they are destroyed
if(this.transform.position.y>jelly.transform.position.y+10)
Destroy (gameObject);
}
[Edit by Berenger : Please make sure to format your code properly next time. It's the button with a bunch of 1 and 0]
Answer by Berenger · Jun 03, 2012 at 04:19 PM
You can use a rigidbody in a similar way. But if you want to push a character controller, you need to look how the jump is performed in character motor and do something similar, in an other direction.
hmmm... I tried it but I have no idea what I'm doing here. I hypothesized that I could just change the jump "command" where it tells the velocity on the y axis to change and specify that beforehand that it needs to find jelly and apply velocity to the charactercontroller but I'm just shooting in the dark here
function OnCollisionEnter(collision : Collision) {
// when the spawned grappling hook collides with another object, it is made kinematic, thus freezing it in place and making it appear as if it was stuck rigidbody.is$$anonymous$$inematic = true;
//This gets the shooter, or jelly in this case... jelly = GameObject.Find("jelly"); //this was supposed to make jelly jump jelly.charactercontroller.velocity.y = $$anonymous$$athf.$$anonymous$$in(0, velocity.y) - movement.gravity Time.deltaTime; jelly.charactercontroller.velocity.x = $$anonymous$$athf.$$anonymous$$in(0, velocity.x) Time.deltaTime;w
//after force is added the now "stuck" grapple will wait for 1 and a half seconds until it destroys itself yield WaitForSeconds (1.5); Destroy (gameObject); }
//the coordinates of the grapples object against the coordinates of jelly function Update(){ jelly = GameObject.Find("jelly"); //If they are at the same Y height or less than jelly, they are destroyed if(this.transform.position.y<=jelly.transform.position.y){ Destroy (gameObject); } //If they are greater than 10 units over the height of jelly they are destroyed if(this.transform.position.y>jelly.transform.position.y+10){ Destroy (gameObject); } }
Take a look at that. If it doesn't help, could you add \ before your code and \<\\pre> after, for the formatting ? Also, Find( name ) is a bit slow, you should try another to get the jelly's reference. By tag or type is faster.