- Home /
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.
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.
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;
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 convertfloat' 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.
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;
Thank you robertbu - that did it. I appreciate all the help One step closer to the end result I'm looking for!
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
Follow this Question
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