Fairly new to Unity scripting and have been trying to create a third person system w/ Tank controls (Horizontal Rotate with Arrow Keys) while also allowing some camera movement with the mouse.
My issue is I can’t seem to work out how to use directional keys for character rotation (which is linked to main camera) while ALSO allowing camera orbiting with the mouse. Currently I can rotate the camera with the mouse vertically only. I’m not sure how to differentiate “Actual Player Rotation” and “Camera Rotation” if that makes sense? My Character Controller / Camera Controller scripts as they are right now are below:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCameraController : MonoBehaviour
{
public float RotationSpeed = 1;
public Transform Target, Player;
float keyX, mouseY;
void Start()
{
}
void LateUpdate()
{
CamControl();
}
void CamControl()
{
keyX += Input.GetAxis("Horizontal") * RotationSpeed;
mouseY -= Input.GetAxis("Mouse Y") * RotationSpeed;
mouseY = Mathf.Clamp(mouseY, -5, 20);
transform.LookAt(Target);
Target.rotation = Quaternion.Euler(mouseY, keyX, 0);
Player.rotation = Quaternion.Euler(0, keyX, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCharacterController : MonoBehaviour
{
public float movementSpeed = 5;
public float rotationSpeed = 50;
private Transform cameraTransform;
private void FixedUpdate()
{
transform.position += transform.rotation * new Vector3(0, 0, Input.GetAxis("Vertical")) * movementSpeed * Time.fixedDeltaTime;
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Horizontal") * rotationSpeed * Time.fixedDeltaTime, 0);
}
}
“Horizontal” controls the player rotation currently, “Mouse Y” controls vertical camera movement. I feel like I’ll need to rework my char controller to handle the rotation instead but wondered if there was something simple I was missing with the current setup.
Then why is line 24 in your camera controller also using “Horizontal” ?
Do you mean you want automatic horizontal camera motion, eg., “stay behind the tank?” That’s another thing, usually handled by a SmoothFollow script on the camera, or else all the Kool Kids today are using Cinemachine from the Unity Package Manager because it does a lot of really spiffy camera stuff.
Okay, I realised a big part of my issue was that my camera controller was handling my Player rotation based on the camera rotation, and I basically need it to work the other way around. My issue it that I both want to have the camera follow the players rotation AND have the seperate ability to “look around” to a fixed extent while the cameras position is fixed behind the player at all times.
I have set up a new, cleaner tank controller in place of my original character controller and now have a player that can navigate the scene using tank controls and a camera that follows the players Location but NOT the target rotation.
How can I add the follow rotation and “look around” mechanics to my camera controller?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankControls : MonoBehaviour
{
public GameObject Player;
public bool isMoving;
public float horizontalMove;
public float verticalMove;
void Update()
{
if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
{
// Player.GetComponent<Animator>().Play("Walk"))
isMoving = true;
horizontalMove = Input.GetAxisRaw("Horizontal") * Time.deltaTime * 150;
verticalMove = Input.GetAxisRaw("Vertical") * Time.deltaTime * 3.9f;
Player.transform.Rotate(0, horizontalMove, 0);
Player.transform.Translate(0, 0, verticalMove);
}
else
{
isMoving = false;
// Player.GetComponent<Animator>().Play("Idle"))
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ThirdPersonCameraController : MonoBehaviour
{
public float RotationSpeed = 1;
public Transform Target, Player;
float mouseX, mouseY;
void Start()
{
}
void LateUpdate()
{
CamControl();
}
void CamControl()
{
mouseX += Input.GetAxisRaw("Mouse X") * RotationSpeed;
mouseY -= Input.GetAxisRaw("Mouse Y") * RotationSpeed;
mouseY = Mathf.Clamp(mouseY, -15, 20);
transform.LookAt(Target);
Target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
}
}
Almost anytime someone wants to “add” something to a script, that’s a clear “tell” that they’re overlooking the fact that this is software engineering, not cake baking. You don’t just “add some stuff,” especially complicated stuff.
Since camera code is some of the hardest code to get right, again I suggest as I did above,
Why write all that crud again and again and again?! That’s not game development. That’s punishment.