This question was
closed Dec 03, 2017 at 08:53 AM by
JungOmul for the following reason:
Fixed.
Animation Problem
Hi. I made a script for my door. What I want is Press E -> Open Door-> Stay Opened -> Press E -> Close Door -> Stay Closed But my script makes the door opens and then closes right after... Plz help here is my script.
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;
bool isOpened;
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, 50.0f))
{
if (hit.collider.CompareTag ("Door"))
{
if (isOpened == false)
{
doorOpen.clip = Door_Open;
doorOpen.Play ("Door_Open");
isOpened = true;
}
}
if(isOpened == true)
{
if(doorOpen.isPlaying)
{
doorClose.clip = Door_Close;
doorClose.PlayQueued ("Door_Close");
isOpened = false;
}
}
}
}
}
}
I changed my script but it's still the same.
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;
bool isOpened;
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, 50.0f)) {
if (hit.collider.CompareTag ("Door")) {
if (isOpened == false) {
doorOpen.clip = Door_Open;
doorOpen.Play ("Door_Open");
isOpened = true;
}
}
}
}
if(isOpened == true)
{
if(Input.GetKeyDown(KeyCode.E))
{
Ray ray = new Ray (fpsCam.transform.position, fpsCam.transform.forward);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, 50.0f))
{
if(hit.collider.CompareTag("Door"))
{
doorClose.clip = Door_Close;
doorClose.PlayQueued ("Door_Close");
isOpened = false;
}
}
}
}
}
}
----------------------------------------------------------------------------------------------------------------------------- Fixed. I just changed if -> else if. Here is my script.
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;
bool isOpened;
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, 50.0f))
{
if (hit.collider.CompareTag ("Door"))
{
if (isOpened == false) {
doorOpen.clip = Door_Open;
doorOpen.Play ("Door_Open");
isOpened = true;
}
}
else if (isOpened == true)
{
doorClose.clip = Door_Close;
doorClose.Play ("Door_Close");
isOpened = false;
}
}
}
}
}
Comment
Follow this Question
Related Questions
Making bool true on GetKeyDown and false once animation is done 2 Answers
If statement not working 1 Answer
Animation problem? 0 Answers
model 'flinch' or 'jerk' on hit 1 Answer
Onde encontro a pasta "projet's assets"? 0 Answers