Noob/beginner here. I have searched this issue but haven’t found anything on it myself (apologies if this has already been solved/ answered on here). I’m making a super basic FP character controller following this tutorial (https://www.youtube.com/watch?v=PmIPqGqp8UY). It’s great and I’ve got it working, but I ran into some problems along the way and I’m really interested as to why. The final script looks like this (missing some code to lock the mouse to centre of screen):
START OF SCRIPT
``
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementScript_V3 : MonoBehaviour
{
[SerializeField] Transform playerChar_FP_POV = null;
[SerializeField] float mouseSensitivty = 3.5f;
float cameraPitch = 0.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PlayerCameraControl();
}
public void PlayerCameraControl()
{
Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
cameraPitch -= mouseDelta.y;
cameraPitch = Mathf.Clamp(cameraPitch, -90.0f, 90.0f);
playerChar_FP_POV.localEulerAngles = Vector3.right * cameraPitch;
transform.Rotate(Vector3.up * mouseDelta.x * mouseSensitivty);
}
}
``
END OF SCRIPT
Where the SerializeField Transform “playerChar_FP_POV” is a field which has had a camera (child object of “PlayerChar_Body”) dragged into it so its transform is accessible from the script (see attached image no.1Image showing what Serialize field transform is attached to
While I was experimenting, I found I couldn’t pass “mouseDelta.y” directly into the “PlayerChar_FP_POV” X rotation transform (second last line of script above) the same way I passed mouseDelta.x into the “PlayerChar_Body” Z rotation transform (last line of script above). I first had to create a float “cameraPitch” then assign its value to the inverse of mouseDelta.y (cameraPitch -= mousedelta.y), then clamp the range of “cameraPitch” with Mathf.Clamp (script above- in the last method). Then and only then could I successfully pass the value into “player_FP_Pov” to alter its X rotation transform. If I tried passing mousedelta.y straight into X transform of “playerChar_FP_POV” like so (look in the last method):
START OF SCRIPT
```
`using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementScript_V3 : MonoBehaviour
{
[SerializeField] Transform playerChar_FP_POV = null;
[SerializeField] float mouseSensitivty = 3.5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PlayerCameraControl();
}
public void PlayerCameraControl()
{
Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
playerChar_FP_POV.localEulerAngles = Vector3.right * mouseDelta.y;
transform.Rotate(Vector3.up * mouseDelta.x * mouseSensitivty);
}
}`
```
END OF SCRIPT
(passing mouseDelta.y straight into “playerChar_FP_POV” X rotation transform; leaving out clamping camera range and inverting input)
The X rotation transform of the “PlayerChar_FP_POV” (the players camera) is locked into place and does not respond to mouse Y axis movement (see picture). Parent object can follow mouse x axis on its Z axis, child object cannot follow mouse Y axis on it’s x axis
Again, I have the controller working fine. But purely from an educational perspective, I’m trying to figure out why this doesn’t work. I feel like understanding that would further my understanding a tad more. I hope this was clear enough, don’t want to waste peoples time!
Unity Version: 2022.3.22f1
Cheers for reading this!!!
Problem explained above, this section is not entirely applicable.
HergTheFlerg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.