- Home /
Object rotating around object at two levels
So im trying to have a spotlight move around the player in a fixed manner with a 3rd person perspective. The Spotlight moves to follow where the mouse is relative to its fixed rotation point around the player. On top of it the spot light is constantly pointing outwards and should never face the player. Lastly Im hoping to be able to make the spotlight look up or down if necessary, also be able to move the spotlight up or down if possible. I have simple version that does everything but the adjustments to the look direction and the start height of the spotlight, so i think im doing something wrong. Any help is greatly appreciated, Thanks in advance.
public void lightMouseMovement()
{
float distToLight;
float distToLookAt;
Plane playerPlane;
float rayDist = 0f;
//Distance from Player to Light Object
distToLight = (player.transform.position - this.transform.position).magnitude;
//Distance from Player to Light Look At Object
distToLookAt = (player.transform.position - lookAtObject.transform.position).magnitude;
playerPlane = new Plane(Vector3.up, player.transform.position);
//Creates ray from Screen to Point
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//Sets rayDist to distance from Camera to movementPlane OR cancels out(parallel or opposite direction)
if (playerPlane.Raycast(ray, out rayDist))
{
//Get Collision Point in world space
Vector3 point = ray.GetPoint(rayDist);
//Move to new Rotational Posistion
this.transform.position = player.transform.position + (point - player.transform.position).normalized * distToLight;
}
if (playerPlane.Raycast(ray, out rayDist))
{
//Get Collision Point in world space
Vector3 point = ray.GetPoint(rayDist);
//Move to new Rotational Posistion
lookAtObject.transform.position = player.transform.position + (point - player.transform.position).normalized * distToLookAt;
}
//Make Height Adjustment to new point
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y + lightYAdj, this.transform.position.z);
lookAtObject.transform.position = new Vector3(lookAtObject.transform.position.x, lookAtObject.transform.position.y + lookAtYAdj, lookAtObject.transform.position.z);
//Turn Light to Look at Look At Object
this.transform.LookAt (lookAtObject.transform.position);
Answer by pointcache · Jan 04, 2014 at 07:58 AM
Maybe raycast on screen, take hit point, subtract vectors, apply to pointlight.
right now I Im shooting a ray onto a premade Plane, getting the point from that, subtracting that hit point from the players position then adding it to the current players position to get its new location. To make sure its fixed im normalizing the (hit-Player) and multiplying it by the distance between the (player-light) position.
The main problem is making the spotlight look up or down on this track, or even raising the track. I could rotate where its looking but as it rotates it wouldnt rotate around as well, so it would hit the player. I have it now so im rotating two objects, one the spotlight the other a lookat point so it can rotate around while looking outwards.
Try to make a "rig" a set up of parented objects where each in hierarchy is affected differently providing the final motion you need, that's what i did with complex camera setups and many other things.
You should be able to solve this problem simpler than using a second, look-at object. Posting your code would help the folks in the list give you an accurate answer.
I just updated the post to include my current code. I originally thought that as well but I was having no luck on getting this result.