- Home /
3rd person camera problems
I'm in need of some help. I've been trying to get this 3rd person camera to rotate using the mouse. It isn't working,
I'm showing two different classes
This one is the camera follower: using UnityEngine; using UnityEngine.InputSystem;
public class CameraFollower : MonoBehaviour { [Header("Target")]
[Tooltip("Gameobject you want the camera to follow.")]
public GameObject target = null;
[Tooltip(" Speed for lerping - the less of this\nvalue, the faster the snap.")]
public float posCamSpeed = 0.125f;
[Tooltip(" Speed for rotating - the more of this\nvalue, the faster the snap.")]
public float rotCamSpeed = 0.125f;
[SerializeField, Tooltip("Offset for 3rd person")]
private Vector3 offset = new Vector3(0f, 0f, 0f);
private float cInputX;
private float cInputY;
private Vector2 CamInputRotation;
private void Start()
{
}
// LateUpdate is called after all Updates - this is to take into account all character movement
void LateUpdate()
{
CamInputRotation = target.GetComponent<PlayerMovement>().cameraInput;
//Splits
cInputX = cInputX + CamInputRotation.x;
cInputY = cInputY + CamInputRotation.y;
Vector3 desiredPosition = target.transform.position + offset;
Vector3 TargetsPosition = target.transform.position;
transform.position = Vector3.Slerp(transform.position, desiredPosition, posCamSpeed);
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(TargetsPosition - transform.position + new Vector3(cInputX, cInputY, 0f)), rotCamSpeed * Time.deltaTime);
Debug.Log(CamInputRotation);
}
}
And this one is the PlayerMovement script
using UnityEngine; using UnityEngine.InputSystem;
[RequireComponent(typeof (Rigidbody))] public class PlayerMovement : MonoBehaviour { private Rigidbody controller; private Vector3 playerVelocity; private bool groundedPlayer;
[SerializeField]
private float playerSpeed = 2.0f;
[SerializeField]
private float jumpHeight = 1.0f;
[SerializeField]
private float gravityValue = -9.81f;
private Vector2 movementInput = Vector2.zero;
private bool jumped = false;
public Vector2 cameraInput = Vector2.zero;
private void Start()
{
controller = gameObject.GetComponent<Rigidbody>();
}
public void OnMove(InputAction.CallbackContext context)
{
movementInput = context.ReadValue<Vector2>();
}
public void OnJump(InputAction.CallbackContext context)
{
jumped = context.action.triggered;
}
public void OnLooking(InputAction.CallbackContext context)
{
cameraInput = context.ReadValue<Vector2>();
}
public void OnCollisionStay(Collision collision)
{
if (collision.collider.tag == "Ground")
{
groundedPlayer = true;
}
else groundedPlayer = false;
}
void Update()
{
//if (groundedPlayer && playerVelocity.y < 0)
//{
// playerVelocity.y = 0f;
//}
Vector3 move = new Vector3(movementInput.x, 0, movementInput.y);
controller.AddForce(move * Time.deltaTime * playerSpeed, ForceMode.Impulse);
//if (move != Vector3.zero)
//{
// gameObject.transform.forward = move;
//}
// Changes the height position of the player..
if (jumped && groundedPlayer)
{
playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravityValue);
}
//playerVelocity.y += gravityValue * Time.deltaTime;
//controller.Move(playerVelocity * Time.deltaTime);
//Debug.Log(cameraInput);
}
}
I do hope someone arrives soon.
Anyway, I debugged the cameraInput variable from the PlayerMovement script. I'm not sure why, but it's not showing any input being made, no matter how fast I move my mouse. I checked the Input Action Map, and everything's set up right (Pass Through - Vector2 - Mouse (Delta).
I'm just not sure what's wrong with this?
Please clean up the code and post only the relevant parts. It would make solving the problem much easier and quicker. The only relevant piece of information here is that you weren't able to get mouse input from the new input manager. Next step would be to show relevant information about that, like the input actions, the input component, reading and storing the input.
Your answer
Follow this Question
Related Questions
Change input from mouse to game pad 1 Answer
clamping camera movement to map size and camera corners 0 Answers
My Scene View is Fine but my Game View is just black but the Camera Works 0 Answers
Automatic Camera Rotation in the 3D Game Kit 0 Answers
Multiple Players on one Computer/Console 5 Answers