So I am pretty new to C# coding so this may be completely wrong, but here goes. When i try to walk into a wall, my player starts jittering and i hjave no ida how to solve it. My code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
public float speed;
private Vector2 direction;
private Rigidbody2D rb;
void Start() {
rb = this.GetComponent<Rigidbody2D>();
}
void Update() {
GetInput();
Move();
}
public void GetInput() {
direction = Vector2.zero;
if (Input.GetKey(KeyCode.W)) {
direction += Vector2.up;
}
if (Input.GetKey(KeyCode.S)) {
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.A)) {
direction += Vector2.left;
}
if (Input.GetKey(KeyCode.D)) {
direction += Vector2.right;
}
}
public void Move() {
transform.Translate(direction * speed * Time.deltaTime);
}
}
If anyone can spot how to fix it, that’ll be great!
Thanks!
EDIT: I fixed it using rigidbody2d.addforce. Weirdly, I have to multiply my speed by 1000 so i just created another variable and let the script multiply it by itself.