Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ivan Kukucov · Sep 29, 2015 at 09:02 AM · transformmousepositionscreenpointtoray

Set position flom RaycastHit.point

 void Update () {
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         Physics.Raycast(ray, out hit);
         transform.position = hit.point;
     }

The object moves from the hit point to the origin point teleports to the hit point and moves to the camera and again and again. I want it to stay at the hit point and only move when the hit point changes.

Comment
Add comment · Show 6
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 Denvery · Sep 29, 2015 at 11:00 AM 0
Share

Hello, Ivan!

If I've tested your code, the object moves exactly with the mouse pointer. Please explain your goal more detailed

avatar image Ivan Kukucov Denvery · Sep 29, 2015 at 12:39 PM 0
Share

I have a plane that is just set to static nothing else done to it and a standard unity cube. The goal is to move the cube along the surface of the plane. The reason I'm training to do this with rays is because i want to be able to put other geometries on top of the plane that are not part of the plane and be able to move the cube along the other geometries surface. Hope this is more clear.

avatar image Denvery Ivan Kukucov · Sep 29, 2015 at 12:59 PM 0
Share

Also please explain what you get with your current code

Show more comments
Show more comments
avatar image Ivan Kukucov Denvery · Sep 29, 2015 at 01:49 PM 0
Share

This stops the cube moving to the camera but i need to be able to move in all axes and with this i lock the Z and i tryed it with the Y and again it stops the movment to the camera but i need to move in 3d space.

2 Replies

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

Answer by Ivan Kukucov · Sep 29, 2015 at 08:33 PM

So my problem was that i had a collider on the cube that the raycast is hitting so my hit.point was moving every time its hitting the collider. Well thanks for the help and sorry for wasting you time.

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 hexagonius · Sep 29, 2015 at 01:31 PM

wrap the raycast with an if and change the position in there. that's the place where you know you hit something.

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 Ivan Kukucov · Sep 29, 2015 at 01:38 PM 0
Share

I asume this is what you mean and it does not work.

 RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, out hit))
         {
             transform.position = hit.point;
         }

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

31 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 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

ScreenToWorldPoint offset affecting ScreenPointToRay ? 1 Answer

how can my move a obect alon with the player 2 Answers

InverseTransformVector not changing frame of reference? 0 Answers

Rotate a GameObject smoothly towards mouse 1 Answer

How to make an object move forward a set distance based on its rotation? 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