Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FreddyFryd · Mar 12, 2019 at 08:47 PM · scripting problemanimatortriggerscripting beginnerfps controller

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

210 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I change animation key frame property in script? 1 Answer

How to trigger animaton on two different Game Object's at the same time? 1 Answer

OnTriggerEnter2D crashing editor 0 Answers

Animation Trigger is saved even if it is not used immediately. 1 Answer

Collison detection not working? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges