- Home /
How can I get touch control buttons to move player in the same way as keyboard buttons do?
Hello all. I have 2 UI buttons that move my player UP and DOWN along the Y axis. I'm using CrossPlatformInputManager.GetAxis ("Verticle"); for the touch input and movement. My question is simply how can I alter or write a script that will move my player smoothly? Perhaps inputing MathF.Larp?
Keyboard button input moves my player very smoothly and I cant seem to do the same with the touch buttons.
you can use FixedUpdate(), or multiply speed by Time.deltaTime.
not sure how to implement what you're saying
Can I see your code please? I can probably implement it for you!
Answer by ImpOfThePerverse · May 15, 2018 at 04:52 PM
I adapted the following code from an answer I posted in another thread where someone wanted to get smooth movement when triggering a camera pan when the mouse was at either edge of the screen, but it should be re-useable here. Pass the function your calculated directionY, and where you set rb.velocity, multiply moveSpeed by the result it returns.
public float timeToFullSpeed;
float speedTimer;
float GetSpeedMultiplier(float directionY)
{
if (directionY > Mathf.Epsilon)
{
speedTimer = Mathf.Min(timeToFullSpeed, speedTimer + Time.deltaTime);
}
else if (directionY < -Mathf.Epsilon)
{
speedTimer = Mathf.Max(-timeToFullSpeed, speedTimer - Time.deltaTime);
}
else
{
if (speedTimer > 0)
{
speedTimer = Mathf.Max(0f, speedTimer - Time.deltaTime)
}
else
{
speedTimer = Mathf.Min(0f, speedTimer + Time.deltaTime)
}
}
return speedTimer / timeToFullSpeed;
}
hey man, not sure how to implement your code into my code. Can you possibly take a look at my code here:
float directionY; public float moveSpeed; Rigidbody2D rb;
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
void Update () {
directionY = CrossPlatformInput$$anonymous$$anager.GetAxis ("Verticle");
}
void FixedUpdate()
{
transform.position = new Vector3(transform.position.x,
$$anonymous$$athf.Clamp( transform.position.y, -3.479f, 7.25f),
transform.position.z);
rb.velocity = new Vector2(rb.velocity.x, directionY * moveSpeed);
}
}
This should do it. Just copy/paste the function and variables I posted into your class, and modify your code as follows:
void Start () {
rb = GetComponent<Rigidbody2D> ();
}
void Update () {
directionY = CrossPlatformInput$$anonymous$$anager.GetAxis ("Verticle");
}
void FixedUpdate()
{
transform.position = new Vector3(transform.position.x,
$$anonymous$$athf.Clamp( transform.position.y, -3.479f, 7.25f),
transform.position.z);
// calculate the speed multiplier here from directionY
float speed$$anonymous$$ultiplier = GetSpeed$$anonymous$$ultiplier(directionY);
// use the speed$$anonymous$$ultiplier in place of directionY
rb.velocity = new Vector2(rb.velocity.x, speed$$anonymous$$ultiplier * moveSpeed);
}
// paste in the GetSpeed$$anonymous$$ultiplier code here
float GetSpeed$$anonymous$$ultiplier...
}
dude, thank you SO much, this worked perfectly!
Your answer
Follow this Question
Related Questions
CrossplatformInput not detecting MultiTouch on Android build 1 Answer
Smooth Camera Movement script with problem. 0 Answers
store the user touch input 0 Answers
Move along grid where facing 1 Answer