- Home /
The Camera is facing backwards of the Character
I don't know why but the camera is Not facing the same way as the Model any Help?
The Model Is Facing the Right Way
But Here's what It Looks Like
Yeah its facing backwards I Don't Know If Its The Code
using UnityEngine;
public class FPSFirstPerson : MonoBehaviour
{
public Transform camera;
public Rigidbody rb;
public float camRotationSpeed = 5f;
public float cameraMinimumY = -60f;
public float cameraMaximumY = 75f;
public float rotationSmoothSpeed = 10f;
public float walkSpeed = 9f;
public float runSpeed = 14f;
public float maxSpeed = 20f;
public float jumpPower = 30f;
public float _extraGravity = 45;
float bodyRotationX;
float camRotationY;
Vector3 directionIntentX;
Vector3 directionIntentY;
float speed;
public bool grounded;
void Update()
{
LookRotation();
Movement();
extraGravity();
GroundCheck();
if(grounded && Input.GetButtonDown("Jump"))
{
Jump();
}
}
void LookRotation()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
bodyRotationX += Input.GetAxis("Mouse X") * camRotationSpeed;
camRotationY += Input.GetAxis("Mouse Y") * camRotationSpeed;
camRotationY = Mathf.Clamp(camRotationY, cameraMinimumY, cameraMaximumY);
Quaternion camTargetRotation = Quaternion.Euler(-camRotationY, 0, 0);
Quaternion bodyTargetRotation = Quaternion.Euler(0, bodyRotationX, 0);
transform.rotation = Quaternion.Lerp(transform.rotation, bodyTargetRotation, Time.deltaTime * rotationSmoothSpeed);
camera.localRotation = Quaternion.Lerp(camera.localRotation, camTargetRotation, Time.deltaTime * rotationSmoothSpeed);
}
void Movement()
{
directionIntentX = camera.right;
directionIntentX.y = 0;
directionIntentX.Normalize();
directionIntentY = camera.forward;
directionIntentY.y = 0;
directionIntentY.Normalize();
rb.velocity = directionIntentY * Input.GetAxis("Vertical") * speed + directionIntentX * Input.GetAxis("Horizontal") * speed + Vector3.up * rb.velocity.y;
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
if(Input.GetKey(KeyCode.LeftShift))
{
speed = runSpeed;
}
if(!Input.GetKey(KeyCode.LeftShift))
{
speed = walkSpeed;
}
}
void extraGravity()
{
rb.AddForce(Vector3.down * _extraGravity );
}
void GroundCheck()
{
RaycastHit groundHit;
grounded = Physics.Raycast(transform.position, -transform.up, out groundHit, 1.25f);
}
void Jump()
{
rb.AddForce(Vector3.up * jumpPower, ForceMode.Impulse);
}
}
Answer by Camden9 · Sep 16, 2020 at 05:57 PM
It is most likely something small like a - (or its positive when it should be negative) somewhere on your camera's transform or rotation in your script. are they both facing the correct direction in your scene before you press play, then it turns around? or is it backwards in the editor too? But I am saying all this as someone who doesn't know a lot, just trying to help if I can!
I Did Correct my Character Facing the Z Axis For the Animation Could Work So $$anonymous$$y Character is Fine. Yes, When I press play they both go separate directions, But the camera just faces the opposite Direction
So I started a test project with a capsule, a camera, and your code on it to hopefully better find the issue. I noticed a couple things, one issue is that when dealing with your clamping your camera, you are clamping the y rotation, when you should be clamping the X (X rotation on the camera deals with looking up and down, Y is side to side) Also I can't find anywhere in the script that you are rotating the camera to match the body rotation, which is why your camera is looking backwards. The way that you wrote the script is good, but I think that you just missed a few points, I would recommend following a tutorial on fps camera in unity, (there are like a billion) then going back to this script and trying to make it work! I would urge you to try to fix this script though, because finding what you did wrong and fixing it will teach you a lot!
Your answer
Follow this Question
Related Questions
Slime script? 1 Answer
How to I put a speed limit on my character's movement? 1 Answer
Smooth jump script? 1 Answer