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 /
  • Help Room /
avatar image
0
Question by danielisawinner · Apr 11, 2017 at 08:06 AM · animationcarexitenter

Enter and exit with animations

This is my enter and exit script what should i do to make my character play an animation clip while entering and exiting? My enter button is 'E' How can i edit the script to play an animation? How can i make my character open the car door and sit in it?


  var car : Transform;
  var player : Transform;
  var exitPoint : Transform;
  var doorTriggerLeft : Transform;
  var PlayerCamera : Camera;
  var CarCamera : Camera;
  var isPlayerVisible : boolean;
  
  
  function Update(){
      if (Input.GetButton("e")&& isPlayerVisible){
          // make player invisible and still standing
          player.gameObject.SetActiveRecursively(false);
          player.gameObject.active = false;
          // parent player to Exit Point
          player.parent = exitPoint.transform;
          player.transform.localPosition = Vector3(-1.5,0,0);
          // parent PlayerParent to car
          exitPoint.parent = car.transform;
          exitPoint.transform.localPosition = Vector3(-0.5,0,0);
          // Enable Car as controllabe object
          GameObject.Find("Car").GetComponent("CarController").enabled=true;
          GameObject.Find("Car").GetComponent("CarUserControl").enabled=true;
          PlayerCamera.enabled = false;
          CarCamera.enabled = true;
          }
          else
          {
      if (Input.GetKeyUp("f")){
          // make character visible again
          player.gameObject.SetActiveRecursively(true);
          player.gameObject.active = true;
          // unparent player from everything
          player.transform.parent = null;
          // parent Exit Point to door Trigger
          exitPoint.parent = doorTriggerLeft.transform;
          // disable car as controllable
          GameObject.Find("Car").GetComponent("CarController").enabled=false;
          GameObject.Find("Car").GetComponent("CarUserControl").enabled=false;
          PlayerCamera.enabled = true;
          CarCamera.enabled = false;
  
          }   
      }
  }
  
  function OnTriggerEnter(Player : Collider) {
      isPlayerVisible = true;
      }
  
  function OnTriggerExit(Player : Collider) {
      isPlayerVisible  = false;
      }






Comment
Add comment · Show 12
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 juan_poder_azure · Apr 11, 2017 at 10:03 AM 0
Share

for open the car door you can use iTween when you play the animation with a time delay, and for sit the player on the car you can use iTween too when the animation is playing.(see tutorials of iTween this is very usefull for change transforms from one point to another smoothly and do like animations but much more optimized)

But if you want to have the movements the most naturals you will have to use root motion on the player animation(go to the clip file and here you will see $$anonymous$$otion, here if you created one animation with root motion you can select root motion and the player will use the movement of the animation(if you created movement on the animation)

avatar image juan_poder_azure · Apr 11, 2017 at 01:46 PM 0
Share

@danielisawinner juan_poder_azure · 3 hours ago Assets/Enter.js(8,4): BCE0044: expecting EOF, found 'Animator'.

This is the error i get if i put a ':' after animator if i dont put this symbol ':' then i get a ton of errors

Then send me the code so i can see the problem, i work with C# you are in java this could be the problem what i writted in C# the code, but send me i want to see it.

 GetComponent<Animator>();
 
 //you writted this?//

 
avatar image danielisawinner juan_poder_azure · Apr 11, 2017 at 02:25 PM 0
Share

Will this work on my humanoid character ? Can you create the same code in C#? also my animations are in humanoid.

avatar image danielisawinner danielisawinner · Apr 11, 2017 at 02:45 PM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class EnterExit : $$anonymous$$onoBehaviour {
 
     public Transform vehicle;
     public Transform player;
     public Transform exitPoint;
     public Transform doorTriggerLeft;
     public Camera PlayerCamera;
     public Camera CarCamera;
     public bool isPlayerVisible;
 
 
     // Use this for initialization
     void Start () {
     }
 
     // Update is called once per frame
     void Update () {
         if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.E) && isPlayerVisible){
             //set player invisable
             player.gameObject.SetActive(false);
             //set the exit parent
             player.parent = exitPoint.transform;
             player.transform.localPosition = new Vector3(-1.5f,0.0f,0.0f);
             //parent player to car;
             exitPoint.parent = vehicle.transform;
             exitPoint.transform.localPosition = new Vector3(-0.5f,0.0f,0.0f);
             //enable driving
             (GameObject.Find("Car").GetComponent("CarController") as $$anonymous$$onoBehaviour).enabled = true;
             (GameObject.Find("Car").GetComponent("CarUserControl") as $$anonymous$$onoBehaviour).enabled = true;
             PlayerCamera.enabled = false;
             CarCamera.enabled = true;
             isPlayerVisible = false;
         }
         else{
             if(Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.F)){
                 //set player invisable
                 player.gameObject.SetActive(true);
                 //set the exit parent
                 player.transform.parent = null;
                 //parent player to car;
                 exitPoint.parent = vehicle.transform;
                 //enable walking
                 (GameObject.Find("Car").GetComponent("CarController") as $$anonymous$$onoBehaviour).enabled = false;
                 (GameObject.Find("Car").GetComponent("CarUserControl") as $$anonymous$$onoBehaviour).enabled = false;
                 PlayerCamera.enabled = true;
                 CarCamera.enabled = false;
                 isPlayerVisible = true;
             }
         }
     }
     void OnTriggerEnter(Collider Player){
         isPlayerVisible = true;
     }
 
     void OnTriggerExit(Collider Player){
         isPlayerVisible = false;
     }
 }


