I’ve been working on a 2D RPG game in Unity, and for some reason, whenever my character runs into a wall, the camera begins to shake violently. I have a RigidBody2D and a BoxCollider2D connected to him and a TilemapCollider connected to my Tilemap. The RigidBody component is Dynamic with a gravity scale of 0 (because it’s a top-down game). Also, the camera is a child of the player. Here’s the script to my character controller:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlayerController : MonoBehaviour
{
// Variable for the player speed and animation
float speed = 2f;
private Animator animator;
private Vector3 change;
// Called before the first frame is loaded
void Start()
{
// Gets animator component
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
// Detects change in the player's coordinates
change = Vector2.zero;
change.x = Input.GetAxisRaw("Horizontal");
change.y = Input.GetAxisRaw("Vertical");
if (change != Vector3.zero)
{
// Detects whether the player's animation should play or not
MoveCharacter();
animator.SetFloat("moveX", change.x);
animator.SetFloat("moveY", change.y);
animator.SetBool("moving", true);
}
else
{
animator.SetBool("moving", false);
}
}
void MoveCharacter()
{
// Allows movement on the X axis and calculations to allow the player to move
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(Vector2.right * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(-Vector2.right * speed * Time.deltaTime);
}
// Allows movement on the Y axis
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
{
transform.Translate(Vector2.up * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
{
transform.Translate(-Vector2.up * speed * Time.deltaTime);
}
}
}