- Home /
Is my camera script looking at the scene's transform and not the player(parent)'s?
I'm trying to nail down the feel of a first person player character and decided to use a rigid body instead of a character controller. When I added new scripts for a rigid body, I got the camera and X axis clamp working again, but now the player won't move in the direction of it's own Vector3.forward.
In the scene view, I can see that the camera and the player game object are both rotating by the mouse x input, but using vertical and horizontal inputs will only move the player game object along the scene's X and Z axes.
Is something in my script skipping over the player and looking at the scene's transform or am I just doing this weird?
This is my player move script:
public float movementSpeed;
private Rigidbody rb;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
PlayerMove();
}
void PlayerMove()
{
float vInput = Input.GetAxis("Vertical");
float hInput = Input.GetAxis("Horizontal");
Vector3 tempVect = new Vector3(hInput, 0, vInput);
tempVect = tempVect.normalized * movementSpeed * Time.deltaTime;
rb.MovePosition(transform.localPosition + tempVect);
}
}
And this is my camera script:
{
[SerializeField] private Transform playerBody;
public float mouseSensitivity;
private float xAxisClamp;
// Use this for initialization
void Start()
{
LockCursor();
xAxisClamp = 0.0f;
}
private void LockCursor()
{
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
CameraRotation();
}
private void CameraRotation()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xAxisClamp += mouseY;
if (xAxisClamp > 80.0f)
{
xAxisClamp = 80.0f;
mouseY = 0.0f;
ClampXAxisRotationToValue(280.0f);
}
else if (xAxisClamp < -80.0f)
{
xAxisClamp = -80.0f;
mouseY = 0.0f;
ClampXAxisRotationToValue(80.0f);
}
transform.Rotate(Vector3.left * mouseY);
playerBody.Rotate(Vector3.up * mouseX);
}
private void ClampXAxisRotationToValue(float value)
{
Vector3 eulerRotation = transform.eulerAngles;
eulerRotation.x = value;
transform.eulerAngles = eulerRotation;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613005117im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why my character moves left when i press right key ? Controls are upside down? 2 Answers
How do I jump? 0 Answers