Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Zeepblok · Oct 30, 2017 at 09:06 AM · movement scriptmousepositionmouseclickmovetowards

Object dissapears on MoveTowards

Hi,

I have a project where I want to move my player to the position I click.

I have the object, a camera and a terrain object.

I attached the script to the terain object. When I click my object doesnt move toward the point but dissapears. It still is there but the x, y and z are going all over the place. What am I doing wrong?

 public class MouseHandler : MonoBehaviour
 {
     public GameObject unit;
     Vector3 targetPosition;
     public float speed;
 
     void Start()
     {
         targetPosition = transform.position;
     }
     void Update()
     {
 
         if (Input.GetMouseButtonDown(0))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit))
             {
                 float step = speed * Time.deltaTime;
                 unit.transform.position = Vector3.MoveTowards(transform.position, targetPosition, step);
             }
         }
     }
 }
Comment
Add comment · Show 3
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 Hellium · Oct 30, 2017 at 09:12 AM 0
Share

Since you set targetPosition to transform.position (in the Start function), the distance between the start position and end position of $$anonymous$$oveTowards equals to 0 which leads to this behaviour.

avatar image Zeepblok · Oct 30, 2017 at 09:16 AM 0
Share

Removed it but it didnt fix it :(

avatar image Hellium Zeepblok · Oct 30, 2017 at 09:18 AM 0
Share

What does Debug.Log( Vector3.Distance( transform.position, targetPosition ) ) output just before you set the position of your unit?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Oct 30, 2017 at 09:19 AM

Are you sure you don't want to call :

 unit.transform.position = Vector3.MoveTowards(unit.transform.position, targetPosition, step);

Also, I guess you want the unit to be moved towards the position click. If so, put the following line before setting the position :

 targetPosition = hit.point;

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 Zeepblok · Oct 30, 2017 at 09:37 AM 0
Share

Now the object doesnt dissapear but now it only slides a fraction of a pixel to the side. If I click a bunch of times you see it moveing slightly

avatar image Hellium Zeepblok · Oct 30, 2017 at 09:44 AM 0
Share

Try to increase the speed, but keep in $$anonymous$$d that $$anonymous$$oveTowards does not move an object over time like an animation. To do so, you will need coroutines.

avatar image Zeepblok Hellium · Oct 30, 2017 at 09:46 AM 0
Share

I put all the move stuff in a move function now and run it in the update frame. Now the object moves but only to the right. almost there thnx for the tips :)

Found the problem. $$anonymous$$y mouse position isnt being read correctly. its always the same wherever I click.

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

120 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How do I make a rigid body move towards position of touch 0 Answers

Object heading towards mouse click/ touch position, script improvement. 1 Answer

Make an object move forward and back in one click.,Moving an object forward and back to it's original place with one click. 0 Answers

Convert Event.current.mousePosition to Input.mousePosition 0 Answers

coordinates 0, 0 is center, not bottom left corner? 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