Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Door open with RayCast

Discussion in 'Scripting' started by Harardin, Aug 11, 2015.

  1. Harardin

    Harardin

    Joined:
    Aug 5, 2015
    Posts:
    58
    Hi I made this to simple scripts it’s not ideal don’t know how to make door close when use Input.KeyDown on it again but open state works good, for this in Animation Controller I used Triggers.

    Here is RayCast script it go on a camera or else.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DoorRayCast : MonoBehaviour
    5. {
    6.     public float Doordistance = 1.5f;
    7.     // Use this for initialization
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void FixedUpdate()
    15.     {
    16.         if (Input.GetButtonDown("actButton"))
    17.         {
    18.             CheckRayCast();
    19.         }
    20.  
    21.     }
    22.     private void CheckRayCast()
    23.     {
    24.         Ray ray = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0));
    25.         RaycastHit data;
    26.         if (Physics.Raycast(ray, out data, Doordistance))
    27.         {
    28.             GetComponent<DoorsActAnim>();
    29.         }
    30.     }
    31. }
    And Scrip that starts animation when Input button is presed

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DoorsActAnim : MonoBehaviour {
    5.     private Animator DoorAnim;
    6.     private AnimatorStateInfo BaseLayer;
    7.     private AnimatorStateInfo currentBaseState;
    8.     static int idleDoor = Animator.StringToHash("Base Layer.apartment_door_idle");
    9.     static int openDoor = Animator.StringToHash("Base Layer.apartment_door_open");
    10.     static int closeDoor = Animator.StringToHash("Base Layer.apartment_door_close");
    11.     public float Doordistance = 1.5f;
    12.  
    13.     // Use this for initialization
    14.     void Start()
    15.     {
    16.         DoorAnim = GetComponent<Animator>();
    17.     }
    18.     void FixedUpdate () {
    19.         currentBaseState = DoorAnim.GetCurrentAnimatorStateInfo(0);
    20.         if (Input.GetButtonDown("actButton"))
    21.         {
    22.             DoorAnim.SetBool("Open", true);
    23.         }
    24.         else if (Input.GetButtonDown("actButton"))
    25.         {
    26.             DoorAnim.SetBool("Close", true);
    27.         }
    28.     }
    29.    
    30.     }
    31.  
     
  2. Uberpete

    Uberpete

    Joined:
    Jan 16, 2014
    Posts:
    78
    You need to set the Animator "Open" bool to false when you close the door.

    Code (CSharp):
    1.       if (Input.GetButtonDown("actButton"))
    2.         {
    3.  
    4.            if (DoorAnim.GetBool ("Open") == false){
    5.             DoorAnim.SetBool("Open", true);
    6.             DoorAnim.SetBool("Close", false);
    7.            }
    8.  
    9.          else
    10.              {
    11.    
    12.             DoorAnim.SetBool("Open", false);
    13.             DoorAnim.SetBool("Close", true);
    14.  
    15.               }
    16.         }
    A tidier way to do this would be to use Animator Triggers