How to focus camera on back of object when key is pressed.
I'm using unity 5.5.
And here are my scripts:
Here is my ThirdPersonCamera.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCamera : MonoBehaviour {
private const float Y_ANGLE_MIN = 0.0f;
private const float Y_ANGLE_MAX = 50.0f;
public Transform lookAt;
public Transform camTransform;
private Camera cam;
private float distance = 10.0f;
private float currentX = 0.0f;
private float currentY = 0.0f;
private float sensitivityX = 4.0f;
private float sensitivityY = 1.0f;
private void Start()
{
camTransform = transform;
cam = Camera.main;
}
private void Update()
{
if (Input.GetKey(KeyCode.Mouse1))
{
currentX += Input.GetAxis("Mouse Y");
currentY += Input.GetAxis("Mouse X");
Debug.Log("Cam position:" + camTransform.position.ToString());
Debug.Log("Cam rotaion:" + camTransform.rotation.eulerAngles);
}
currentX = Mathf.Clamp(currentX, Y_ANGLE_MIN, Y_ANGLE_MAX);
}
private void LateUpdate()
{
Vector3 dir = new Vector3(0,0, -distance);
Quaternion rotation = Quaternion.Euler(currentX, currentY, 0);
camTransform.position = lookAt.position + rotation * dir;
camTransform.LookAt(lookAt.position );
if (Input.GetKey(KeyCode.W))
{
Quaternion characterRotation = lookAt.transform.rotation;
currentY = 0.0f;
currentX = 25.0f;
Debug.Log("Character rotation: " + characterRotation.eulerAngles);
Debug.Log("Cam rotation: " + cam.transform.rotation.eulerAngles);
}
//Debug.Log("Y: " + currentY + " X: " + currentX);
}
}
Here is my CharacterMovement.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour
{
private CharacterController controller;
private float verticalVelocity;
private void Start()
{
controller = GetComponent<CharacterController>();
}
private void Update()
{
//inputs = location variable
Vector3 location = Vector3.zero;
location.x = Input.GetAxis("Horizontal");
if (controller.isGrounded)
{
verticalVelocity = -1;
if (Input.GetButton("Jump"))
{
verticalVelocity = 10;
}
if (Input.GetKey(KeyCode.W))
{
transform.position += transform.forward * Time.deltaTime * CharacterGlobals.CharacterMovementSpeed;
//Debug.Log("Location: " + transform.position);
//Debug.Log("Character speed is: " + CharacterGlobals.CharacterMovementSpeed);
}
if (Input.GetKey(KeyCode.S))
{
transform.position -= transform.forward * Time.deltaTime * CharacterGlobals.CharacterMovementSpeed;
// Decrease character speed by - CharacterGlobals.CharacterMovementSpeed -= 1;
// Debug.Log("Location: " + transform.position);
}
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(0, -3, 0);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(0, 3, 0);
}
}
else
{
verticalVelocity -= 14.0f * Time.deltaTime;
}
location.y = verticalVelocity;
controller.Move(location * Time.deltaTime);
}
}
When i press the right button of the mouse and move camera is rotating perfectly. Everything is working fine except the thing i want to achieve.
If you look at my ThirdPersonCamera.cs
script you will see condition catching when button W
is pressed.
What i want to achieve is to rotate and position the camera at the back of the object and setting rotation.x
of the camera to 25 so it looks a bit from above.
However i'm not able to achieve it. Can you please point where is mistake and how can i achive the goal ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612124829im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera always behind player 0 Answers
Code Optimization 1 Answer
Recognize when ever camera looks up and turns back down 0 Answers
why the camera rotate in the z axis 0 Answers
Rotating Camera on click and drag 0 Answers