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 Entevily · Jun 16, 2014 at 06:59 PM · raycastpositionmouserelativegolf

Find point between object and mouse position

How do I rotate the 1st object around a 2nd object based off of the mouse position relative to the Second object.

Hard to describe so please check out the picture.

alt text

So, I need to rotate the object (ORANGE) around a Ball (RED) dependent on the position of the Mouse (BLUE) in 3d space.

I was thinking the best way to do this would be to get a raycast from the mouse position to a plane. Then get a second raycast (GREEN) from the ball to the mouse position. And then position the Object (ORANGE) a certain distance down the raycast.

I guess, I'm wondering if this is the easiest way to do this or am I missing something even easier.

If this is the easiest then I'll move forth and try to do this.

If you don't understand what I'm talking about still, think MiniGolf. Any assistance in this matter is greatly appreciated. Thanks.

raycast example.png (1.7 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Jun 16, 2014 at 07:07 PM

You don't need a raycast. Your green line is the vector resultant of blue minus red. I think that this could (not tested) explain better.

 // compute green vector
 var delta = blue.transform.position - red.transform.position;
 // get its dir
 var dir = delta.normalized;
 // get its length
 var mag = delta.magnitude;
 
 // place the orange in the green vector direction in distance 10% of its length relative to blue position
 orange.transform.position = dir * mag * 0.1f + blue.transform.position;
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 Entevily · Jun 16, 2014 at 08:27 PM 0
Share

Sisso, I love your quick response but after everything I put in place I still get the following errors.

Assets/Scripts/AlignPaddle.cs(47,58): error CS1502: The best overloaded method match for UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments > Assets/Scripts/AlignPaddle.cs(47,58): error CS1503: Argument #2' cannot convert float' expression to type UnityEngine.Vector3'

Here's my code, its in C#:

 using UnityEngine;
 using System.Collections;
 
 public class AlignPaddle : $$anonymous$$onoBehaviour
 {
         public GameObject ball;
         public GameObject paddle;
         public Camera theCamera;
 
         Vector3 ballPosition = new Vector3 ();
         Vector3 paddlePosition = new Vector3 ();
         Vector3 mousePosition = new Vector3 ();
     
         int layer;
         int layer$$anonymous$$ask;
     
         // Use this for initialization
         void Start ()
         {
                 layer = 8;
                 layer$$anonymous$$ask = 1 << layer;
         }
     
         // Update is called once per frame
         void Update ()
         {
                 // Grab the balls position
                 ballPosition = ball.transform.position;
 
                 // Throw a raycast from mouse position to "playing" feild" and record the hit point as mouse position.
                 RaycastHit hit;
                 Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                 if (Physics.Raycast (ray, out hit, 100, layer$$anonymous$$ask)) {
                         mousePosition = hit.point;
                 }
 
                 // Compute vector between mouse and ball
                 Vector3 delta = mousePosition - ballPosition;
                 
                 // Get Vectors direction
                 Vector3 dir = delta.normalized;
                 
                 // Get the length (distance between mouse and ball.)
                 float mag = delta.magnitude;
                 
                 // compute the distance of orange from blue as 10% (0.1f)
                 float distance = Vector3.Lerp (Vector3.zero, mag, 0.1f) * mag;
                 
                 // place the orange in the green vector direction in distance 10% of its length relative to blue position
                 paddlePosition = dir * distance + mousePosition;
                 paddle.transform.position = paddlePosition;
         }
 }
 




Please let me know if you see something I have done wrong. Obviously converting from javascript to c# isn't always perfect. I know it's saying that I cannot convert floats to Vector3's and ViceVersa but when I change the variable types I get even more errors. Let me know thoughts. Thanks.

avatar image robertbu · Jun 16, 2014 at 08:50 PM 1
Share

You are mixing and matching Vector3 and float in this line:

 float distance = Vector3.Lerp (Vector3.zero, mag, 0.1f) * mag;

If you wanted to use Lerp, it would be:

 float distance = $$anonymous$$athf.Lerp(0.0f, mag, 0.1f);

But all you really have to do is:

 float distance = 0.1f * mag;
avatar image Entevily · Jun 16, 2014 at 08:56 PM 0
Share

Thank you robertbu - that did it. I appreciate all the help One step closer to the end result I'm looking for!

avatar image Sisso · Jun 16, 2014 at 09:18 PM 0
Share

Sorry guys, I have received email notification only now.

Thanks @robertbu for the fix. I was with Vector3.Lerp(origin, destination, percent) in the head :P

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Instantiating Object On Map 1 Answer

Fire weapon from turret to mouse position (3D Isometric shooter) 1 Answer

Having an object point towards mouse position in 3d world 1 Answer

Spawn Objects Where i click 2 Answers

2d Position to 3d Position 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