- Home /
how to gradually increase acceleration of a gameobject (cube etc) with "each state of joystick (left analog stick)" ?
I'm trying to accelerate a gameobject gradually with joystick but it should change with each state. For example if push half way the left stick it moves with half speed and full speed if i push left stick all the way. think about it 3rd person games like splinter cell , uncharted where character moves faster (from slow walk, walk, run , sprint etc) with each push of the movement stick.
I'm currently using this code that i found on this forum, which works fine but it accelerating depending on public variables , not "how you pushed joystick"
Any idea ?
public float speed = 0f;
public float maxSpeed = 10f;
public float forceMagnitude = 2f;
void FixedUpdate() {
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Move (h, v);
}
void Move(float h, float v)
{
if (v != 0 || h != 0)
{
movement.Set (h, 0f, v);
speed = Mathf.Min(speed + forceMagnitude * Time.deltaTime, maxSpeed);
}
else
{
speed = Mathf.Max(speed - forceMagnitude * Time.deltaTime * 1.5f, 0);
}
movement = movement.normalized * speed * Time.deltaTime;
playerRigidbody.MovePosition(transform.position + movement);
}
I'm not really able to help you much but here is a topic I found on figuring out the Joystick Distance from the center. https://stackoverflow.com/questions/29156351/finding-distance-between-analog-stick-center-and-current-position