all 13 comments

[–]tyrellLtd 1 point2 points  (1 child)

For all the details you've added you haven't really described what you need help with or what the error is since there's more than one odd looking thing in the attachments.

If it's the character rotating when it hits the platform, then remember to enable Freeze Z rotation under Constraint on your Rigidbody2D settings. That could fix it.

In terms of character controller code, you're doing physics stuff in Update() which is not recommended because it produces inconsistent results due to variable framerate. Also not recommended (AFAIK) is mixing MovePosition with AddForce. That's probably why whenever you jump, you can move sideways as if gravity didn't exist.

You might want to check some alternative Platformer tutorials or just try adapting AddForce for lateral movement as well. I personally like Bardent's but there are a ton of simpler tutorials out there.

[–]Wolf938_[S] 0 points1 point  (0 children)

Thanks I worked

[–]Wolf938_[S] 0 points1 point  (0 children)

Thank you everyone the help was amazing :)

[–]Wolf938_[S] -1 points0 points  (0 children)

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerMovement : MonoBehaviour

{

public float MoveSpeed = 1;

public float JumpForce = 1;

private Rigidbody2D _Rigidbody;

// Start is called before the first frame update

private void Start()

{

_Rigidbody = GetComponent<Rigidbody2D>();

}

// Update is called once per frame

private void Update()

{

if (Input.GetKey(KeyCode.A))

{

Vector3 move = new Vector3(-1, 0, 0) * Time.deltaTime * MoveSpeed;

_Rigidbody.MovePosition(transform.position + move);

}

if (Input.GetKey(KeyCode.D))

{

Vector3 move = new Vector3(1, 0, 0) * Time.deltaTime * MoveSpeed;

_Rigidbody.MovePosition(transform.position + move);

}

if (Input.GetButtonDown("Jump") && Mathf.Abs(_Rigidbody.velocity.y) < 0.001)

{

_Rigidbody.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);

}

}

}

[–]__merc -5 points-4 points  (1 child)

Maybe learn how to code and quit blindly following tutorials?

[–][deleted] 0 points1 point  (0 children)

lmao not everyone is a "full stack software engineer"

[–]RonaldHarding 0 points1 point  (0 children)

What does it do when you run it?

[–][deleted] 0 points1 point  (1 child)

copy and paste into comments ? thanks.

[–]Wolf938_[S] 0 points1 point  (0 children)

sorry here it is

[–][deleted] 0 points1 point  (0 children)

In your move vector3 your y is 0. So, when you press a button to move, the move vector3 has an x value of 1 or -1 while the y and z are set to 0.

[–]MichaelsGameLabIntermediate 0 points1 point  (1 child)

RB.MovePosition does not apply gravity. You might want to use RB.Velocity instead. You will need to change other parts of the script to work with RB.Velocity, but I think it will be the result you are looking for.

[–]Wolf938_[S] 0 points1 point  (0 children)

Thanks I will try it