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 Jack Harlem · Jan 12, 2013 at 12:30 AM · movetowardsvector3.movetowardsmouse click

Using "Vector3.MoveTowards" to translate an object to mouse click

I'm looking to translate an object to a mouse click, but do so so the object actually travels to the click instead of "snapping" to the mouse click.

I am trying to use the function "Vector3.MoveTowards()" to accomplish this, but to no avail. What am I doing wrong? (Code Below)

Other info:

  • Game world is a birds-eye camera, Cube with dimensions (200,2,200) and a Sphere called "Player" with (10,10,10) dimensions

  • The Player Object has a RigidBody attached

  • I have tried variations of ordering my MoveTowards function, and also tried Lerp and Slerp

       using UnityEngine;
         using System.Collections;
         
         public class MoveToClick : MonoBehaviour {
             
             GameObject player;
             RaycastHit hit;
             Vector3 playerPos = new Vector3 (0,5,0);
             float speed;
             
             // Use this for initialization
             void Start () {
                 player = GameObject.Find("Player");
                 player.transform.position = playerPos;
                 speed = 100.0f;
                 hit = new RaycastHit(); 
                 //speed = speed * Time.deltaTime;
             
             }
         
             //Update is called once per frame
                 void Update () {
                 if (Input.GetKeyDown(KeyCode.Mouse0)) {
                     if (Input.GetMouseButtonDown(0)) {
                         movePlayer();
                     }
                 }
             }
             
             void movePlayer(){
                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);                
                 if (Physics.Raycast(ray, out hit, 1000.0f)) {
                     print(playerPos  + " " + hit.point  + " " + speed);
                     transform.position = Vector3.MoveTowards(hit.point, playerPos, speed);
                 }
             }
         }
    
Comment
Add comment · Show 1
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 Wolfram · Jan 12, 2013 at 12:55 AM 0
Share

Note "$$anonymous$$oveTowards" as well as all Lerp variants don't do an animation, they return a single value, once. To move the object each frame a bit, you'd have to do something every frame. Unless you are applying a force to a Rigidbody. Also, maybe this will help: Rigidbody.$$anonymous$$ovePosition

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Lovrenc · Jan 12, 2013 at 01:01 AM

2 mistakes here:

  1. You are only moving your character when you press your key. How can you get animation then? When you press youre key, you should store the value he has to walk to (Vector3). You have to animate it every frame (or until he reaches that spot).

  2. Vector3.MoveTowards has 3 parameters. StartPosition, EndPosition, and a float, that says where you should be between those 2 points.

If last parameter is 0: You will be in start position; If last parameter is .5f: You will be in the middle of 2 positions; If last parameter is 1: You will be on end position;

    using UnityEngine;
     using System.Collections;
 
     public class MoveToClick : MonoBehaviour {
 
         GameObject player;
         RaycastHit hit;
         Vector3 playerPos = new Vector3 (0,5,0);
         float speed;
         
         Vector3 previousPosition;
         Vector3 targetPosition;
         float lerpMoving;
 
         // Use this for initialization
         void Start () {
             player = GameObject.Find("Player");
            player.transform.position = playerPos;
            speed = 100.0f;
             hit = new RaycastHit(); 
            //speed = speed * Time.deltaTime;
 
         }
 
         //Update is called once per frame
            void Update () {
             if (Input.GetKeyDown(KeyCode.Mouse0)) {
                 if (Input.GetMouseButtonDown(0)) {
                   Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);          
                     if (Physics.Raycast(ray, out hit, 1000.0f)) {
                       previousPosition = transform.position;
                       targetPosition = hit.point;
                       lerpMoving = 0;
                     }
                 }
             }
             if(lerpMoving < 1)
               movePlayer();
         }
 
         void movePlayer(){
              lerpMoving += Time.deltaTime; 
              transform.position = Vector3.MoveTowards(previousPosition, targetPosition, speed * lerpMoving);
            }
         }
     }
Comment
Add comment · Show 5 · 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 Jack Harlem · Jan 14, 2013 at 10:14 PM 0
Share

Your update works, and for that I thank you.

Could you expand a little bit on you first talking point. Do you mean animating in the conventional sense, or do you mean something else when you say it?

avatar image Lovrenc · Jan 14, 2013 at 10:21 PM 0
Share

I ment this: Your movement code has to execute every frame (if ofcourse player is to move that is). How you set it up, you only were calling movement command when you held your mouse button down (because you had the code inside is$$anonymous$$ouseDown if statement bracket).

Naturally every frame (not totally true but lets keep it like that) your code has to calculate new position for the gameObject to put it there.

avatar image Wolfram · Jan 14, 2013 at 10:23 PM 0
Share

Look at his code. When you press the mouse button, lerp$$anonymous$$oving is set to 0. Then further down, still in Update() and therefore called every frame, he calls movePlayer(), which changes transform.position, each frame a little further. That's what is meant by "animating". Every frame you add some small delta to a position or rotation, and in the end, with enough fps, this looks like movement.

avatar image Jack Harlem · Jan 14, 2013 at 10:50 PM 0
Share

Ah, I understand you both now. $$anonymous$$akes things a little clearer.

Once again, thank you both for your help

avatar image reko91 · Apr 01, 2017 at 08:24 PM 0
Share

I know this is an old post, but shouldn't you incorporate the speed in the lerp$$anonymous$$oving float variable ? I mean, say lerp$$anonymous$$oving is 0.01, then in your movePlayer function, the 3rd parameter of the $$anonymous$$oveTowards, would be 1, as 0.01 * 100 (speed) = 1; But you'll keep calling your movePlayer() as lerp$$anonymous$$oving is still below 1, but the object has already reached it's final position, so there's no point in doing the check again ? So a way around this would be : lerp$$anonymous$$oving += (speed*Time.deltaTime) ?

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

11 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

Related Questions

MoveTowards is not working with RectTransform component 0 Answers

Unexpected behavior on Vector3.moveTowards for Player 0 Answers

conserve momentum of vector3.moveTowards in 2D game 0 Answers

Adjust the speed of an individual axis when using MoveToward,Adjust the speed by individual axis usingMoveToward 1 Answer

Vector3.MoveTowards MaxDistanceDelta value 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