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 slayer29179 · Dec 05, 2011 at 04:53 PM · raycastterrainobject

Click on floor, record vector 3 position

Hey, I am making a game and i am having trouble with this area.

The problem is I want to record where my mouse is on the X, Y, Z location when I press down and i am hoping i could store this as a variable or 3 variables because then i could use that code to tell an object to go to that location. I do not know where to start, i read another post similar to mine and that was about ray cast but that just confused to much, but when i got it to work i put the script on the floor and nothing happening i put it on my object and it spun out and jumped towards the camera, this was the code i prevoisly tried:

 function Update () 
 {
 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 if (Physics.Raycast (ray, hit, 100)) {
     var hitPoint = hit.point;
 }
 }

some extra details: my object is a cube with box collision my floor is a large cube the code i will use to tell the object to go to the next location would be a way point code which i found on YouTube, i am just hoping i could use the X, Y, Z locations to put them onto the GUI for the object to follow.

please help! thank you!

my game: http://www.megaupload.com/?d=JAF3VSHS

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 IRetrograde · Dec 05, 2011 at 05:21 PM 1
Share

Slayer, does both your floor and your cube have box collision? Sorry if I'm being picky but you didn't specify if your floor had collision as well.

Also, try debugging what's going on.

The example here is great since it spawns a particle effect in the hit location. You could do something similar by putting a debug object at that position to see what's happening without altering the scene.

http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

2 Replies

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

Answer by Bicko · Dec 05, 2011 at 05:31 PM

Here you go, put this script in your scene, attach the cube that you want to move to the script's "Move Me" variable in the editor. This should give you a starting point to work from.

 var moveMe : GameObject;
 
 function Update()
 {
     //Check for the mouse being pressed (also works as a touch input)
     if (Input.GetMouseButtonDown(0))
     {
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (Physics.Raycast(ray, hit))
         {
             moveMe.transform.position = hit.point;
         }
     }
 }
Comment
Add comment · Show 5 · 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 slayer29179 · Dec 06, 2011 at 02:11 PM 0
Share

i am sorry i tried this and no look :/ please check my game thank you!

avatar image Bicko · Dec 07, 2011 at 12:57 PM 1
Share

Changed my answer, try this, again let me know if it doesn't work (I haven't looked at your game, but this should do what you want).

avatar image slayer29179 · Dec 07, 2011 at 01:19 PM 0
Share

Thank you Bicko! it works :D will need to mess around with the pivot point but it works :D thank you so much! i thought i would of had to change my game

avatar image wenhua · Jan 10, 2012 at 02:30 AM 0
Share

Hi Bicko,do you know how to do this,if when we mouse click a location or an objects,i wanted it to be display the coordinates of the vector3 position in the scene.

avatar image Bicko · Jan 10, 2012 at 02:34 PM 0
Share

wenhua: That hit.point above is a Vector3, you could use this to get the information you want.. if you wanted to know the coordinates of the object you clicked on, use hit.collider.transform.position;

If you need to turn this in to a string to store somewhere, use hit.point.ToString();

avatar image
1

Answer by dannyskim · Dec 05, 2011 at 05:29 PM

Your code looks perfectly fine. You may want to check your Physics settings inside your scene and the objects that you're working with.

When you look at a GameObject, the center of the object, in your case a cube, is well, in the center. So when you think about it, the hit.point that you're having the cube move to on the floor is trying to move it's center point to the hit.point on the floor. So, if you have gravity active in the scene and your objects are not kinematic, that is going to explain why your cube "spun out and jumped towards the camera."

You either have to A:

Create an offset on your Y position ( assuming Y is still your world up axis ) to compensate for the two box colliders overlapping each other.

Or B:

Make a simple primitive model that has it's pivot changed inside a 3D Modeling program and import it to compensate for this overlap as well. The reason why I say this is because the norm for the pivot point of most characters in a 3D game is at their feet to better symbolize where you want them to move and what their position is in relation to the ground that they're standing on. This also fixes the issue that you might be having.

What you may want to try is also setting the two objects to ignore collision on each other and set them both as kinematic on your Rigidbodies. If you don't have rigidbodies on your objects, this may explain why you're having spinning out of control problems as well.

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 slayer29179 · Dec 06, 2011 at 02:11 PM 0
Share

i am sorry it didn't work :/ its like the object its flying towards is the camera please check my game and help me i really need this for college...

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

7 People are following this question.

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

Related Questions

Spawn a mesh in terrain 1 Answer

Make an instantiated object match the slope of the terrain 1 Answer

Detecting the subobject of a mesh 1 Answer

Move 2 object by mouse to same position 1 Answer

Get raycast hit objects terrainData 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