- Home /
Rigidbody2D stuck on Box Colliders
I have a camera with a box collider and Rigidbody2D (The character). This camera contains the following script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMover : MonoBehaviour
{
public float speed; // set to 5 in editor
public float accelleration; // set to 3 in editor
public float jumpSpeed; // set to 10 in editor
public float sprintMultiplier; // set to 2 in editor
Vector3 tempVelocity;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
tempVelocity.Set(0, jumpSpeed, 0);
this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
}
else
{
if(Input.GetKeyDown("a") && !Input.GetKey("d"))
{
tempVelocity.Set(-speed, 0, 0);
this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
}
if(Input.GetKeyDown("d") && !Input.GetKey("a"))
{
tempVelocity.Set(speed, 0, 0);
this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
}
if(Input.GetKey("a") && !Input.GetKey("d"))
{
tempVelocity.Set(-accelleration * Time.deltaTime, 0, 0);
this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
}
if(Input.GetKey("d") && !Input.GetKey("a"))
{
tempVelocity.Set(accelleration * Time.deltaTime, 0, 0);
this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
}
if(Input.GetKeyUp("a") || Input.GetKeyUp("d"))
{
tempVelocity.Set(-this.GetComponent<Rigidbody2D>().velocity.x, 0, 0);
this.GetComponent<Rigidbody2D>().AddForce(tempVelocity, ForceMode2D.Impulse);
}
}
if(Input.GetKeyDown(KeyCode.LeftShift))
{
speed *= sprintMultiplier;
accelleration *= sprintMultiplier;
}
if(Input.GetKeyUp(KeyCode.LeftShift))
{
speed /= sprintMultiplier;
accelleration /= sprintMultiplier;
}
}
}
After falling a short distance, the character collides with one or more sprites that I have created which also contain box colliders in the 2D variant. When moving left or right using the "a" and "d" keys, the character is occasionally stopped, but only while colliding with the sprites. What could be changed in order for the character to keep moving while colliding with these other sprites?
Apologies if the content of this question is difficult to read, this is my first question on this forum.
Hi @Christiansquare,
I think I understand, but need a bit of clarification. You'd like the player to collide with the sprites, but push them aside? Or would you prefer to have the player pass through the sprites?
-Jason
Answer by Christiansquare · Jan 23, 2020 at 05:16 PM
With a constant velocity (without using acceleration as in the script above), the character no longer gets stuck.
Answer by mscardinal · Jan 18, 2020 at 06:34 PM
@Christiansquare Looks like an easy to fix problem. The collider of the object that stops the caracter from moving has to be set to trigger.
Doing that causes the character to not physically collide with the objects. I would like for the character to collide with the objects. However, the character gets stuck as if it is against a wall when moving on top of the objects and there is no wall. How may I post a video to this forum?
i dont know how to post a video. But what are you actually trying to do ? I think some pictures could help.
Here are some pictures: https://imgur.com/a/eTf0X$$anonymous$$0 Note that while the "stopped" velocity is not 0, the character does not move at all.
Your answer
Follow this Question
Related Questions
Problem with 2 Rigidbodies having respective colliders (2d) 1 Answer
Rigidbody2D linearDrag - Suddenly Change 0 Answers
Enemy AI 2D not working? ( Rigidbody.Addforce() ) 2 Answers
How do i make my grounded variable change as I hit the platform and as I jump? 2 Answers
Rigidbody2d.AddForce behaving odd 1 Answer