- Home /
The player randomly freezes in place while other objects move ingame
So i've made a script a long while back and then i left it be while i was making my game, now it starts to bother me since i cant publish a game with errors like that, so basically my character freezes randomly in place when holding a or d in my 2D game.
Heres the code : using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
// Floats
public float speed;
public float jumpforce;
private float moveInput;
public float CheckRadius;
// References
private Rigidbody2D rb;
public Transform groundCheck;
public LayerMask whatIsGround;
public LayerMask wallLayerMask;
public Transform wallCheckPoint;
// bools
private bool isGrounded;
private bool facingRight;
public bool wallSliding;
public bool wallCheck;
// ints
private int extraJumps;
public int extraJumpValue;
void Start()
{
facingRight = true;
extraJumps = extraJumpValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, CheckRadius, whatIsGround);
float horizontal = Input.GetAxis("Horizontal");
moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);
Flip(horizontal);
}
void Update()
{
if(isGrounded == true)
{
extraJumps = extraJumpValue;
}
if (Input.GetKeyDown(KeyCode.W) && extraJumps > 0 && !wallSliding)
{
rb.velocity = Vector2.up * jumpforce;
extraJumps--;
}
else if(Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpforce;
}
if (!isGrounded)
{
wallCheck = Physics2D.OverlapCircle(wallCheckPoint.position, 0.1f, wallLayerMask);
if (facingRight && Input.GetAxis("Horizontal") > 0.1f || !facingRight && Input.GetAxis("Horizontal") < 0.1f);
{
if(wallCheck)
{
HandleWallSliding();
}
}
}
if(wallCheck == false || isGrounded)
{
wallSliding = false;
}
}
void HandleWallSliding()
{
rb.velocity = new Vector2(rb.velocity.x, -0.7f);
wallSliding = true;
if (Input.GetKeyDown(KeyCode.W))
{
if(facingRight)
{
rb.AddForce(new Vector2(-1.5f, 3) * jumpforce);
}
else
{
rb.AddForce(new Vector2(1.5f, 3) * jumpforce);
}
}
}
private void Flip(float horizontal)
{
if(horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}
i've set the layers, worked with changing colliders, nothing, it randomly freezes sometimes and i don't know how to fix it :(
If you use " rb.velocity = new Vector2(moveInput speed, rb.velocity.y);" the player will freeze because you aren't adding onthe velocity. IE: rb.velocity += new Vector2(moveInput speed, rb.velocity.y);
Answer by suIly · Aug 06, 2019 at 01:38 PM
If you use " rb.velocity = new Vector2(moveInput speed, rb.velocity.y);" the player will freeze because you aren't adding onthe velocity. IE: rb.velocity += new Vector2(moveInput speed, rb.velocity.y);
Your answer
Follow this Question
Related Questions
In my 2D platformer game, how would I create a height marker?, 1 Answer
How to check if an object hits the ground hard enough then add explosive force around it (2D) 1 Answer
Jumping from special jump orb 1 Answer
Problem with Falling Platform Script 3 Answers
Problem with 2D movement. My character is moving by himself between two points... :) 1 Answer