Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by William_Goodspeed · Aug 19, 2014 at 10:20 AM · mouseclick

Move object to mouse click position

Hey folks,

So I've had some time away from Unity and I've come back to make quite a simple script but I keep getting tripping over my own code.

So basically I've got a couple of things going only, firstly I've got a Character object, nested within it I've got the main camera and also an object to hold the character model.

Outside of the Character object I've got an Empty Game Object called target.

Here's what I want to do, wherever the player clicks the target object moves instantly to that position.

Now I've tried placing a simple script on the target object but it doesn't appear to work.

 using UnityEngine;
 using System.Collections;
 
 public class targetmove : MonoBehaviour {
 
 
     // Use this for initialization
     void Start () {
 
         Vector3 newPosition = transform.position;
 
 
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKeyDown(KeyCode.Mouse0)) 
         {
             if (Input.GetMouseButtonDown(0)) 
             {
                 newPosition.x = Input.mousePosition.x;
                 newPosition.y = Input.mousePosition.y;
                 transform.position = newPosition;
             }
         }
 
     }
 }

I know that ray casting may be a way to go but I've not used it that much and also when I tried it I had problems as my camera is nested within my character.

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

2 Replies

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

Answer by Ilgiz · Aug 19, 2014 at 11:38 AM

maybe it's

 using UnityEngine;
 using System.Collections;
 public class targetmove : MonoBehaviour
 {
     Vector3 newPosition;
     void Start () {
         newPosition = transform.position;
     }
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit))
             {
                 newPosition = hit.point;
                 transform.position = newPosition;
             }
         }
     }
 }
 
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 Hahasif · Jan 07, 2015 at 02:11 AM 1
Share

Thank you, this worked for me.

avatar image SexiCowboy · Jan 24, 2020 at 01:53 PM 0
Share

For me this script is just kind of teleporting the object to the clicked position. How can i make the object kind of move to the clicked position?

avatar image ngcongtru9 · Aug 09, 2021 at 12:20 PM 0
Share

Oh, verry good. thanks you so mush

avatar image
1

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

6 People are following this question.

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

Related Questions

How To Perform A Mouse Click On Game Object 5 Answers

Mouse controlled Laserpointer 1 Answer

OnMouseDown doesn't work 1 Answer

Clicking button locks mouse, doesn't activate button. how do I fix this? 0 Answers

Detect mouseDown & mouseUp events on handles in editor 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