- Home /
How can I create a chest that opens on click?
Hi, So this is a first time for me but I'm really confused here so here goes nothing:
I have a 3D model that I made in cinema 4D, it's a simple chest that has an open and close animation on it. I want to bring it into unity so that in my game a player can open and close it with a mouse click, I've tried looking around for answers but can't seem to find any. I've tried the animations in various file formats and imports. At least I just want it to play the open animation on click, stop, and then play the close animation on a second click. Currently I have no script for this as I'm struggling to even get the model working with the textures in unity.
Just in case you need to know what kind of model it is, here's my chest:
Answer by Spherical Cow · Dec 09, 2013 at 02:36 PM
Great model. Here is what I would do in C# (this is untested, but should work).
This is the no frills way to do it...
using UnityEngine;
using System.Collections;
public class OpenCloseChest : MonoBehaviour {
public bool isOpen;
void OnMouseUp(){
isOpen = !isOpen;
}
void Update(){
if(isOpen)
{
animation["Open"].wrapMode = WrapMode.Once;
animation.Play ("Open");
}
if(!isOpen)
{
animation["Open"].wrapMode = WrapMode.Once;
animation.Play ("Close");
}
}
}
But this would play either animation whenever the chest was opened, so it would be advisable to add a timer to control when the chest is opened.
using UnityEngine;
using System.Collections;
public class OpenCloseChest : MonoBehaviour {
float openCloseTimer;
public float setOpenCloseTimer;
public bool isOpen;
void Awake(){
openCloseTimer = 0
}
void OnMouseUp(){
if(openCloseTimer <= 0)
isOpen = !isOpen;
}
void Update(){
openCloseTimer -= Time.deltaTime;
if(openCloseTimer <= 0)
openCloseTimer = 0;
if(isOpen)
{
animation["Open"].wrapMode = WrapMode.Once;
animation.Play ("Open");
openCloseTimer = setOpenCloseTimer;
}
if(!isOpen)
{
animation["Close"].wrapMode = WrapMode.Once;
animation.Play ("Close");
openCloseTimer = setOpenCloseTimer;
}
}
}
The final issue is that the script above, while limiting how quickly the chest could be open or closed, does not limit the distance from which the player could open so-said chest. So, the final addition is to check the distance the player is from the chest and then enable the ability to open it.
public class OpenCloseChest : MonoBehaviour {
public float distanceToActivate;
public float setOpenCloseTimer;
float distance;
float openCloseTimer;
bool isSwitchable;
bool isOpen;
GameObject player;
void Awake(){
isSwitchable = false;
openCloseTimer = 0;
player = GameObject.FindGameObjectWithTag("Player");
}
void OnMouseUp(){
if(openCloseTimer <= 0 && isSwitchable)
isOpen = !isOpen;
}
void Update(){
distance = Vector3.Distance(this.transform.position, player.transform.position);
if(distance <= distanceToActivate)
isSwitchable = true;
else
isSwitchable = false;
openCloseTimer -= Time.deltaTime;
if(openCloseTimer <= 0)
openCloseTimer = 0;
if(isOpen)
{
animation["Open"].wrapMode = WrapMode.Once;
animation.Play ("Open");
openCloseTimer = setOpenCloseTimer;
}
if(!isOpen)
{
animation.Play ("Close");
animation["Close"].wrapMode = WrapMode.Once;
openCloseTimer = setOpenCloseTimer;
}
}
}
Hope that helps!
A.
Hey thanks for the help! I have a slight problem with the code though, for some reason when playing the game the chest will loop play either the open or close animation automatically, there's also an issue with the third lot of coding you posted, there seems to be an error and I don't know C# well enough to try and fix it. I've included a screenshot if you're interested.
thanks again for your help!
Your answer
Follow this Question
Related Questions
How to animate inside Unity? (Blender animation is finished) 1 Answer
Maya Import Problems 0 Answers
Animation importing issues from maya 1 Answer