Trying to make a door. Zoning or Load Level with Animation.
Hi i have been trying to make a door. That like skyrim. Opens then let's you zone into an house or castle or whatever.
I have been following like 3 tutorial at once but to no avail. here's what i have so far.
Interact
using UnityEngine; using System.Collections;
public class InteractScript : MonoBehaviour {
public float interactDistance = 5f;
void Update ()
{
if(Input.GetKeyDown(KeyCode.F))
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(Physics.Raycast(ray, out hit, interactDistance))
{
if(hit.collider.CompareTag("Door"))
{
hit.collider.transform.parent.GetComponent<DoorZoneScript>() .ChangeDoorState();
}
else if(hit.collider.CompareTag("NextLevelDoor"))
{
hit.collider.transform.parent.GetComponent<DoorZoneScript>() .ChangeDoorState();
Application.LoadLevel(0);
}
}
}
}
DoorScript
using UnityEngine; using System.Collections;
public class DoorZoneScript : MonoBehaviour {
public bool open = false;
public float doorOpenAngle = -15f;
public float DoorCloseAngle= 0f;
public float smooth = 2f;
public AudioClip open_door;
private AudioSource source;
private float volLowRange = .5f;
private float volHighRange = 1.0f;
void Awake ()
{
source = GetComponent<AudioSource> ();
}
public void ChangeDoorState()
{
open = !open;
if (open)
{
float vol = Random.Range (volLowRange, volHighRange);
source.PlayOneShot(open_door,vol);
}
}
void Update ()
{
if (open) //open = true
{
Quaternion targetRotation = Quaternion.Euler (0,doorOpenAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, smooth * Time.deltaTime);
}
else
{
Quaternion targetRotation2 = Quaternion.Euler (0,DoorCloseAngle, 0);
transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation2, smooth * Time.deltaTime);
}
}
}
Let me know if theres a way i can do.
Do i need to make the door script, Interact script, then make a trigger script for when the door opens?
I.E: Door is closed Approaches door Press F to enter Door Opens about 15f Zone in.
Answer by Yuchemey · Mar 06, 2016 at 11:00 AM
Keep in mind i am still very new at this.
I found a way to make the door zone like in skyrim. - $$anonymous$$ake a script of a door to make it rotate like 15~20 degrees - Create an object and make it a trigger, place it behind the door but make sure it is Behind the door so that when it opens it triggers the zoning. with OnTriggerEnter ()