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 Exagerate · Feb 20, 2013 at 07:34 PM · animationtranslatetranslationdoorslimitations

How to limit the transform.Translate distance

Hey there folks,

I've created this simple code that is an activated animation to open/close a sliding door when it's been clicked (well, when the right click is being held down to close it, it also has a seperate script that is almost identical to hold down left click to open it).

I want to be able to limit the distance the door can close, naturally; how can I expand on this code to make it do so?

C#

 using UnityEngine;
 using System.Collections;
 
 public class cabdoorclose : MonoBehaviour
 {
     
 public void OnDoorclose()
     {
         transform.Translate(Vector3.forward * Time.deltaTime);
     }
 }

This is the clickable object to open/close the door:

C# using UnityEngine; using System.Collections;

 public class cabdoorclick : MonoBehaviour
 {
     public cabdooropen dooropen;
     public cabdoorclose doorclose;
     void OnMouseEnter ()
     {
         guiText.text = "Hello World!";
     }
     void OnMouseOver ()
     {
         if (Input.GetMouseButton (1)) {
         doorclose.OnDoorclose();
         }
         if (Input.GetMouseButton (0)) {
         dooropen.OnDooropen();
         }
     }
 }
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 robertbu · Feb 20, 2013 at 09:50 PM

Is this code working? The problem I see is that OnDoorclose() is only called once, but with a Time.deltaTime included, it should be called repeatedly from Update(). One way to move something over time is to use Vector3.MoveTowards():

Here is an example script. In the inspector set v3Open and v3Closed to the positions you want the door in when it is open/closed. Call SetOpenClose() to either open or close the door.

 public class OpenClose : MonoBehaviour {
     
     public Vector3 v3Open;
     public Vector3 v3Close;
     public float fDistPerSecond = 1.0f;
     
     private Vector3 v3To;
     
     void Start() {
         v3To = transform.position;
     }
     
     void SetOpenClose(bool bOpen) {
         if (bOpen)
             v3To = v3Open;
         else
             v3To = v3Close;
     }
     
 
     void Update () {
         transform.position = Vector3.MoveTowards(transform.position, v3To, fDistPerSecond * Time.deltaTime);
     }
 }


Comment
Add comment · Show 7 · 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 Exagerate · Feb 21, 2013 at 12:22 AM 0
Share

Hi Robert,

yes the code does work as it is, just moving infinitely if I held the mouse click on it for that long.

I've struggled to understand what you meant by "In the inspector set v3Open and v3Closed to the positions you want the door in when it is open/closed", forgive me but i've only been using Unity for 3/4 days.

Do you mean move it by hand and somehow save and label that state? if so, how do I go about this?

Thanks also for your reply, it is appreciated.

$$anonymous$$ind regards,

avatar image robertbu · Feb 21, 2013 at 01:35 AM 0
Share

I recommend testing this code in a new project with just a camera and a cube at the origin. Attach the modified version of the script below to the cube. Click on the cube in the hierarchy panel. In the Inspector panel you will see the OpenClose script. Three variables will be listed v3Open, v3Close, and FDistPerSecond. FDistPerSecond sets how fast the object will move. v3Open and v3Close are the positions that the object considers open and closed. Click on the triangle next to v3Open and the x,y,z values will be displayed. You can change the values. For the test, use (-1,0,0) for v3Open and (1,0,0) for v3Closed. Run the script.

 using UnityEngine;
 using System.Collections;
 
 public class OpenClose : $$anonymous$$onoBehaviour {
     
     public Vector3 v3Open;
     public Vector3 v3Close;
     public float fDistPerSecond = 1.0f;
     
     private Vector3 v3To;
     
     void Start() {
         v3To = transform.position;
         InvokeRepeating("RepeatOpenClose",0.0f, 3.0f);
     }
     
     void SetOpenClose(bool bOpen) {
         if (bOpen)
             v3To = v3Open;
         else
             v3To = v3Close;
     }
     
     void Update () {
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, v3To, fDistPerSecond * Time.deltaTime);
     }
     
     bool b = false;
     void RepeatOpenClose() {
         SetOpenClose (b);
         b = !b;
     }
 }

The additions I made to this modified version trigger the open/close method very 3 seconds.

avatar image Exagerate · Feb 21, 2013 at 06:26 AM 0
Share

Hi Robert,

I've followed your instructions and edited it over many hours now- by default the door just flies off into the distance infinitely- i've tried lowering the value to 0.01 to see if it was just set too far- but to no avail unfortunately. Am I doing something wrong with simply applying your latest code to a cube and setting the values in the inspector to 1? Did you mean for this to be linked to one of my existing C# scripts?

The most I managed to develop your script into (something that the user could control by any input) was the following, which made the door fly infinitely until it was clicked- then it would stop and nothing further could be done with it.

 using UnityEngine;
 using System.Collections;
  
     public class cabdooropenclose : $$anonymous$$onoBehaviour
 
 {
     public Vector3 v3Open;
     public Vector3 v3Close;
     public float fDistPerSecond = 0.1f;
     
     private Vector3 v3To;
     
     void On$$anonymous$$ouseDown ()
     {
         v3To = transform.position;
     }
  
     void SetOpenClose(bool bOpen) {
        if (bOpen)
          v3To = v3Open;
        else
          v3To = v3Close;
     }
  
     void Update () {
        transform.position = Vector3.$$anonymous$$oveTowards(transform.position, v3To, fDistPerSecond * Time.deltaTime);
     }
     
     bool b = true;
     void RepeatOpenClose() {
        SetOpenClose (b);
        b = !b;
     }
 }

Thanks for your responses so far; I hope to get there soon ;)

avatar image robertbu · Feb 21, 2013 at 09:25 AM 0
Share

I made the change you were trying to make. If you click on it, the door will open/close. Before you integrate it into your scene: 1) Create a new scene, 2) Create a block at the origin (0,0,0), 3) Attach the script. Note you might get a fast initial move if you did not place your block at the origin to start.

 using UnityEngine;
 using System.Collections;
  
     public class OpenClose2 : $$anonymous$$onoBehaviour
 {
     public Vector3 v3Open = new Vector3(1.0f, 0.0f, 0.0f);
     public Vector3 v3Close = new Vector3(-1.0f, 0.0f, 0.0f);
     public float fDistPerSecond = 1.0f;
     bool b = true;
  
     private Vector3 v3To;
  
     void On$$anonymous$$ouseDown ()
     {
        SetOpenClose (b);
         b = !b;
     }
  
     void SetOpenClose(bool bOpen) {
        if (bOpen)
          v3To = v3Open;
        else
          v3To = v3Close;
     }
  
     void Update () {
        transform.position = Vector3.$$anonymous$$oveTowards(transform.position, v3To, fDistPerSecond * Time.deltaTime);
     }
 }
avatar image Exagerate · Feb 21, 2013 at 09:45 PM 0
Share

Thanks Robert! This is doing exactly what I expect it to! I am sure I will have no problem learning from it and adapting it to be within my scene!

$$anonymous$$indest regards,

Show more comments

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

9 People are following this question.

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

Related Questions

Transform.Translate causing object to teleport 0 Answers

trigger animation problems 2 Answers

open a animated door 3 Answers

Opening a door when a button object has something it it's collider. 0 Answers

Is it possible to continue a method after Input? 2 Answers


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