- Home /
Play animation of FPS player after hitting trigger
Hi there
I am in a pickle. Currently I am trying to figure out how to play an animation of my FPS controller after hitting a trigger. In other words.. I have a FPS controller and when the player walks through a door frame, it will trigger an animation of the FPS controller walking a few steps further into the room and then sit down. It all sounds simple enough, but I am new to Unity and scripting, and I just cant understand what I am doing wrong.
I have my box collider (doorframe) set in place with 'Is Trigger' checked. I have made the animation and set up the animator with a new parameter 'EnterPlay' and defined an empty default state, with a transition to my animation, which is set to 'true' under conditions.
This is the code I have for the script, which goes on the player. Its not working at all.. and when I play, the animation starts at once without being triggered.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerEnterTube : MonoBehaviour {
private Animator _animator;
// Use this for initialization
void Start () {
_animator = GetComponent<Animator>();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
_animator.SetBool("EnterPlay", true);
}
}
// Update is called once per frame
void Update () {
}
}
Can anyone please help me understand why this is not working?
Thank you in advance!
Answer by WarmedxMints · Mar 12, 2019 at 10:26 PM
You need to set the trigger on your door and the animation to start on your player. So in your player class, do this;
private Animator _anim;
private void Start()
{
_anim = GetComponent<Animator>();
}
public void RunAnimation()
{
_anim.SetBool("EnterPlay", true);
}
Then put a trigger collider on your door and have a script on it like this;
private void OnTriggerEnter(Collider other)
{
var player = other.GetComponent<MyPlayerClass>();
if (player == null)
return;
player.RunAnimation();
}
So when the player enters the doors trigger collider, the door will check if it is the player that entered, if it isn't then it will return and do nothing. If it is, it will call the method to start the animation on the player.
Answer by FreddyFryd · Mar 13, 2019 at 12:41 PM
Hi @WarmedxMints Thank you for your reply. I am still not quite sure what to do. I have added this script to my player:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerEnterAnimation : MonoBehaviour {
private Animator _anim;
public object PlayerEnterAnimation { get; internal set; }
// Use this for initialization
private void Start ()
{
_anim = GetComponent<Animator>();
}
// Update is called once per frame
public void Update ()
{
_anim.SetBool("FPSEnterTube", true);
}
internal void RunAnimation()
{
throw new NotImplementedException();
}
}
And then this script to my door:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorTrigger : MonoBehaviour {
// Use this for initialization
private void OnTriggerEnter(Collider other)
{
var player = other.GetComponent<PlayerEnterAnimation>();
if (player == null)
return;
player.RunAnimation();
}
// Update is called once per frame
void Update () {
}
}
And it just doesn't work. There is something I am doing wrong, but I just don't understand what. Would really appreciate some help.
Thank you so much in advance.