Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
avengerxp

move a plane from a to b with 1 key stroke

Question

ok, I have got up to this far with the script.

 

it will move the object its attached to if i continuosly keep pressing the key.

 

but what I want to do is activate an action where when i press the key once, will move the object from it's current position to a nother and stop.

Please login or register to see this code.

What I want is, I press the "l" key once and the object should move from the current ostion to some uints and stop. (trigering a sliding door panel to slide a side with a keystroke

Share this post


Link to post
Share on other sites

8 answers to this question

Recommended Posts


  • 0

Not sure how to do this in javascript but in c# something like this

This will move the object from where it is to "pointB" which is where ever you want it to be (maybe an object (just use object.transform.position))

Please login or register to see this code. Edited by Calum.McManus

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote. Its better to use yield return 0/null. Since its a lot cleaner. Also, replace Vector3.Distance with while(pointB - transform.position.sqrMagnitude > 0)

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

I using WaitForEndOfFrame usually to avoid it running too quickly on longer loops as it has lead to crashing before, and the vector3.distance works fine for me.

If this is for a door then Vector3.Lerp instead on Vector3.MoveTowards by be nicer as it slows down at the end instead on just move to the position and stopping dead. 

Then you would have to put a reset in for the door so it goes back to being closed, (unless it stays open for good in which case disable the script at the end when the door is open.

Share this post


Link to post
Share on other sites
  • 0

Yield return 0 instructs it to wait one frame. Also sqrMagnitude is faster since it avoids the square root function

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Oh right, so what is the difference between WaitForEndOfFrame() and yield return 0? seems like a pointless diffrence

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Shorter code! And its more natural but its up to you :P

Share this post


Link to post
Share on other sites
  • 0
Please login or register to see this quote.

Haha fair enough, I use ReSharper which stores in common lines for you so when i type in yield i can just tab in the whole line anyway xD

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...