Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ebilrubberducki · Dec 09, 2013 at 11:41 AM · animationgameobjectimportmodel

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: alt text

capture.png (130.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ebilrubberducki · Dec 10, 2013 at 07:06 PM 0
Share

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.alt text

thanks again for your help!

help1.jpg (368.0 kB)
avatar image RuneShiStorm · Jun 30, 2017 at 03:43 AM 0
Share

maybe you have set the animation itself on loop?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Cropping animation clip of FBX file at import 0 Answers

How can I animate between 3 gameObjects? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges