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
2
Question by tonydemot · Feb 08, 2012 at 01:42 PM · movementraycastobjectmouseclick

Object Movement via Mouse Click?

i want to move an object on the x axis to a specific point where i right click my mouse would i do this with raycasting?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheFish657 · Jul 01, 2016 at 04:32 PM

you need to raycast from the camera to the mouse, then add a check for mouse click in an if statement. Then, change the object's x position to the x position of the hit.

Comment
Add comment · Show 3 · 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 Czar-Man · Nov 05, 2016 at 01:41 AM 0
Share

Is this also true for 2D games?

EDIT: I mean, I am able to get the mouse position. However, is it necessary to draw a raycast from the camera to the mouse position? I currently notice the mouse position having x and y values disproportionately greater than the actual position.

avatar image bubzy Czar-Man · Nov 05, 2016 at 07:19 AM 1
Share
 Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
avatar image Czar-Man bubzy · Nov 06, 2016 at 09:37 PM 0
Share

What I was also asking, is it necessary to draw a raycast from the camera to the mouse position? I currently notice the mouse position having x and y values disproportionately greater than the actual position.

avatar image
0

Answer by kamyflex · Nov 05, 2016 at 09:03 AM

Use raycast to find where the user clicked, Then create a new Vector3 where "x = raycast.hit.x" and keep y and z the same as your characters y and z, Then use NavMesh to move the object to the Vector3 you created.

Comment
Add comment · 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
0

Answer by bubzy · Nov 05, 2016 at 07:23 AM

from : https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

 using UnityEngine;
 
 public class ExampleClass : MonoBehaviour 
 {
     void Update() 
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
 
         if (Physics.Raycast(ray, out hit, 100)) 
             Debug.DrawLine(ray.origin, hit.point);
     }
 }


you can use the parameters on hit.point to determine your position to move to

Comment
Add comment · 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 bubzy · Nov 05, 2016 at 07:30 AM 1
Share

Just to elaborate on your specific request i knocked this up. using UnityEngine; using System.Collections;

 public class movetomouse : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     public GameObject objectTo$$anonymous$$ove;
 
     void Start () {
     
     }
 
     // Update is called once per frame
   
         void Update()
         {
         if (Input.Get$$anonymous$$ouseButtonDown(1))
         {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hit;
 
             if (Physics.Raycast(ray, out hit, 100))
                 Debug.DrawLine(ray.origin, hit.point);
             objectTo$$anonymous$$ove.transform.position = new Vector3(hit.point.x, objectTo$$anonymous$$ove.transform.position.y, objectTo$$anonymous$$ove.transform.position.z);
         }
     }
 
 }

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnMouseUp() Click Display Effect. 1 Answer

Mouse Click and Spawn object 4 Answers

How to make object clickable when user enter collider ? 0 Answers

How to properly set the position clicked by the mouse? 0 Answers

Move while mouse button is held down 3 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