- Home /
Player Passing Through Walls 2D
So my player is passing through walls. I have 2D colliders on both of them and after a lot of research I've discovered that using transform.Translate to move my character is what is causing the problem. However, I can't figure out how to move my character using AddForce or MovePosition. Everything I've tried has either messed up my animations or had really janky movement. Here's my code, help would be much appreciated:
using System;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
[SerializeField] float walkSpeed = 2.3f;
[SerializeField] float runSpeed = 5f;
[SerializeField] float jumpPower = 5f;
float speedFactor = 2.3f;
float moveSpeed;
bool isRunning = false;
bool onGround = false;
bool isJumping = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
SpriteRenderer playerSpriteRenderer;
Animator playerAnim;
Rigidbody2D rigidBody;
void Start()
{
playerSpriteRenderer = GetComponent <SpriteRenderer>();
playerAnim = gameObject.GetComponent<Animator>();
rigidBody = GetComponent<Rigidbody2D>();
}
void Update()
{
FlipXAxisOnMove();
}
void FixedUpdate()
{
MoveHorizontally();
Jump();
GetPlayerVelocity();
}
void GetPlayerVelocity()
{
playerAnim.SetFloat("vSpeed", rigidBody.velocity.y);
}
void Jump()
{
onGround = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
playerAnim.SetBool("onGround", onGround);
if (onGround && Input.GetButtonDown("Jump"))
{
playerAnim.SetBool("isJumping", isJumping);
rigidBody.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
}
}
void MoveHorizontally()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
speedFactor = runSpeed;
isRunning = true;
playerAnim.SetBool("isRunning", isRunning);
}
else if (Input.GetKeyUp(KeyCode.LeftShift))
{
speedFactor = walkSpeed;
isRunning = false;
playerAnim.SetBool("isRunning", isRunning);
}
moveSpeed = Input.GetAxis("Horizontal") * Time.deltaTime * speedFactor;
transform.Translate(moveSpeed, 0, 0);
playerAnim.SetFloat("moveSpeed", Mathf.Abs(moveSpeed));
}
void FlipXAxisOnMove()
{
if (playerSpriteRenderer != null)
{
if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow))
{
playerSpriteRenderer.flipX = true;
}
else if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow))
{
playerSpriteRenderer.flipX = false;
}
}
}
}
Answer by fluxhackspro · Jun 11, 2018 at 08:00 PM
Well you can use GetAxis to get a value on x and then set it as a vector2. Then use Rigidbody2D.velocity to move the character. I've used it before and it works pretty nice.
here is a example code from unitys tutorials:
void FixedUpdate ()
{
float x = Input.GetAxis ("Horizontal");
Vector2 move = new Vector2 (x * speed, rb.velocity.y);
rb.velocity = move;
}
where rb is Rigidbody2D and speed is a set float of how fast you want to move. Try this and i hope it works out for you
~Flux
Your answer
Follow this Question
Related Questions
character shaking when it moves 0 Answers
Cannot get my character to rotate on Z axis. --Top Down Character View 0 Answers
How can I apply the dash movement? (Topdown 2D game) 0 Answers
Weird velocities (bug?) when moving character in unity 2D, 1 Answer
How to send collision points to the physics engine? 2 Answers