- Home /
Rigid Body Movement (Mobile Issue)
Hey guys, I'm having some troubles getting movement to work on mobile. It works on the computer but not the phone. The code that I have for the computer is:
//Movement arrows
if (Input.GetKey(KeyCode.RightArrow))
r2d.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y, 0);
if (Input.GetKey(KeyCode.LeftArrow))
r2d.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y, 0);
That works, and here the code for the mobile that for some reason won't move the object:
if (Input.touchCount > 0)
{
var touch = Input.GetTouch(0);
if (touch.position.x < Screen.width / 2)
{
r2d.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y);
}
else if (touch.position.x > Screen.width / 2)
{
r2d.velocity = new Vector3(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y, 0);
}
}
If I do tranform.Translate instead for the touch part it works, but that removes the smoothness of the movement. If you guys have any idea let me know! Thanks a lot in advance. :-)
The mobile does not support keyboard input so you should change Input.GetAxis unless you are using a gamepad : r2d.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y);
to something like this :
r2d.velocity = new Vector2(SO$$anonymous$$EVALUEHERE* moveSpeed, r2d.velocity.y);
you have to check left or right yourself, and use $$anonymous$$athf.Lerp if you want to increment the value smoothly. Cheers!
Answer by rajan4uto · Dec 31, 2015 at 09:38 AM
Instead of "Input.GetAxis("Horizontal")" use 1 0r -1 according to your script. I mean "Input.GetAxis("Horizontal")" return 1 or -1(if you press left then it return -1 and vice-versa).
I have seen that you are working for mobile so i want to tell you one more thing that don't use rigidbody with gravity on better u should make your own code for physics or you can use "character controller" that will work fine in mobile. Main problem is that "graphics and physics are not synchronize" that's the reason in mobile, rigidbody look like it is vibrating if your camera is following it.
Answer by _Yash_ · Dec 31, 2015 at 06:11 AM
Try Replacing:
r2d.velocity = new Vector2(Input.GetAxis("Horizontal") * moveSpeed, r2d.velocity.y);
With:
r2d.velocity = new Vector2((touch.position.x - Screen.width / 2) * moveSpeed, r2d.velocity.y);
Answer by rajan4uto · Dec 31, 2015 at 06:55 AM
public float moveSpeed=6;
public Rigidbody r2d;
void Update()
{
if (Input.touchCount > 0) {
Debug.Log("yes");
var touch = Input.GetTouch (0);
if (touch.position.x < Screen.width / 2) {
r2d.velocity = new Vector2 (-1 * moveSpeed, r2d.velocity.y);
} else if (touch.position.x > Screen.width / 2) {
r2d.velocity = new Vector3 (1 * moveSpeed, r2d.velocity.y, 0);
}
else
{
r2d.velocity = new Vector3 (0 * moveSpeed, r2d.velocity.y, 0);
}
}
}
I found this as great one because Input.GetAxis("Horizontal") return 1 or -1 by pressing left or right key so in first case use -1 and in second 1. It will work.
You could create a two small transparent buttons using GUI or canvas and if they are pressed that is on mobile if the buttons are touched player move.It may work more ease
Your answer
Follow this Question
Related Questions
Runner 2D: Move Camera or Objects? 1 Answer
Why Rigidbody 2d doesnt stop sliding? 2 Answers
Why isn't my joystick code working? 1 Answer