- Home /
how to apply force on correct direction in wall jump using Rigidbody
Hi guys, i need help with wall jump/leap logic. my game is 2.5D endless runner very simple character is cube and it'll stay cube.i want to make wall jumping with rigidbody i have made it using OnCollisionEnter it is working but force is in wrong direction straight back i don't know how to apply it on angle .
My scene and roughly what i want
code i am using fot it.
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
if (!IsGrounded && contact.normal.y < 0.1f)
{
//Force is straight
characterControl.RIGIDBODY.AddForce(contact.normal * 10f, ForceMode.Impulse);
Debug.DrawRay(contact.point, contact.normal, Color.yellow, 5f);
}
}
}
Complete script
public class PlayerJump : MonoBehaviour
{
[SerializeField]
protected float JumpForce;
[SerializeField]
protected float FallMultiplier;
[SerializeField]
protected float lowJumpGravity;
public bool IsGrounded;
public BoxCollider dectorCollider;
public CharacterControl characterControl;
void Awake()
{
characterControl = GetComponent<CharacterControl>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
if (IsGrounded)
{
characterControl.Jump = true;
}
}
}
void FixedUpdate()
{
IsGrounded = false;
ApplyGravity();
//Applying Jump
if (characterControl.Jump == true)
{
characterControl.RIGIDBODY.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
characterControl.Jump = false;
}
}
void ApplyGravity()
{
if (characterControl.RIGIDBODY.velocity.y < 0f)
{
characterControl.RIGIDBODY.velocity += Vector3.up * Physics.gravity.y * (FallMultiplier - 1) * Time.deltaTime;
}
//if it's jumping
else if (characterControl.RIGIDBODY.velocity.y > 0f && characterControl.Jump == false)
{
characterControl.RIGIDBODY.velocity += Vector3.up * Physics.gravity.y * (lowJumpGravity - 1) * Time.deltaTime;
}
}
void OnTriggerStay(Collider GroundColider)
{
if (!GroundColider.isTrigger && GroundColider.gameObject.tag == "Ground")
{
IsGrounded = true;
}
}
//for wall jump testing failed
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
if (!IsGrounded && contact.normal.y < 0.1f)
{
//Force is straight
characterControl.RIGIDBODY.AddForce(contact.normal * 10f, ForceMode.Impulse);
Debug.DrawRay(contact.point, contact.normal, Color.yellow, 5f);
}
}
}
}
Hello there
I recommend you to Simplify your post. Remake it just asking "How to aplly a force in corect direction" or something liek this.
Good luck,
ok bro let's try that thanks btw
Answer by ZeBarba · Oct 07, 2019 at 11:31 AM
In your for each you should use contact.normal.normalized
.
Just contact.normal
is a world coordinate that increases y value as you go up, therefore making the magnitude also increase.
normalize your normal and you should be good.
didn't work still same result, thoughj i noticed it right now that force is increasing on collision with left wall
How are you moving your player around? If it is only on the left wall, maybe the automatic movement force is been added with the wall jump force (the horizontal component of that force).
yes it automatic movement since it is endless runner yeah that makes sense
void Update()
{
move = (transform.forward * speed * Time.deltaTime);
RIGIDBODY.$$anonymous$$ovePosition(transform.position + move);
}
.normal
should be a direction, with .point
being the coordinate, but the documentation of ContactPoint is pretty light. Is that now how it works?
i am not sure what you mean by that but no it don't work with .point, normal is the direction of the wall
Yeah don't use point
as that's a coordinate, I was disagreeing with ZeBarba's claim that the normal will change with the position of the contact point.
Answer by tormentoarmagedoom · Oct 07, 2019 at 09:04 AM
Ok lets see the prolem then.
As I undestand,. you want to apply the forces to go up when touching a wall. I've never done something like that, but if i had this problem, i should use colliders to detect on wich side of the caracter is the wall.
Then apply a "prepared" force (i mean, if wall is at right, i have a vector, always the same, that apllies a force to left and up), and the same to other side. I mean, Not to calculate the direction usign values from the wall, mean only to detect if its ruigth or left. PReviously, in editor do some toests to find a good vector to "diagonaly jump" to each side.
Did i explained well? Whas that your problem? or what?
Respond, Good luck!
thank you bro, okay first i want to make it clear what i actually want to do, i want to make wall jump like in 2d games here the video it's very short 4 seconds please watch it, https://www.youtube.com/watch?v=qgYP3hbQzz4
about that right left thing it is not a problem i am using OnEnterCollision and apply forces on wall normal vector also one force in upward direction.it is working problem is with each collision it looks like forces is increasing and cube is acting like those old arcade ping pong games for your better understanding video link of my scene and problem again very short so must watch https://www.youtube.com/watch?v=g2hP4QYQJ2w
$$anonymous$$y code for that
void OnCollisionEnter(Collision collision)
{
foreach (ContactPoint contact in collision.contacts)
{
if (!IsGrounded && contact.normal.y < 0.1f)
{
Debug.DrawRay(contact.point, contact.normal, Color.yellow, 5f);
if (walljump == true)
{
characterControl.RIGIDBODY.AddForce(contact.normal * 6f, Force$$anonymous$$ode.Impulse);
characterControl.RIGIDBODY.AddForce(Vector3.up * 3f, Force$$anonymous$$ode.Impulse);
}
}
}
}
and that viberation in is due to screen recorder don't $$anonymous$$d that sorry
You should set the rigidbody.velocity to zero when it hits the other wall,and then apply the force for the following jump. and if your player is a ball you should set your rigidbody.angularVelocity to zero. See below
rigidbody.velocity = Vector3.zero;
rigidbody.angularVelocity = Vector3.zero;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How can I make a physics base projectile move along the path of a sphere? 1 Answer
How would I get collision at different speeds? 2 Answers
Physics.OverLapSphere is not working Please help me to fix it or replace it 0 Answers
Storing gameObjects in an array and essentially printing them? 1 Answer