This is the code in C# can you help? @juan_poder_azure

avatar image danielisawinner juan_poder_azure · Apr 12, 2017 at 10:03 AM 0
Share

I get the error NullReferenceException: Object reference not set to an instance of an object PlayAnimation.Awake () (at Assets/PlayAnimation.cs:10)

avatar image juan_poder_azure danielisawinner · Apr 12, 2017 at 10:47 AM 0
Share

Erase:

 Animator anim;

And paste it before of Awake function.

avatar image juan_poder_azure · Apr 11, 2017 at 04:53 PM 0
Share

@danielisawinner

 Animator anim;

Paste this where all public values is.

 anim = GetComponent<Animator>();

Paste this on Start

 anim.Play("nameofstate");


And this where you push the E key.

avatar image danielisawinner juan_poder_azure · Apr 11, 2017 at 04:57 PM 0
Share

O$$anonymous$$ understood but all I have to do is create two new states name it enter and exit but the question that arises is do I have to link it with any other animations and will humanoid animations work ?

avatar image juan_poder_azure danielisawinner · Apr 11, 2017 at 07:41 PM 0
Share

The humanoid animation works allways on mecanim or Animator, its not necessary link with others if you play it manually via script like we are doing.

The links its only if you want to reproduce an animation after another, with a boolean and a full configuration on mecanim for reproduce the animations well, this is much more difficult, first do what i sayed and try to reproduce it via script with the code what i gave you.

avatar image danielisawinner juan_poder_azure · Apr 12, 2017 at 05:25 AM 0
Share

I get the error unexpected symbol else and also parsing error

avatar image danielisawinner danielisawinner · Apr 12, 2017 at 06:13 AM 0
Share

Can you please edit the script ? And also i want my character to play the animation so I have to create 2 states or do I have to create a new animator controller because whenever I click play it says that my door trigger left has no animator attached but a script is trying to access it. But I don't want my door trigger to play an animation. @juan_poder_azure

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by juan_poder_azure · Apr 11, 2017 at 09:14 AM

First you have to add an animator or animation component to the object what you want to animate(animator if its generic or humanoid animation the clip, animation if its a legacy clip, see all this on the clip rig avatar) when you aded the animator component then you have to assign it an avatar, use the one what comes with the clip, after this create an animator controller on the assets tab, after this you can open the animator window on window: animator.

After this you will see some states, create one with a name and assign it a clip.

After all this you can go to the script and use the next:

Animator anim;

void Awake () { anim = GetComponentAnimator(); } //here use the <> simbols on the Animator word, i cant write here this...//

function OnTriggerEnter(Player : Collider) { isPlayerVisible = true; anim.Play("nameofanimatorstateclip"); }

function OnTriggerExit(Player : Collider) { isPlayerVisible = false; anim.Play("nameofanimatorstatecliptwo"); }

Too you can use a boolean with the animator for end the clip at the right moment and transition between the two clips perfectly on the frame what you want, but this is more difficult, for this you will have to see some tutorials, here you have some good : http://unity3d.com/es/learn/tutorials/topics/animation/animator-controller

Comment
Add comment · Show 6 · 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 danielisawinner · Apr 11, 2017 at 09:29 AM 0
Share

