- Home /
Question by
mountainfire · May 14, 2014 at 02:35 AM ·
timerspeedpickupaffect
Speed Affect Timer
I have a script in c# that allows the user to pick up a speed boost. The script works fine, I can pick up the item, and my characters speed increases appropriately. However, the effect is permanent, and I only want the effect to last for a certain amount of time before defaulting back to 500. Here is the portion I currently have;
if (other.gameObject.tag == "speed")
{
other.gameObject.SetActive(false);
AudioSource.PlayClipAtPoint(hit_power_fist, new Vector3(0,0,0));
speed = 1000;
}
as for the entire character controller, I have this, incase it helps;
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class PlayerController : MonoBehaviour
{
public float speed;
public GUIText countText;
public GUIText winText;
private int count;
public AudioClip hit_power_fist;
void start ()
{
count = 0;
SetCountText();
winText.text = "";
countText.text = "0"; }
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);
}
void OnTriggerEnter (Collider other)
{
if(other.gameObject.tag == "Pickup")
{
other.gameObject.SetActive(false);
AudioSource.PlayClipAtPoint(hit_power_fist, new Vector3(0,0,0));
count = count + 1;
SetCountText();
}
if (other.gameObject.tag == "speed")
{
other.gameObject.SetActive(false);
AudioSource.PlayClipAtPoint(hit_power_fist, new Vector3(0,0,0));
speed = 1000;
}
}
void SetCountText()
{
countText.text = "Score: " + count.ToString();
if (count >= 42) {
winText.text = "You Win!!";
}
}
}
Comment
Best Answer
Answer by robertbu · May 14, 2014 at 02:39 AM
One simple solution is to use Invoke. Create a function:
void ResetSpeed() {
speed = 500;
}
Then just below line 40, put:
Invoke("ResetSpeed", 10.0f);
This will restore the speed in 10 seconds.
Your answer
Follow this Question
Related Questions
Timescale not affect timer? (C#) 2 Answers
Problem with speed boost 2 Answers
Destroy trigger after a certain time 2 Answers
Having a speed counter 2 Answers