Knock Back in 3D only works for Y axis
I'm learning unity and for my first try I want to make a game in which there are boulders that can kill the player on contact. I have set my avatar with a RigidBody and capsuleCollider. The boulder is a simple rigidbody sphere with it's collider. When both of them collide the avatar gets knocked up in the Y axis as the script says but it almost stays on the same position for the X and Z axis.
This is the script I have tried:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag.Equals("Enemy"))
{
//Character stops walking
anim.SetBool(hashWalking, false);
//Gets the direction of the collision
Vector3 direction = transform.position - collision.gameObject.transform.position;
//Sets a fixed Y axis direction for the knockback
direction.y = 1;
Debug.Log("Direction=" + direction*deadForce);
//rb.velocity = direction * deadForce;
rb.AddForce(direction.normalized*deadForce);
}
}
I've tried both AddForce and set the velocity. When I play the game I get the info about the vector applied and it actually shows that the Z value is greater than Y but still seems like there's no movement backwards, just upwards, like in this example I have an output
Direction=(29.7, 300.0, -852.8)
What am I doing wrong? And also my spheres goes forward until the character gets hit, once this happens the sphere bounces back. How can I make them either stop or keep going after the collision without them being pushed back?