- Home /
Question by
Zawdie_Zombie · Dec 05, 2021 at 05:59 PM ·
camera-movementplayer movement
So how do I rotate my camera to my player
So every time I want to right click, I want to move my camera to where my mouse is, and rotate my player to my camera. But instead when I right click my camera just go crazy like a madlad. my camera is a isometric btw.
Here my code yall madlads. public CharacterController controller;
public float speed = 12f;
public float gravity = -9.18f;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
Vector3 velocity;
bool isGrounded;
public Transform maincamera;
void Update()
{
transform.LookAt(maincamera);
transform.LookAt(maincamera, Vector3.left);
}
float AngleBetweenTwoPoints(Vector3 a, Vector3 b)
{
return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetKey("left shift") && isGrounded)
{
speed = 20f;
}
else
{
speed = 12f;
}
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
public class cameraSystem : MonoBehaviour
{
public Camera cam;
public float speed = 5.0f;
Vector2 movement;
Vector2 mousePos;
void Update()
{
if (Input.GetMouseButtonDown(1))
{
Debug.Log("Aiming");
transform.position = new Vector3(speed * Time.deltaTime, 0, 0);
// If the right button on the mouse is hold down or click it moves the camera to it position.
}
}
}
Comment
Answer by UnityPlum · Dec 11, 2021 at 08:03 PM
// Have "player" as a variable of type gameobject and assign in inspector
Vector3 playerPosition = player.transform.position;
// This class is assigned to the camera object
Vector3 cameraPosition = transform.position;
Vector3 dirBetween = (playerPosition - cameraPosition).normalized;
// Rotate camera to look at player
transform.forward = dirBetween;
// To rotate player to look at camera: player.transform.forward = -dirBetween;
Your answer
![](https://koobas.hobune.stream/wayback/20220613050234im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem in rotating camera around a moving player 1 Answer
Simple Third Person Camera-Relative Movement with WASD - kind of working but need a fix 2 Answers
Camera script for ball not working as intended, looking for some tips, or advise. 0 Answers
Camera problem 2 Answers
Trying to get the camera to orbit, but also steer the player when moving. 0 Answers