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 /
avatar image
0
Question by Harardin · Mar 05, 2017 at 10:33 AM · c#movementvector3.movetowards

Error with moving object to mouse clicked coordinates. [SOLVED]

I made a little script that should take a coordinates an the move object to it. But is moves object to an opposite direction. I am workin with 2d scene.

Here is the code:

public Camera MainCamera; public LayerMask layers; public float movementSpeed = 20.0f; private bool IsMoving = false; private Vector3 targetPosition;

     // Use this for initialization
     void Start()
     {
         targetPosition = transform.position;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetMouseButton(1))
         {
             SetCoordinates();
         }
         if (IsMoving)
         {
             Moving();
         }
     }
     private void SetCoordinates()
     {
         Ray ray = MainCamera.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast(ray, out hit, Mathf.Infinity, layers))
         {
             targetPosition.x = hit.point.x;
             targetPosition.y = hit.point.y;
             targetPosition.z = transform.position.z;
             Debug.DrawRay(transform.position, targetPosition, Color.red);
         }
         IsMoving = true;
     }
     private void Moving()
     {
         transform.position = Vector3.MoveTowards(transform.position, targetPosition, movementSpeed * Time.deltaTime);
 
         if (transform.position == targetPosition)
         {
             Debug.Log(targetPosition);
             Debug.Log(Input.mousePosition + " mouse pos");
             IsMoving = false;
         }
     }
 }
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
Best Answer

Answer by LiloE · Mar 05, 2017 at 11:01 AM

Use ScreenToWorldPoint instead of Raycast

In 2D scenes like yours, use the Camera.main.ScrenToWorldPoint function instead of rays.
The Raycast is for 3d physics.
If you need to detect a hit on a game object, use Physics2D.OverlapPoint

Basic Example

 var position = Camera.main.ScreenToWorldPoint(mousePosition);
 var col = Physics2D.OverlapPoint(position);

Example for moving a target to mouse location

 public class MovePointToPoint : MonoBehaviour {
     public float Speed = 5;
     Vector3 target = Vector3.zero;
 
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown(0)) {
             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             target.z = 1;
         }
 
         if (transform.position != target) {
             float step = Speed * Time.deltaTime;
             transform.position = Vector3.MoveTowards(transform.position, target, step);
         }
     }
 }
Comment
Add comment · Show 4 · 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 Harardin · Mar 05, 2017 at 11:12 AM 0
Share

I changed code like this:

     private void SetCoordinates()
     {
         targetPosition.x = $$anonymous$$ainCamera.ScreenToWorldPoint(Input.mousePosition).x;
         targetPosition.y = $$anonymous$$ainCamera.ScreenToWorldPoint(Input.mousePosition).y;
         targetPosition.z = transform.position.z;
         Is$$anonymous$$oving = true;
     }
     private void $$anonymous$$ove()
     {
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, movementSpeed * Time.deltaTime);
         if (transform.position == targetPosition)
         {
             Is$$anonymous$$oving = false;
         }
     }

And object start to move just on Y coordinates.

avatar image Harardin · Mar 05, 2017 at 11:20 AM 0
Share

I also used a hit detection but object moved far away from mouse coordinates. Something like this mouse in x=10, y=10, and the object moves above it on x=100, y=100.

avatar image Harardin · Mar 05, 2017 at 11:32 AM 0
Share

$$anonymous$$y mistake I guess. Just fixed a few thing an all working good thank you.

     private void SetCoordinates()
     {
         targetPosition.x = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
         targetPosition.y = Camera.main.ScreenToWorldPoint(Input.mousePosition).y;
         targetPosition.z = transform.position.z;
         Debug.Log(targetPosition);
         Is$$anonymous$$oving = true;
     }
     private void $$anonymous$$ove()
     {
         transform.position = Vector3.$$anonymous$$oveTowards(transform.position, targetPosition, movementSpeed * Time.deltaTime);
         if (transform.position == targetPosition)
         {
             Is$$anonymous$$oving = false;
         }
     }
avatar image LiloE Harardin · Mar 05, 2017 at 11:46 AM 0
Share

Great, but see my example for a more streamlined option. You can convert the entire vector once and just modify the z component later.

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

297 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 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 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

Making a bubble level (not a game but work tool) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Shifting blocks to the right to remove empty spaces - C# 0 Answers

How to use smoothly move camera to position using lerp? 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