Call animation only once?
Hello everybody! I have a problem and I hope somebody could help me here...
I have a script that calls an animation on a gameObject (its a rack door that opens up and should stay open). The thing is, the opening animation loops, even though I:
set wrap mode to once
uncheck Loop
put a line with the wrap mode into my script
I even created an idle animation for the door in its open position, but nothing helps.
Could it be because of the script? the trigger gets called every frame and thats why the aniamtion loops? What can I do to make it play only once? heres my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class raycastforward : MonoBehaviour
{
private GameObject _Rack_Door_Angle;
new Animation animation;
public GameObject otherObject;
Animator otherAnimator;
void Start()
{
animation = GameObject.Find("_Rack_Door_Angle").GetComponent<Animation>();
}
void Update ()
{
{
RaycastHit hit;
float theDistance;
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
if (Physics.Raycast(transform.position, (forward), out hit))
{
theDistance = hit.distance;
print(theDistance + " " + hit.collider.gameObject.name);
if (hit.collider.isTrigger)
{
otherObject.GetComponent<Animator>().SetTrigger("Move_Rack");
animation["_Rackdoor_Open"].wrapMode = WrapMode.Once;
animation.Play("_Rackdoor_Open");
}
}
}
}
}
Answer by RaphaelSindermann · May 29, 2018 at 01:13 PM
Well, found the anwser myself: I created the same script for the trigger and animated the trigger to get up. So, the door animation plays and afterwards the second script on the trigger gets activated and moves the trigger away. The door stays in place!
Script for the ray:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class raycastforward : MonoBehaviour
{
private GameObject _Rack_Door_Angle;
new Animation animation;
public GameObject otherObject;
Animation otherAnimator;
void Start()
{
animation = GameObject.Find("_Rack_Door_Angle").GetComponent<Animation>();
}
void Update()
{
{
RaycastHit hit;
float theDistance;
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
if (Physics.Raycast(transform.position, (forward), out hit))
{
theDistance = hit.distance;
print(theDistance + " " + hit.collider.gameObject.name);
if (hit.collider.isTrigger)
{
animation.Play("_Rackdoor_Open");
}
}
}
}
}
script for the trigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class targetUp : MonoBehaviour
{
private GameObject Raycast_target;
new Animation animation;
public GameObject otherObject;
Animation otherAnimator;
private void Start()
{
animation = GameObject.Find(name: "Raycast_target").GetComponent<Animation>();
}
void Update()
{
{
RaycastHit hit;
float theDistance;
Vector3 forward = transform.TransformDirection(Vector3.forward) * 10;
Debug.DrawRay(transform.position, forward, Color.green);
if (Physics.Raycast(transform.position, (forward), out hit))
{
theDistance = hit.distance;
print(theDistance + " " + hit.collider.gameObject.name);
if (hit.collider.isTrigger)
{
animation.Play("_Target_Up");
}
}
}
}
}
Your answer
Follow this Question
Related Questions
How can I fix this animation flick 0 Answers
Code error :/ 1 Answer
need help with simple timing code 1 Answer
Move to next animation with clicking the game object 0 Answers