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 DoctorWhy · Dec 06, 2012 at 06:54 PM · cameramovementmouse

GameObject follow the mouse

I need to have a GameObject follow the mouse on the screen perfectly, and move flatly (see image below). The camera can be pretty much any angle, so right, forward, and up may be different at any given time.

This code works, except it causes the gameobject to move in a radius around the camera, instead of making it look like it is at a consistent depth away from the camera (the model looks like it gets bigger around the edges of the camera).

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 gameobject.transform.position = ray.GetPoint(5);

How would I go about doing this?

EDIT:

Currently, if the camera is looking straight at the model, the green is how the model is currently moving relative to the camera, the white dot. The white lines is the camera projection. The red line is how the model should move. No matter the angle of the camera, the model should be moving in a flat plane relative to the camera, instead of the radius arc that is done with the code above.

So how would I dynamically calculate the radius from the camera to make sure the model moves on a flat plane relative to the camera??

alt text

ex4.jpg (72.9 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by nsejosh · Dec 10, 2012 at 05:58 PM

adapted from the angrybots sample-

call CalcDesiredMoveTargetFromMouse when you want the player to start moving towards the mouse ( probably in your Update function, or when the mouse is clicked, depending on whether you want to only do this at mouse click ) and then set your game object's transform to mDesiredWorldPosition however you want ( smoothly or immediately, etc )

 Vector3 GetPlaneRayIntersection ( Plane inPlane, Ray inRay )
 {
     float dist;
     inPlane.Raycast( inRay, out dist );
     return inRay.GetPoint( dist );
 }
         
 Vector3 ScreenPointToWorldPointOnPlane ( Vector3 inScreenPoint, Plane inPlane, Camera inCamera ) 
 {
     // Set up a ray corresponding to the screen position
     Ray ray = inCamera.ScreenPointToRay( inScreenPoint );
     
     // Find out where the ray intersects with the plane
     return GetPlaneRayIntersection( inPlane, ray );
 }
         
 void CalcDesiredMoveTargetFromMouse()
 {
     Vector3 cursorScreenPosition = Input.mousePosition;
         
     //should we build a plane around where the character
     Transform myTransform = GetComponent< Transform >();
         
     Plane playerMovementPlane = new Plane( myTransform.up, myTransform.position );
         
     mDesiredWorldPosition = ScreenPointToWorldPointOnPlane( cursorScreenPosition, playerMovementPlane, Camera.mainCamera );
         
     mHasDesiredWorldPosition = true;        
 }
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 azmat786n · Dec 06, 2012 at 07:52 PM

//apply this on object that you want to move with mouse

 function Update() {
    pos = Input.mousePosition;
    pos.z = cube.transform.position.z- mainCamera.transform.position.z;
    transform.position = mainCamera.ScreenToWorldPoint(pos);
 }



//try it i hope it will work.

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 DoctorWhy · Dec 06, 2012 at 07:59 PM 0
Share

Wouldn't that only work for 2D? Z isn't always the depth of the gameObject...

avatar image
0

Answer by XienDev · Dec 10, 2012 at 05:41 PM

There are some ways...

1) is to get crossproduct of camerapos, gamobject pos, and gameobject.up. then project your ray to cross product; (ray intersection point)

2) create a plane representing yout gameobject, and move along it

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 DoctorWhy · Dec 10, 2012 at 05:51 PM 0
Share

Could you please provide example code for 1?

I thought of number 2, but that sounds more like a hack than a solution (using a box collider intersecting the center point that moves with the camera and checks for a collision and moves the object there).

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

12 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

Related Questions

Camera movement question 1 Answer

How do I implement double click feature to move camera back to previous position? 1 Answer

How do I move the player towards the direction of the camera using cinemachine? 0 Answers

Top down movement and rotation 2 Answers

How to select one prefab from more ? 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