- Home /
Question by
BBoffa · Apr 14, 2020 at 08:16 PM ·
movement2d-platformerrigidbody2dboxcollider2d
Character input works but gets dragged back for no reason
Im making a 2D platformer but my character is getting dragged back to the starting position it can only move 0.2 away from its place.
My character has box collider and Rigidbody2D but it doesnt fall to the ground which has a box collider on it. I only have one piece of code which consists of this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Jump();
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position = movement * Time.deltaTime * moveSpeed;
}
void Jump()
{
if(Input.GetButtonDown("Jump"))
{
gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 5f), ForceMode2D.Impulse);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Rigid Body Movement (Mobile Issue) 3 Answers
2D Physics Problem? 1 Answer
Player sprite clipping through game boundary in 2d scene 1 Answer
Newest Unity version bug? (Unity 2019.3.0a4) 0 Answers
2D Colliders bug. 1 Answer