Hey. How do I finish the game if the crystal is my final destination? I use OnTriggerEnter when my player collides with it, but nothing happens. It should show a short animation, whose trigger is “Win”. Instead, the animation starts as soon as I start the game and as I said, at the end pf my game when I reach the crystal, nothing happens. This is my player controller script: ( look at line 90)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
Animator anim; // Reference to the animator component.
float restartTimer;
public float restartDelay = 5f;
public Text countText;
public float walkSp = 2;
public float runSp = 6;
public float jumpH = 3;
[Range(0, 1)]
public float grav = -13;
public float turnSmoothTime = 0.2f;
float turnSmoothVelocity;
private int count;
public Transform coinEffect;
public float speedSmoothTime = 0.1f;
float Smooth;
float cSpeed;
float speedY;
Animator animator;
Transform camera;
CharacterController controller;
void Start()
{
animator = GetComponent<Animator>();
camera = Camera.main.transform;
controller = GetComponent<CharacterController>();
count = 0;
SetCountText();
}
void Update()
{
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 inputDir = input.normalized;
if (inputDir != Vector2.zero)
{
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + camera.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, turnSmoothTime);
}
bool run = Input.GetKey(KeyCode.LeftShift);
if (Input.GetKeyDown(KeyCode.Space))
{
if (controller.isGrounded)
{
float jumpVelocity = Mathf.Sqrt(-2 * grav * jumpH);
speedY = jumpVelocity;
}
}
float targetSpeed = ((run) ? runSp : walkSp) * inputDir.magnitude;
cSpeed = Mathf.SmoothDamp(cSpeed, targetSpeed, ref Smooth, speedSmoothTime);
speedY += Time.deltaTime * grav;
Vector3 velocity = transform.forward * cSpeed + Vector3.up * speedY;
controller.Move(velocity * Time.deltaTime);
float animationSpeedPercent = ((run) ? 1 : .5f) * inputDir.magnitude;
animator.SetFloat("speedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
if (controller.isGrounded)
{
speedY = 0;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Sphere"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText();
Transform effect = Instantiate(coinEffect, transform.position, transform.rotation) as Transform;
}
else if (other.gameObject.CompareTag("Crystal"))
{
restartTimer += Time.deltaTime;
anim.SetTrigger("Win");
if (restartTimer >= restartDelay)
{
Application.LoadLevel(Application.loadedLevel);
}
}
}
void SetCountText()
{
countText.text = "Collectibles : " + count.ToString();
}
}