0

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);
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
Letholor
  • 175
  • 1
  • 3
  • 12
  • I can post the entire C# script for this class if needed. – Letholor Apr 02 '17 at 02:56
  • The `GetKey` function is used to get key press. It has nothing to do with UI images on the screen. What you need is a visual joystick. – Programmer Apr 02 '17 at 03:01
  • I am using a 2D platformer style of game. The joystick works good for a top-down situation but not so much when I'm needing animations, gravity, etc. to play with it. – Letholor Apr 02 '17 at 03:44
  • Visual joystick works with any game type including 2D platformer or FPS when you need to have a UI control the character or move an object on the screen. Please try what's in that answer first. If it does not work, create a new question with the code that doesn't work. CrossPlatformInputManager should do fine. – Programmer Apr 02 '17 at 03:52
  • I'm sorry for the frustration. I tried the video that was posted in the other stream. I will do more digging inside of the CrossPlatformInputManager documentation and see if I can figure things out. Thank you for your responses and showing me the proper direction to look in. It was getting very frustrating to not have the code work no matter what I tried. – Letholor Apr 02 '17 at 17:23
  • No problem. You can use the values from `CrossPlatformInputManager.GetAxis("Horizontal")` to move the player left and right. You can google how these works.... I modified your answer with an example. You can check that out from where it says **"EDIT"**. – Programmer Apr 02 '17 at 17:37
  • 1
    You're a genius @Programmer! Thank you for all of your help. It made a large difference. – Letholor Apr 02 '17 at 19:03

0 Answers0