Hey guys, I’ve made a PacMan and I’ve got a powerup that speeds you up. But what I need help with is that I only want it to last about 5 seconds then it returns to normal speed. The game is in 2D if that matters which I doubt it. Here is my script below.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PacmanMove : MonoBehaviour
{
public float speed = 0.4f;
Vector2 dest = Vector2.zero;
public Text countText;
private int count;
private Rigidbody2D rb;
void Start()
{
dest = transform.position;
rb = GetComponent<Rigidbody2D>();
count = 0;
SetCountText();
}
void FixedUpdate()
{
// Move closer to Destination
Vector2 p = Vector2.MoveTowards(transform.position, dest, speed);
GetComponent<Rigidbody2D>().MovePosition(p);
// Check for Input if not moving, this is also all the movement
if ((Vector2)transform.position == dest)
{
if (Input.GetKey(KeyCode.UpArrow) && valid(Vector2.up))
dest = (Vector2)transform.position + Vector2.up;
if (Input.GetKey(KeyCode.RightArrow) && valid(Vector2.right))
dest = (Vector2)transform.position + Vector2.right;
if (Input.GetKey(KeyCode.DownArrow) && valid(-Vector2.up))
dest = (Vector2)transform.position - Vector2.up;
if (Input.GetKey(KeyCode.LeftArrow) && valid(-Vector2.right))
dest = (Vector2)transform.position - Vector2.right;
}
// Animation Parameters
Vector2 dir = dest - (Vector2)transform.position;
GetComponent<Animator>().SetFloat("DirX", dir.x);
GetComponent<Animator>().SetFloat("DirY", dir.y);
}
bool valid(Vector2 dir)
{
// Cast Line from 'next to Pac-Man' to 'Pac-Man'
Vector2 pos = transform.position;
RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
return (hit.collider == GetComponent<Collider2D>());
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Pacdot"))
{
other.gameObject.SetActive(false);
count = count + 10;
SetCountText();
}
// if Pacman collides with "PowerUp" his speed becomes that
if (other.gameObject.CompareTag("PowerUp"))
{
speed = 2f;
}
}
//shows the score that you have from the PacDots
void SetCountText()
{
countText.text = "Count: " + count.ToString();
}
}