1.But my character is humanoid and animations are also changed to humanoid 1. $$anonymous$$y character has an animator with animations. What should I do? 2. Also do I have to create a new script for the enter and exit animation? If not how can I edit my script? ($$anonymous$$y animations are downloadEd from mixamo) ($$anonymous$$y character is setup just like in this video https://youtu.be/wh5uA$$anonymous$$rT_so )

avatar image juan_poder_azure danielisawinner · Apr 11, 2017 at 09:34 AM 0
Share

its very easy, a second and i will post your code with the things what you have to write.

avatar image juan_poder_azure danielisawinner · Apr 11, 2017 at 09:40 AM 1
Share
 var car : Transform;
   var player : Transform;
   var exitPoint : Transform;
   var doorTriggerLeft : Transform;
   var PlayerCamera : Camera;
   var CarCamera : Camera;
   var isPlayerVisible : boolean;
   Animator anim;
   
  function Awake(){
 anim = GetComponent-Animator-();  //the - simbols are the simbols what i writted you on the other message, write the simbols of the other message not these -,   i cant write here these simbols//
 }
   
   function Update(){
       if (Input.GetButton("e")&& isPlayerVisible){
           // make player invisible and still standing
           player.gameObject.SetActiveRecursively(false);
           player.gameObject.active = false;
           // parent player to Exit Point
           player.parent = exitPoint.transform;
           player.transform.localPosition = Vector3(-1.5,0,0);
           // parent PlayerParent to car
           exitPoint.parent = car.transform;
           exitPoint.transform.localPosition = Vector3(-0.5,0,0);
           // Enable Car as controllabe object
           GameObject.Find("Car").GetComponent("CarController").enabled=true;
           GameObject.Find("Car").GetComponent("CarUserControl").enabled=true;
           PlayerCamera.enabled = false;
           CarCamera.enabled = true;
           }
           else
           {
       if (Input.Get$$anonymous$$eyUp("f")){
           // make character visible again
           player.gameObject.SetActiveRecursively(true);
           player.gameObject.active = true;
           // unparent player from everything
           player.transform.parent = null;
           // parent Exit Point to door Trigger
           exitPoint.parent = doorTriggerLeft.transform;
           // disable car as controllable
           GameObject.Find("Car").GetComponent("CarController").enabled=false;
           GameObject.Find("Car").GetComponent("CarUserControl").enabled=false;
           PlayerCamera.enabled = true;
           CarCamera.enabled = false;
   
           }   
       }
   }
   
   function OnTriggerEnter(Player : Collider) {
       isPlayerVisible = true;
       anim.Play("nameofanimatorstate");
       }
   
   function OnTriggerExit(Player : Collider) {
       isPlayerVisible = false;
       anim.Play("nameofanimatorstatetwo");
       }
avatar image danielisawinner juan_poder_azure · Apr 11, 2017 at 09:49 AM 0
Share

Will it work with the character from the video? SInce I am a noob does nameoftheanimatorstate mean name of the animations? Can you please tell me the steps to place the animations in the animator and do I have to link it with anything?

Show more comments
Show more comments
avatar image
0

Answer by danielisawinner · Apr 12, 2017 at 02:01 PM

My Player has an animator Controller called Animator 1

My animations are called Entering

Can you please edit these scripts?

using UnityEngine;

 using System.Collections;

   

 public class PlayAnimation : MonoBehaviour

 {

     Animator anim;

     // Use this for initialization

     void Awake()

     {

        

     }

   

     private void NewMethod()

     {

         anim.GetComponent<Animator>();

     }

   

     // Update is called once per frame

     void Update()

     {

         if (Input.GetKeyDown(KeyCode.A))

             anim.Play("Entering");

     }

 }





@juan_poder_azure

Comment
Add comment · Show 7 · 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 juan_poder_azure · Apr 12, 2017 at 03:32 PM 0
Share

Yes, without problems :)

 using System.Collections;
    
  public class PlayAnimation : $$anonymous$$onoBehaviour
  {
      Animator anim;
 
      void Awake()
      {
        anim = GetComponent<Animator>();     
      }
      void Update()
      {
          if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A))
              anim.Play("Entering");
      }
  }

Now will work but what you need is this :

link text

Study all lessons all by one and with special attention, after this try to reproduce by yourself one by one and do this until you can use all what its in all lessons in the same script, then you will know to write and understand code, this is how i learned scripting.

avatar image danielisawinner juan_poder_azure · Apr 12, 2017 at 03:34 PM 0
Share

But my character plays the animation but goes in the ground

avatar image juan_poder_azure danielisawinner · Apr 12, 2017 at 03:43 PM 0
Share

This is because it doesnt have a rigidbody or charachter controller or... you didnt placed a terrain, see tutorials first, create things after.

Show more comments
Show more comments

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

187 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

Related Questions

How to make realistic Car Driving animation 1 Answer

Gameobject not showing in different Android devices 2 Answers

Help with Enter/Exit modified for a turret or cannon in C# 1 Answer

Enter door? 1 Answer

how can I move a particle system along an animated model? 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