I want to set the camera 7.5f units away from a player based on where the mouse is in relation to the player.
Warning: Before you answer the question or leave a reply, I ask that you view the picture that I put towards the end of the question so you have an idea of what I am asking for. It is a diagram in which "Point A" is where the camera should be. It is a "for instance," meaning it is an example of what could be an outcome of the mouse, player, and camera.
I found the distance between the mouse and the player:
//Distance: mouse -> player
D1 = Mathf.Sqrt(Mathf.Pow(mousePos.x - player.transform.position.x, 2)
+ Mathf.Pow(mousePos.z - player.transform.position.z, 2));
I set up the statement that will find the relation between the mouse and the player:
if(mousePos.x - player.transform.position.x >= 0)
{
CamX = //Put Code Here
}
if (mousePos.x - player.transform.position.x <= 0)
{
CamX = //Put code Here
}
if (mousePos.z - player.transform.position.z >= 0)
{
CamZ = //Put Code Here
}
if (mousePos.z - player.transform.position.z <= 0)
{
CamZ = //Put Code Here
}
Here are the variables that I am using:
private float D1;
private float D2;
private float D3;
private float CamX;
private float CamZ;
Here is the section of code that I am working on. Don't take it as my full work for it is merely a brainstorm of code:
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
mousePos = new Vector3(hit.point.x, 0.8f, hit.point.z);
//Distance: mouse -> player
D1 = Mathf.Sqrt(Mathf.Pow(mousePos.x - player.transform.position.x, 2)
+ Mathf.Pow(mousePos.z - player.transform.position.z, 2));
if(mousePos.x - player.transform.position.x >= 0)
{
CamX = //Put Code Here
}
if (mousePos.x - player.transform.position.x <= 0)
{
CamX = //Put code Here
}
if (mousePos.z - player.transform.position.z >= 0)
{
CamZ = //Put Code Here
}
if (mousePos.z - player.transform.position.z <= 0)
{
CamZ = //Put Code Here
}
//Distance: mouse -> camera
D2 = D1 + 7.5f;
//Distance: player -> camera
D3 = Mathf.Sqrt(Mathf.Pow(player.transform.position.x - CamX, 2)
+ Mathf.Pow(player.transform.position.z - CamZ, 2));
...
Here is a picture diagraming how I want everything to be positioned:
How can I find the position of the camera (point(CamX, CamZ)) that is 7.5f units away from the player and passes through the coordinates of the mouse (point(mousePosX, mousePosZ)).
Couple different ways you could go about this, but one way would be to get the direction vector from the mouse to the player position, and then add that onto your player position multiplied by 7.5 Some example code here:
// assuming Y is the same for player and mouse
Vector3 normDirection = ((Vector3)playerPos - (Vector3)mousePos).normalized; // unit vector from mouse to player
Vector2 extendedDir = new Vector2(normDirection.x * 7.5f, normDirection.z * 7.5f);
Vector2 cameraPos = new Vector2(playerPos.x + extendedDir.x, playerPos.z + extendedDir.y); // extended vector added to player pos
Camera.main.transform.position = new Vector3(cameraPos.x, .8f, cameraPos.y); // set camera position
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.