This problem that I am having is related to a 2D game that I am working on. All of the other questions that I have seen asked are related to the Unity3D versions of games and tutorials. I don't understand why it isn't working for me.
In my script, I am trying to get my player to move left and right. It is strange to me because I can get the player to jump, fall down, and throw a snowball using the buttons that I created on the screen. For some reason, however, the player will not respond to the left button clicks and right button clicks that I placed on the screen. Any help is greatly appreciated, thank you in advance.
void Update () {
if (Input.GetKey(left))
{
MoveLeft();
}
else if (Input.GetKey(right))
{
MoveRight();
}
else
{
StopMoving();
}
public void MoveLeft()
{
theRB.velocity = new Vector2(-moveSpeed, theRB.velocity.y);
}
public void MoveRight()
{
theRB.velocity = new Vector2(moveSpeed, theRB.velocity.y);
}
public void StopMoving()
{
theRB.velocity = new Vector2(0, theRB.velocity.y);
}
EDIT:
This is what you have done:
if (CrossPlatformInputManager.GetAxis("Horizontal")>0){
MoveRight();
}
else if (CrossPlatformInputManager.GetAxis("Horizontal")<0){
MoveLeft();
}
else{
StopMoving();
}
Event better, you can pass the speed to the MoveXXX functions and multiple the value by moveSpeed
.
Example:
float horiAxis = CrossPlatformInputManager.GetAxis("Horizontal");
if (horiAxis > 0){
MoveRight(horiAxis);
}
public void MoveLeft(float val)
{
theRB.velocity = new Vector2(-moveSpeed * val, theRB.velocity.y);
}