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 Suduki · Apr 12, 2012 at 06:38 PM · c#movementgameobjectmouse

Moving a Game Object to the mouse

So I've searched and searched, and couldn't find an answer to my question (yes, I searched here too).

So what I want, is after a right mouseclick, a gameobject will move to where the mouse clicked. My problem is, when I click somewhere on the terrain, the object moves to 0,0,0, and not to where the mouse clicked.

Here is the code

 using UnityEngine;
 using System.Collections;

 public class Movement : MonoBehaviour
 {
 
 public Vector3 startPoint;
 private float startTime;
 private Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
 // Use this for initialization
 void Start ()
 {
     startPoint = transform.position;
     startTime = Time.time;
 
 }
 
 // Update is called once per frame
 void Update ()
 {    
     if(Input.GetMouseButtonDown(1))
     transform.position = Vector3.Lerp(startPoint, clickedPosition, (Time.time-startTime)/1.0f);
 
       }
 }

It does save the startPoint, but clickedPosition is 0,0,0 no matter what I do. Help will be rewarded with valour.

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 AlucardJay · Apr 13, 2012 at 03:36 AM 0
Share

similar : http://answers.unity3d.com/questions/231349/object-follow-the-mouse-path.html

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gregzo · Apr 12, 2012 at 07:47 PM

search for raycasting! Basicaly, you need to cast a ray from the mouse's ScreenToWorldPoint and get the intersection between the ray and the terrain. This is the position you want your object to move to... There are plenty of examples around!

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
1

Answer by bilo-lwabona · Sep 14, 2014 at 07:28 PM

You can cast a ray from the mouse position and move it to a point wherever the ray intersects something, using Physics.Raycast. Here is some example code:

 Ray ray = this.camera.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
 RaycastHit hit;
 Debug.Log("Casting ray");
 if (Physics.Raycast(ray, out hit, 3000))
 {
     Debug.Log("Ray hit something");
     if (hit.collider.gameObject.tag == "Terrain")
     {
        Vector3 clickedPosition = hit.point;
        transform.position = Vector3.Lerp(startPoint, clickedPosition, (Time.time - startTime) / 1.0f);
     }
 }

This should work, regardless of the screen origin, as you are essentially only working with variables in the 3D environment. Note the additional check to move the object, only if the ray cast from the mouse has hit something ... in this case that would be a gameobject with the user-assigned Tag "Terrain". Hope this helps.

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 dkNinja · Apr 12, 2012 at 08:35 PM

In the code sample you provided, your clickedPosition is initialized at the instantiation of the GameObject. However, at that time, the mousePosition is unknown. You should recapture the mousePosition in the update loop after you check if the mouse button has been pressed.

 void Update()
 {
     if (Input.GetMouseButtonDown(1)) // right mouse button
     {
         clickedPosition = Camera.main.ScreenToWorldPosition(Input.mousePosition);
         transform.position = Vector3.Lerp(startPoint, clickedPosition, (Time.time - startTime) / 1.0f);
     }
 }

One thing to also remember is that mouse coordinates and screen coordinates have different origins in Unity.

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 falconer · Oct 22, 2014 at 05:51 AM

This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position. You can find the article by The Game Contriver on the same here

Move to Touch / Click Position - The Game Contriver

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

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

9 People are following this question.

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

Distribute terrain in zones 3 Answers

How to make my spaceship move by mouse? 2 Answers

Camera movement question 1 Answer

Controlling an animation by dragging a gameObject with the mouse 0 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