- Home /
Following Mouse Motion
In my 3D game, I have three main components to this mechanic: The Player which is pretty much acting as the pivot point, the object which rotates around the Player dictated by the mouse position, the mouse which can move anywhere on the screen but has the object follow it in its orbital circle around the player. Below is simply storyboard to display the effect im looking for in my 3D game. thanks in advance for any insight.
Is this a 2D or 3D game?
Does the camera angle vary with respect to the player?
Is the camera aligned with the axes?
Does the distance between the player and the object change?
Is the object's rotation fixed, or does it need to rotate to match the various angles with respect to the player?
What defines the orbit of the 'object'?
Is the following to be smoothed or immediate?
3D
camera is centered on player
no, its at an angle
no
it is fixed
the distance from the player to this object must remain the same
it needs to happen in real time and have no delay
Answer by robertbu · Dec 15, 2013 at 11:50 PM
Here is a solution given the criteria that you've outlined. Since the camera is not axes aligned, the code uses Unity's mathematical Plane object and does a Plane.Raycast() to figure out a hit point. This script goes on the 'object' and you need to initialize 'target' in the inspector by dragging and dropping your 'Player' object onto the variable. The distance is calculated from the original distance the 'object' is from the 'Player'.
#pragma strict
var target : Transform;
var dist : float;
function Start() {
dist = (target.position - transform.position).magnitude;
}
function Update() {
var plane = Plane(Camera.main.transform.forward, target.transform.position);
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var rayDist : float;
plane.Raycast(ray, rayDist);
var point = ray.GetPoint(rayDist);
transform.position = target.position + (point - target.position).normalized * dist;
}
This code creates a new plane each Update(). That's not very expensive, but it is unnecessary if the camera and the player do not move or rotate. If nothing moves or rotates, it could be done once in Start().
it looks like its working just like i was looking for, Only thing im looking to change is the axis on which its orbiting around.
The axis rotation is the normal of the plane. So to use a different axis, use a different vector as the first parameter when creating the plane. Note if there is an chance that the normal will be orthogonal to the camera, then you will also have to add logic to handle the case that the Raycast() fails. That is, the code should be restructured to:
if (plane.Raycast(ray, rayDist)) {
var point = ray.GetPoint(rayDist);
transform.position = target.position + (point - target.position).normalized * dist;
}
Ok So the last thing I feel that will bring it home, is how do i make it rotate along this new axis, in global coordinates? Right now its rotating correcting along the correct axis, but its slanted cus its in local coords. Also I appreciate your help thus far, you trully have been a huge help.
Its slanted because the axes of rotation is not aligned with the camera. It is not a local/global coordinate issue. Imagine you are holding a coin in your hand with and looking at the face. Now begin to flip that coin along any axis. As it rotates you get an angled ellipse and finally you are edge on to the coin. That is what happens if you select an axis other than Camera.forward for the plane. The circle (and it will be a circle since you specified that the the object distance remain the same), becomes angled with respect to the camera so that it appears as an ellipse.
You are looking for something specific here that I think I'm missing. There are so many different ways to view this problem, which is why I tried to be so specific with my comments/questions up front.
Im looking to have this rotation go around it in 3D space. I changed the axis to Camera.up, so it should rotate it parallel to the ground in the scene. right now since the camera is at an angle its rotating as if its going through the ground, not along it.
Your answer
Follow this Question
Related Questions
Object Rotating around center from Mouse Movement 1 Answer
Character movement on the other Object which is moving ? 0 Answers
Physics2D and curved movement 1 Answer
Moving the camera? 2 Answers