- Home /
How to apply knockback on collision (2D)
Here is the code for my player movement:
function Update () {
animator.SetInteger("Direction", 4);
if (Input.GetKey(moveUp) && isGrounded == 0)
{
rigidbody2D.velocity.y = jumpheight;
isGrounded = 1;
animator.SetInteger("Direction", 2);
}
else if (Input.GetKey(moveLeft))
{
transform.position += Vector3.left * speed * Time.deltaTime;
animator.SetInteger("Direction", 3);
}
else if (Input.GetKey(moveRight))
{
transform.position += Vector3.right * speed * Time.deltaTime;
animator.SetInteger("Direction", 1);
}
}
And here is the code for the collision:
var PlayerHealth = 100.0;
function Start () {
}
function Update () {
}
function OnCollisionEnter2D(coll: Collision2D) {
if (coll.gameObject.tag == "Ghost"){
PlayerHealth = PlayerHealth - 5.00;
rigidbody.AddForce (Vector3.left * 10);
}
}
The add force has no effect on my character. The collision works though, as the players health goes down. I'm pretty certain it has to do with the transform position in my movement, but I'm not sure how to fix it. Any help?
Hmm, yes. You cannot combine physics with hard-setting the position - the position set will simply override any forces applied. You can either re-write your left-right movement to use physics forces, or re-write your knock back to NOT use physics forces, or, here's a nice thought, re-use your jump code by making the knockback hit you back AND UP, setting your isGrounded appropriately.
How would i do any of those? I just copied a youtube tutorial on the movement. How would i make the knock back not use physics but still be a knockback? Or how would i move left and right smoothly without fixing position? (I would rather do the latter as it allows me to put in other effects as well)
addforce can be a pain i always but a big number in add force like 10000 so i know for a fact it has no effect but emolk is right too i think try what i sed you might get lucky
Could anybody suggest a way to change the movement of my character to use physics?
Answer by tetigi · May 07, 2014 at 02:57 PM
As Pyrian said, you cannot combine the physics engine whilst hard-setting the position. However, you can turn the physics engine on and off by setting the RigidBody.isKinematic variable.
Whilst this is on, you can set your position manually as before, and when you want the physics to take over (such as when a collision happens), you turn it off and make sure that you do not make any more position updates until it's back on again. The physics engine should take care of the rest.
Looking at your code, perhaps you could wrap that in an if statement that checks whether or not it is currently kinematic - if it is, do standard control stuff. If it's off, you'll need to wait until your collision is 'finished' before it turns back on.
Maybe something like:
function Update() {
if (rigidbody2D.isKinematic) {
// Do Input key positional stuff
} else {
// Wait for collision to finish
if (collisionIsFinished()) {
rigidbody2D.isKinematic = true;
}
}
// other things
}
bool collisionIsFinished() {
// code that checks when your collision is done or whatever
}
and then in the player code, add one simple change:
function OnCollisionEnter2D(coll: Collision2D) {
if (coll.gameObject.tag == "Ghost"){
PlayerHealth = PlayerHealth - 5.00;
rigidbody.AddForce (Vector3.left * 10);
rigidbody2D.isKinematic = false;
}
Your answer
Follow this Question
Related Questions
Deteriorating force on object? 0 Answers
How do I add diagonal force? 0 Answers
Uneven speed in 2d movement script 2 Answers
knockback 3d c# 1 Answer
Rigidbody2D x velocity not moving unless placed in FixedUpdate 1 Answer