Hello.
I am working on a 2D ‘Metroidvania’ style platformer and am seeking advice on character movement.
I have a strong programming background however movement vectors, forces and general physics based mathematics is definitely not my strong suit.
I do however have a good grasp on Unitys use of rigidbodies (both kinematic and not) and colliders and have experimented extensively with them. I have also tried implementing raycas to determine the surface normals direction and modify movement appropriately which worked to a certain extent.
What I am hoping to achieve:
- Fully interactive with the PhysX engine so gravity/pushing/being pushed around
- Able to smoothly traverse inclines and declines
- exact jump heights no matter the current velocity of the character as well as in-air ‘steering’
I just cant seem to settle on using forces, movePosition or velocity manipulation. Each seems to fall short on one of the above mentioned aspects (along with my understanding of movement physics).
All I am looking for is some pointers in what movement types to use and when to achieve the above listed points.
I can provide code if requested (its currently all a mess as i play around with it).
Thanks.
It would be best to use rigidbody velocities and turn off gravity until you reach the height you want then reenable it at the desired height. Or adjust gravity to allow you to the desired height.
First option you suddenly drop like a brick. Second option you get a floaty jump.
I can setup a simple example if needed, but wouldnt be for about an hour.
I do this and get consistent height jumps:
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, jumpSpeed);
Context:
if (Input.GetKeyDown(KeyCode.Space))
{
var distFromSide = 0.5f * transform.localScale.x;
var bottomOffsetAtSide = 0.3f * transform.localScale.y;
var distFromBottomAtSide = 0.3f * transform.localScale.y;
var bottomOffsetAtCenter = 0.51f * transform.localScale.y;
var distFromBottomAtCenter = 0.1f * transform.localScale.y;
if (enableWallJump)
{
distFromSide += 0.1f * transform.localScale.x;
}
var hitLeft = Physics2D.Raycast(new Vector2(transform.position.x - distFromSide, transform.position.y - bottomOffsetAtSide), -Vector2.up, distFromBottomAtSide);
var hitCenter = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y - bottomOffsetAtCenter), -Vector2.up, distFromBottomAtCenter);
var hitRight = Physics2D.Raycast(new Vector2(transform.position.x + distFromSide, transform.position.y - bottomOffsetAtSide), -Vector2.up, distFromBottomAtSide);
if (hitLeft.collider != null || hitCenter.collider != null || hitRight.collider != null)
{
_rigidbody.velocity = new Vector2(_rigidbody.velocity.x, jumpSpeed);
// _rigidbody.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
}
}
Reference:
Hmm that looks interesting, will have to play around with it. Thanks!
What I am looking for advice on specifically is to be able to control movement more through script but still be able to be affected by outside forces just as en explosion/moving object.
That way I can finely tune ground movement without the uncertainty of AddForce.
Basically I want to have my cake and eat ii :3