• 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 /
This question was closed Jan 26, 2013 at 10:53 AM by Fattie for the following reason:

Duplicate Question And How

avatar image
Question by meg4ntron · Jan 26, 2013 at 05:52 AM · lerpclickteleport

How to prevent an object from teleporting to another object on mouse-click?

I have a character in my scene and when I click on an object, I want the character to move to that object. That part works great except that when I click, my character teleports to the object. How would I write the script so that my character moves slowly toward the object (like he is walking to it). Here is what I have so far... (JS)

 var window : GameObject;
 var boy : GameObject;

 function OnMouseDown(){
 boy.transform.position = window.transform.position;
  }

  function Update(){
  transform.position = Vector3.Lerp(boy.position, window.position, Time.deltaTime);
   
Comment

People who like this

0 Show 2
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 Lovrenc · Jan 26, 2013 at 06:27 AM 1
Share

This is asked at least twice a week.

avatar image meg4ntron · Jan 26, 2013 at 06:19 PM 0
Share

Lovrenc- I'd love the see the two previous questions you saw this week that were identical. And the prior week's as well? It's interesting that from hours of searching, I never ran across those.

Note: If you have nothing useful to say, please refrain from commenting. Thank you for your courtesy in the future.

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by robertbu · Jan 26, 2013 at 06:35 AM

Here is a script. It uses MoveTowards() instead of Lerp(). Lerp would take a little more doing. Note the way you have this structured, whatever object you have this script attached to is the one that get the click, and it does not have to be either the window or the boy.

 var window : GameObject;
 var boy : GameObject;
 
 var targetPosition : Vector3;
 var maxDistancePerFrame = 0.1;
 
 function Start() {
 targetPosition = boy.transform.position;
 }

 function OnMouseDown(){
     targetPosition = window.transform.position; 
  }
  
  function Update(){
      boy.transform.position = Vector3.MoveTowards(boy.transform.position, targetPosition, maxDistancePerFrame);
  }
Comment

People who like this

0 Show 1 · 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 meg4ntron · Jan 26, 2013 at 06:16 PM 0
Share

That worked perfectly! Thank you :)

avatar image

Answer by Nerull22 · Jan 26, 2013 at 06:36 AM

Try something like this instead. I'm not posotive on the actual calls, you may need to look them up in the script reference, but I'm pretty sure I'm pretty close.

 var move : bool = false;
 
 function OnMouseDown()
 {
    move = true;
 }
 
 function Update()
     {
       if(move)
       {
        transform.position = Vector3.Lerp(boy.position, window.position, Time.deltaTime);
       }
     }

Why your code isn't working, is you're saying, "When I click, set my character position to the windows position, and then in update your lerping to it. So it's slowly moving there over time, but then you click and it teleports there. You want to slowly move there when you click, so you want to only lerp when you click. Hopefully this will at least help.

Comment

People who like this

0 Show 0 · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

12 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

Related Questions

Fire missile to mouse click 1 Answer

Clicking on object to teleport player? 1 Answer

lerping colliders heights problem 1 Answer

Crash on loop using Lerp. 1 Answer

Inertia/Smoothing Advice 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