How to apply speed boost to player on mobile game
Hi Guys,
I am trying to apply a speed boost to my player, at the moment i have my player running automatically after a 4 second delay, then everytime you touch the screen the release causes and boost to the players sprint.
the code i have written only applies boost as a one off, example MS(movement speed) =5 boost = 5 MS*BOOST = 25 but i want the boost to add +1 to the run and have gravity & deceleration to slow the player down
so it should work like this movement speed =5 each boost adds 1 untill a maximum has been reached and then it slows down when boost hasnt been activated back to starting movement speed.
this is the code i have so far
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement2 : MonoBehaviour {
public float movementSpeed = 0; //Do not adjust
public float boost = 5;
private Rigidbody myBody;
public float maxSpeed = 10;
public float Acceleration = 10;
public float Deceleration = 10;
public GameObject RaceTimer;
// Use this for initialization
void Awake ()
{
myBody = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
StartCoroutine(Move());
StartCoroutine(speedBoost());
}
IEnumerator Move()
{
yield return new WaitForSeconds(4f);
PlayerMove();
}
IEnumerator speedBoost()
{
yield return new WaitForSeconds(4f);
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
movementSpeed = movementSpeed + boost;
}
else
{
movementSpeed = 5f;
}
}
void PlayerMove()
{
myBody.velocity = new Vector3(movementSpeed, 0, 0f);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
RaceTimer.SetActive(false);
}
}
}
Answer by zakkaiokenx10 · Apr 09, 2019 at 08:13 AM
The only problem i see with your code is the fact that your setting the players speed to 5 if he does not touch the screen. in fact he can only touch the screen every 4 seconds.that doesnt give alot of time to touch the screen, maybe less than a frame?
maybe give a little time buffer for the players ability to tap.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement2 : MonoBehaviour {
public float movementSpeed = 0; //Do not adjust
public float boost = 5;
public float maxSpeed = 10;
public float Acceleration = 10;
public float Deceleration = 10;
public GameObject RaceTimer;
//tip: not specifying a modifier will make it private by default.
bool ActivatedBoost;
Rigidbody myBody;
// Use this for initialization
void Awake ()
{
myBody = GetComponent<Rigidbody>();
}
void FixedUpdate ()
{
//maybe you should look into invokerepeating?
StartCoroutine(Move());
StartCoroutine(speedBoost());
}
void Update() {
//you should only be checking for input in the update method.
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended) {
ActivatedBoost = true;
} //using an else here will break the code as i am setting ActivatedBoost to false in the speedboost method
}
IEnumerator Move()
{
yield return new WaitForSeconds(4f);
PlayerMove();
}
IEnumerator speedBoost()
{
//this will make a 4 second activation period
yield return new WaitForSeconds(4f);
if (ActivatedBoost)
{
movementSpeed = movementSpeed + boost;
ActivatedBoost = false;
} else {
movementSpeed = 5; //floats only need their f suffix when youre using periods (ie, 3.14f)
}
}
void PlayerMove()
{
myBody.velocity = new Vector3(movementSpeed, 0, 0f);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
RaceTimer.SetActive(false);
}
}
}
As a usual, I commented a few tip and points you may want to read. you can remove them when your done reading them.
Thank you for responding, i used your script but it isnt working, the boost doesnt happen, i need it to stack like your are playing a 100m sprint game on consol the more you press the button the faster the player goes, but with your script, it didnt sprint at all its for a athletics game and the player auto runs slow but then tap the screen and it starts to build up a sprint(boost).
thank you for the comments and tips they did help me.