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 Agent_Cooper · May 22, 2015 at 02:53 PM · c#transformpositiontranslate

stop object(door) translation

i am trying to make sliding doors without animation, so i attached this script to the door object(in this case a cube), but i can't figure out how to stop it nor how to move the door to original position when the player is far, how should i do it ?

using UnityEngine; using System.Collections;

public class porte : MonoBehaviour {

 public int iDirection;
 public int startPos;
 public int maxDistance;
 Vector3 pos;
 Vector3 vel;
 protected Transform ThisTransform = null;
 protected PlayerController PC = null;
 protected Transform PlayerTransform = null;
 public float AttackDistance = 10;


 protected virtual void Start()
 {
             
             
             GameObject PlayerObject = GameObject.Find ("Player");
             PC = PlayerObject.GetComponentInChildren<PlayerController> ();

             PlayerTransform = PC.transform;
     
             
             ThisTransform = transform;

     }

 void Update() {
     float DistanceFromPlayer = Vector3.Distance(transform.position, PlayerTransform.position);
     if(DistanceFromPlayer < AttackDistance)
     {
         transform.Translate(Vector3.right * iDirection * Time.deltaTime);
         pos = transform.position;
     }

 
 
 
 }

}

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
1

Answer by TotalAnnihilation · May 22, 2015 at 07:28 PM

Hey! I would do it like this: I only can do java/unityscript, but the idea should be clear.

      if(DistanceFromPlayer < AttackDistance %% dooropening)
      {
          transform.Translate(Vector3.right * iDirection * Time.deltaTime);
          pos = transform.position;
          if(transform.position > Vector3.right * maxMovement)
             dooropening=false;
     }

maxMovement being a var maxMovement:int; you declare the maximum Movment of the door. I guess you have to try and error a little.

For sliding back: Not sure if it works, but you could try it

  if(DistanceFromPlayer > AttackDistance && doorclosing)
      {
          transform.Translate((-1)*Vector3.right * iDirection * Time.deltaTime);
 
          if(transform.position < Vector3.right*minMovement)
          {
             doorclosing = false;
             dooropening = true;
          }
 
      }

dooropening and doorclosing beeing bools. minMovement same as in the other snippet.

Hope it helped!

Comment
Add comment · Show 9 · 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 Agent_Cooper · May 22, 2015 at 07:55 PM 1
Share

ok, i'll try. thank you for your answer

avatar image Agent_Cooper · May 22, 2015 at 08:28 PM 0
Share

unfortunately it says: Operator >' cannot be applied to operands of type UnityEngine.Vector3' and `UnityEngine.Vector3'

avatar image TotalAnnihilation · May 22, 2015 at 08:31 PM 1
Share

oh yeah, sorry. Just compare the components you are changing, meaning transform.position.x < $$anonymous$$$$anonymous$$ovement or more likely just call $$anonymous$$$$anonymous$$ovement $$anonymous$$Position and set it to a position.

avatar image Agent_Cooper · May 22, 2015 at 08:34 PM 0
Share

thank you for your help and patience, i am still a noob with unity and i really appreciate yor help

avatar image TotalAnnihilation · May 24, 2015 at 05:34 PM 1
Share

This tutorial is quite good and explains you the basics of this, I think: https://www.packtpub.com/books/content/unity-game-development-interactions-part-1

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Transform.Position Collision Issue 0 Answers

How can I move an object a certain distance, once? 2 Answers

why is transform.position giving me local location?(c#) 0 Answers

An Efficient Way To Follow Transform's Position? 3 Answers

Lerp to where the target was x seconds ago? 0 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