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;
}
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)
@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?//
Will this work on my humanoid character ? Can you create the same code in C#? also my animations are in humanoid.
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
I get the error NullReferenceException: Object reference not set to an instance of an object PlayAnimation.Awake () (at Assets/PlayAnimation.cs:10)
Erase:
Animator anim;
And paste it before of Awake function.
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.
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 ?
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.
I get the error unexpected symbol else and also parsing error
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
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
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 )
its very easy, a second and i will post your code with the things what you have to write.
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");
}
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?
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");
}
}
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 :
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.
But my character plays the animation but goes in the ground
This is because it doesnt have a rigidbody or charachter controller or... you didnt placed a terrain, see tutorials first, create things after.