- Home /
Question by
ChoosingOwl · Nov 07, 2013 at 11:55 AM ·
animationcollisiontriggerraycasthitopen
Starting animation with collision
Im having trouble making this code work. Im no genius or experienced programmer in unity. Its based on this answer code http://answers.unity3d.com/questions/14361/open-and-close-a-door-with-a-keypress.html
All i want to do is when the player collides with the trigger, it start the animation on the Slider.
using UnityEngine;
using System.Collections;
public class Door : MonoBehaviour {
bool doorIsOpen = false;
GameObject currentDoor;
void DoDoor(bool openCheck, string SlideUp, GameObject thisDoor);
void Start () {
}
// Update is called once per frame
void Update () {
//If the trigger collides with our Player
if (collider.gameObject.name == "Player") {
//Creating a RaycastHit Variable called hit
RaycastHit hit;
//Throws the raycast 10 Unity Metres forward
if(Physics.Raycast (transform.position, transform.forward, hit, 10)){
//if the raycast collides with a gameobject called Slider1 and if the trigger is not trigged
if(hit.collider.gameObject.tag == "SliderUp" && doorIsOpen == false){
//store the gameobject of the collider hit
currentDoor = hit.collider.gameObject;
//call the Door() function with parameters for:
//DoorIsOpen, animation, currentDoor Gameobject
Door(true, "SlideUp", currentDoor);
}
}
}
}
//Start the slider function
void DoDoor(bool openCheck, string SlideUp, GameObject thisDoor){
//Sets the DoorIsOpen variable to true or false
doorIsOpen = openCheck;
//Plays the animation clip SlipUp
thisDoor.animation.Play("SlideUp");
}
Comment
Your answer
Follow this Question
Related Questions
How to Play an animation state on an animator controller on collision? 1 Answer
How to make my animated axe collide with tree properly?? 0 Answers
Animation with frozen player ?? 0 Answers
Possible to stop collider from "popping" through plane when re-centering? 2 Answers
Trying to play other objects' sound and animations from Raycast hit 1 Answer