Hello!
I’m new to coding and unity in general and I’m trying to create a tank controlled character that has a free look mouse.
I’ve followed Filmstorms tutorial on creating a 3rd person setup with camera collision that is 75% what I want. I’d like to take the script a bit further and get the camera to turn with the character. Using the character as a pivot point and get the camera to look at the players forward most of the time.
Original Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour {
public float CameraMoveSpeed = 120.0f;
public GameObject CameraFollowObj;
Vector3 FollowPOS;
public float clampAngle = 80.0f;
public float inputSensitivity = 150.0f;
public GameObject CameraObj;
public GameObject PlayerObj;
public float camDistanceXToPlayer;
public float camDistanceYToPlayer;
public float camDistanceZToPlayer;
public float mouseX;
public float mouseY;
public float finalInputX;
public float finalInputZ;
public float smoothX;
public float smoothY;
private float rotY = 0.0f;
private float rotX = 0.0f;
void Start () {
Vector3 rot = transform.localRotation.eulerAngles;
rotY = rot.y;
rotX = rot.x;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update () {
float inputX = Input.GetAxis("RightStickHorizontal");
float inputY = Input.GetAxis("RightStickVertical");
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
finalInputX = inputX + mouseX;
finalInputZ = inputY + mouseY;
rotY += finalInputX * inputSensitivity * Time.deltaTime;
rotX += finalInputZ * inputSensitivity * Time.deltaTime;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
//rotY = Mathf.Clamp(rotY, -clampAngle, clampAngle); Commented out until rotation is achieved.
Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
transform.rotation = localRotation;
}
private void LateUpdate()
{
CameraUpdater();
}
void CameraUpdater()
{
//set target object to follow
Transform target = CameraFollowObj.transform;
//move towards the game object that is the target
float step = CameraMoveSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
All that I have tried have cause the camera to rotate around itself with the character and/or orbit around the character, but keeping the original view direction and not the rotated characters view.
If I’m not mistaken I need to add something or change how CameraUpdater() handles the movement. It all ready follows the lateral movement but not the horizontal rotation (vertical pivot around the character).
Thank you in advance.