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 Cristik Zafraz · Sep 01, 2013 at 06:41 AM · javascripttransform

How to Translate Object to Mouse Click Point in Game-World Coordinates

Hey everyone, I swear I have gone through every link, forum, and tutorial on how to get this to work, yet nothing works. Thank you for taking the time to read this, my first post - my frustration is killing me. As simple as people are saying this is, I beg you not to merely link me to another example, I've seen at least 10 and I've spent 2.5 hours on it.

Goal: I want a cube (call it A) to translate from its current point to a mouseclick point. Since I read that in space a mouse point would be ambiguous, I created a second object (call it B) to click on, for reference.

What I Tried: As seen below, I tried using answers from forums. To see what coordinates were being returned when I clicked on B, I added a debug lines marked below - yet no matter where I clicked the coords returned were always the same.

What Actually Happens: Cube A moves in a completely unexpected direction, no where near the click area

Possible Reasons for Not Working: 1. hitPos is always the same value regardless of where I click 2. hitPos returns (-0.9, 3.7) on cube B positioned at (2.7, 0) - doesn't make sense function OnMouseDown () {

       var hit : RaycastHit;
   var hitPos : Vector3 = Camera.main.ScreenToWorldPoint(hit.point);
       var theCube = GameObject.Find("Cube");
       var startPos : Vector3 = theCube.transform.position;
   var endPos : Vector3 = hitPos;
       var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
   
 
 if (Physics.Raycast (ray, hit))
 {
 
 var index =0.0;
 var rate = 0.2;
                              //debug commands to print off coordinate values
 Debug.Log(startPos);
 Debug.Log(endPos);
 Debug.Log("Impact at: "+hitPos);
 

while( index < 1.0 ) { theCube.transform.position = Vector3.Lerp( startPos, endPos, index ); Debug.DrawRay(startPos, endPos, Color.red); //goes nowhere near click area index += rate * Time.deltaTime; Debug.Log(theCube.transform.position); //another debug to track Cube A's coords if (startPos == endPos){ //I realize this if statement is probably pointless Debug.Log("Done!"); break; } yield; }

 }

 }

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
0
Best Answer

Answer by Hyperion · Sep 01, 2013 at 04:36 PM

So you want the object to move to mouse.

 //attach this to your camera
 static var mousepos : Vector3;
 function Update () 
 {
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit,1000))
     {
         mousepos = hit.point;
     }
 }
 
 //attach this to your object
  if (Input.GetMouseButtonDown (0)){
     transform.position = mouse.mousepos;
     }

This is a basic example. Hope it helps.

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 Cristik Zafraz · Sep 02, 2013 at 12:16 AM 0
Share

Yes! This code lead me on the right path to getting what I needed. With a few tweaks it was perfect. I am incredibly grateful for your help Hyperion, thank you very much.
What Happens Now: Cube A appears where I click

The only remaining issue now is how to incorporate movement speed, which I tried to achieve in the code I posted earlier. I tried multiplying mousepos by Time.deltaTime, and inserting that result as a parameter of transform.Translate. However it moved in an unexpected direction. From my Debug.Log line (prints the coords of mouseclick, the value of Time.deltaTime, and the destination coord defined as mousepos*Time.deltaTime:

  • Area to send it to: (3.4, 0.0, 7.1) Time unit: 0.01656607 Destination: (0.1, 0.0, 0.1) Clearly the destination point is no longer the clicked point.

Thanks again Hyperion for your help. If you're able to help me further I'll certainly appreciate it. Anybody that knows how to incorporate movement speed, I will be grateful for your help.

Bonus Goal: One of the tweaks to my code was that I changed cube B to a terrain object, so that when I click somewhere in the terrain cube A moves to the clicked area. Assu$$anonymous$$g cube A could translate to a click spot with a movement speed, would the code work for clicking on a sloped part of terrain (say a hill)? Or would I do something like figure out at what point in the desired direction does the hill start, translate to that point, then translate up the hill?

avatar image Hyperion · Sep 02, 2013 at 12:39 AM 0
Share

You're welcome. Could you please accept my answer? As for the 'appearance', try using lerping. You can find out more about that at the link: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Lerp.html

  transform.position = Vector3.Lerp(cube.position, mouse.mousepos, 1); 

Or something like that...

avatar image Cristik Zafraz · Sep 02, 2013 at 01:28 AM 0
Share

Awesome, thank you. I modified Lerp to $$anonymous$$oveTowards, and it works great. You're the best! I owe you a beer.

avatar image Hyperion · Sep 02, 2013 at 03:55 AM 0
Share

Glad to help!

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

17 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

Related Questions

Best Way to make a character move 1 Answer

How to make the player turn left or right only in desired places o edges and not everywhere ???? 0 Answers

How to control speed of animation on Unity by a .txt file? 1 Answer

NullReferenceException: Object reference not set to an instance of an object iShake.Update () (at Assets/scripts/iShake.js:32) 3 Answers

clamp limit variables trouble 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