Fixed
Raycast to play animation
Hi. I'm making door that opens and closes when pressed E. I made Door open and close animations. But it doesn't seems to work.. please help me. I'm desperate. Here is my script. It doesn't make any error.
I fixed my script. I changed the origin of my raycast to my FPSControllerCamera and made my animation to Legacy. It will work just fine if I touch a little in my animations.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Doors : MonoBehaviour {
public Camera fpsCam;
public AnimationClip Door_Open;
Animation doorOpen;
public AnimationClip Door_Close;
Animation doorClose;
private bool isOpened = false;
void Start()
{
isOpened = false;
doorOpen = GetComponent<Animation> ();
doorClose = GetComponent<Animation> ();
}
void Update ()
{
if (Input.GetKeyDown (KeyCode.E))
{
Ray ray = new Ray (fpsCam.transform.position, fpsCam.transform.forward);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 30.0f))
{
if (hit.collider.CompareTag ("Door"))
{
if (!isOpened)
{
doorOpen.clip = Door_Open;
doorOpen.Play ();
isOpened = true;
}
}
{
if (isOpened)
{
doorClose.clip = Door_Close;
doorClose.Play ();
isOpened = false;
}
}
}
}
}
}
What "does not work"?
I've noticed you never set the value of isOpened
to true
or false
after playing the animation.
I made a animations that make door opens and closes. According to the script, when I press E, the raycast calculate distance of the door and the animation should play and the door opens. But nothing happens.
I set the bool by
if(!isOpened) and if(isOpened)
Do I have to set the bool after pressing E? If so, how should I edit the script?
I changed my script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Doors : $$anonymous$$onoBehaviour {
public AnimationClip Door_Open;
Animation doorOpen;
public AnimationClip Door_Close;
Animation doorClose;
private bool isOpened = false;
void Start()
{
isOpened = false;
doorOpen = GetComponent<Animation> ();
doorClose = GetComponent<Animation> ();
}
void Update ()
{
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E)) {
Ray ray = new Ray (transform.position, transform.forward);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 30.0f)) {
if (hit.collider.CompareTag ("Door")) {
if (!isOpened) {
doorOpen.clip = Door_Open;
doorOpen.Play ();
isOpened = true;
}
}
{
if (isOpened) {
doorClose.clip = Door_Close;
doorClose.Play ();
isOpened = false;
}
}
}
}
}
}
But the animation won't play.
To debug, add simple Logs inside your if
statements and check whether they are printed in your console :
if (Physics.Raycast (ray, out hit, 30.0f)) {
Debug.Log("Something hit : " + hit.collider.tag ) ;
if (hit.collider.CompareTag ("Door")) {
Debug.Log( "Door hit " + isOpened ) ;
if (!isOpened) {
doorOpen.clip = Door_Open;
doorOpen.Play ();
isOpened = true;
}
}
{
if (isOpened) {
doorClose.clip = Door_Close;
doorClose.Play ();
isOpened = false;
}
}
}
Answer by LoneWolfSwat · Dec 02, 2017 at 07:06 PM
I dont know very nice about "CompareTag" stuff, but you should just make instaead:
if(hit.collider.gameObject.tag == "Door") {
//open it
}
No, CompareTag is the efficient way to .... compare the tag of a gameobject. The problem does not come from this function.
I fixed my script. Changed the origin of raycast to my FPSControllerCamera and set the animations to legacy.