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.