I need help with my 2D movement script. I was previously using a transform modifier, but that means colliders don’t work. I switched to a rigidbody based system, and now my player won’t move. Where did I go wrong?
public class PlayerController : MonoBehaviour {
public float walkSpeed = 0.5f;
public float runSpeed = 2f;
public float crouchSpeed = .25f;
public float heightDifference = .43f;
Transform playerPos;
Rigidbody2D playerMove;
SpriteRenderer playerPicture;
BoxCollider2D playerBox;
public Sprite walkPicture;
public Sprite runPicture;
public Sprite crouchPicture;
float horMove = 0;
float vertMove = 0;
bool running;
bool crouch;
Vector2 movement;
void Start ()
{
playerPos = GetComponent<Transform>();
playerMove = GetComponent<Rigidbody2D>();
playerPicture = GetComponent<SpriteRenderer>();
playerBox = GetComponent<BoxCollider2D>();
}
void FixedUpdate ()
{
horMove = Input.GetAxis("Horizontal");
vertMove = Input.GetAxis("Vertical");
if(horMove != 0 && vertMove !=0)//Corrects for hyptonose
{
horMove = horMove * .707f;
vertMove = vertMove * .707f;
}
if (Input.GetKeyDown("left ctrl") || Input.GetKeyDown("right ctrl"))
{
if(crouch)
{
crouch = false;
playerMove.position = new Vector2(playerMove.position.x, playerMove.position.y + heightDifference);
}
else
{
crouch = true;
playerMove.position = new Vector2(playerMove.position.x, playerMove.position.y - heightDifference);
}
}
if (Input.GetAxis("Run") > 0 && !crouch)
{
running = true;
}
else
{
running = false;
}
if (running)
{
movement = new Vector2(horMove * runSpeed, vertMove * walkSpeed);
}
else if(crouch)
{
movement = new Vector2(horMove * crouchSpeed, vertMove * crouchSpeed);
}
else
{
movement = new Vector2(horMove * walkSpeed, vertMove * walkSpeed);
}
playerMove.velocity = movement;
playerPos.position = playerMove.position;
SpriteManager();
}
void SpriteManager() //Will be replaced by an animation manager
{
if (running && (horMove != 0 || vertMove != 0))
{
playerPicture.sprite = runPicture;
}
else if (crouch)
{
playerPicture.sprite = crouchPicture;
}
else
{
playerPicture.sprite = walkPicture;
}
if(Input.GetAxis("Horizontal")< 0)
{
playerPicture.flipX = true;
}
else
{
playerPicture.flipX = false;
}
if(crouch)
{
playerBox.size = new Vector2(1.76f, 2.5f);
}
else
{
playerBox.size = new Vector2(1.76f, 3.34f);
}
}
}
public class PlayerController : MonoBehaviour {
public float walkSpeed = 0.5f;
public float runSpeed = 2f;
public float crouchSpeed = .25f;
public float heightDifference = .43f;
Transform playerPos;
Rigidbody2D playerMove;
SpriteRenderer playerPicture;
BoxCollider2D playerBox;
public Sprite walkPicture;
public Sprite runPicture;
public Sprite crouchPicture;
float horMove = 0;
float vertMove = 0;
bool running;
bool crouch;
Vector2 movement;
void Start ()
{
playerPos = GetComponent<Transform>();
playerMove = GetComponent<Rigidbody2D>();
playerPicture = GetComponent<SpriteRenderer>();
playerBox = GetComponent<BoxCollider2D>();
}
void FixedUpdate ()
{
horMove = Input.GetAxis("Horizontal");
vertMove = Input.GetAxis("Vertical");
if(horMove != 0 && vertMove !=0)//Corrects for hyptonose
{
horMove = horMove * .707f;
vertMove = vertMove * .707f;
}
if (Input.GetKeyDown("left ctrl") || Input.GetKeyDown("right ctrl"))
{
if(crouch)
{
crouch = false;
playerMove.position = new Vector2(playerMove.position.x, playerMove.position.y + heightDifference);
}
else
{
crouch = true;
playerMove.position = new Vector2(playerMove.position.x, playerMove.position.y - heightDifference);
}
}
if (Input.GetAxis("Run") > 0 && !crouch)
{
running = true;
}
else
{
running = false;
}
if (running)
{
movement = new Vector2(horMove * runSpeed, vertMove * walkSpeed);
}
else if(crouch)
{
movement = new Vector2(horMove * crouchSpeed, vertMove * crouchSpeed);
}
else
{
movement = new Vector2(horMove * walkSpeed, vertMove * walkSpeed);
}
playerMove.velocity = movement;
playerPos.position = playerMove.position;
SpriteManager();
}
void SpriteManager() //Will be replaced by an animation manager
{
if (running && (horMove != 0 || vertMove != 0))
{
playerPicture.sprite = runPicture;
}
else if (crouch)
{
playerPicture.sprite = crouchPicture;
}
else
{
playerPicture.sprite = walkPicture;
}
if(Input.GetAxis("Horizontal")< 0)
{
playerPicture.flipX = true;
}
else
{
playerPicture.flipX = false;
}
if(crouch)
{
playerBox.size = new Vector2(1.76f, 2.5f);
}
else
{
playerBox.size = new Vector2(1.76f, 3.34f);
}
}
}
]
public class PlayerController : MonoBehaviour {
public float walkSpeed = 0.5f;
public float runSpeed = 2f;
public float crouchSpeed = .25f;
public float heightDifference = .43f;
Transform playerPos;
Rigidbody2D playerMove;
SpriteRenderer playerPicture;
BoxCollider2D playerBox;
public Sprite walkPicture;
public Sprite runPicture;
public Sprite crouchPicture;
float horMove = 0;
float vertMove = 0;
bool running;
bool crouch;
Vector2 movement;
void Start ()
{
playerPos = GetComponent<Transform>();
playerMove = GetComponent<Rigidbody2D>();
playerPicture = GetComponent<SpriteRenderer>();
playerBox = GetComponent<BoxCollider2D>();
}
void FixedUpdate ()
{
horMove = Input.GetAxis("Horizontal");
vertMove = Input.GetAxis("Vertical");
if(horMove != 0 && vertMove !=0)//Corrects for hyptonose
{
horMove = horMove * .707f;
vertMove = vertMove * .707f;
}
if (Input.GetKeyDown("left ctrl") || Input.GetKeyDown("right ctrl"))
{
if(crouch)
{
crouch = false;
playerMove.position = new Vector2(playerMove.position.x, playerMove.position.y + heightDifference);
}
else
{
crouch = true;
playerMove.position = new Vector2(playerMove.position.x, playerMove.position.y - heightDifference);
}
}
if (Input.GetAxis("Run") > 0 && !crouch)
{
running = true;
}
else
{
running = false;
}
if (running)
{
movement = new Vector2(horMove * runSpeed, vertMove * walkSpeed);
}
else if(crouch)
{
movement = new Vector2(horMove * crouchSpeed, vertMove * crouchSpeed);
}
else
{
movement = new Vector2(horMove * walkSpeed, vertMove * walkSpeed);
}
playerMove.velocity = movement;
playerPos.position = playerMove.position;
SpriteManager();
}
void SpriteManager() //Will be replaced by an animation manager
{
if (running && (horMove != 0 || vertMove != 0))
{
playerPicture.sprite = runPicture;
}
else if (crouch)
{
playerPicture.sprite = crouchPicture;
}
else
{
playerPicture.sprite = walkPicture;
}
if(Input.GetAxis("Horizontal")< 0)
{
playerPicture.flipX = true;
}
else
{
playerPicture.flipX = false;
}
if(crouch)
{
playerBox.size = new Vector2(1.76f, 2.5f);
}
else
{
playerBox.size = new Vector2(1.76f, 3.34f);
}
}
}