- Home /
fps camera facing 90 degrees the wrong way, C# coding issue.
Hey all, I'm sure this is an easy fix and I have just dome something silly but i cant find the answer on Google. I'm doing a simple fps to relearn how to use everything and for some reason the main camera is always 90 degrees left of the character model. the movement still works like the camera is pointing the right way and the pitch and yaw functionally work the fine though.
if that's not making sense, imagine if you turn your head is permanently facing to the left. here is the code and thanks for any help.
using UnityEngine;
using System.Collections;
public class FirstPersonController : MonoBehaviour {
public float movementspeed = 3.0f;
public float strafespeed = 2.0f;
public float mouseSensitivityH = 3.0f;
public float mouseSensitivityV = 3.0f;
public float pitchRange = 60.0f;
float desiredPitch = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
//Rotation
float rotYaw = Input.GetAxis("Mouse X") * mouseSensitivityH;
transform.Rotate(0, rotYaw, 0);
desiredPitch -= Input.GetAxis("Mouse Y") * mouseSensitivityV;
desiredPitch = Mathf.Clamp(desiredPitch, -pitchRange, pitchRange);
Camera.main.transform.localRotation = Quaternion.Euler(desiredPitch, 0, 0);
// Movement
float forwardSpeed = Input.GetAxis("Vertical") * movementspeed * -1;
float sideSpeed = Input.GetAxis("Horizontal") * strafespeed;
Vector3 speed = new Vector3( forwardSpeed, 0, sideSpeed);
speed = transform.rotation * speed;
CharacterController cc = GetComponent<CharacterController>();
cc.SimpleMove( speed );
}
}
thanks for the answer, i dont understand what you mean by "speed is Vector3, transform.rotation is Quaternion and you are mulitplying the two" sorry.
i tried moving the cc declaration to start and then into the public declarations up the top and both gave me a 'cc is not declared in current script' error.
the camera was rotated but removing the rotation did nothing.
For the cc, put this in your var declarations at top
private CharacterController cc;
in Start put
cc = GetComponent<CharacterController>();
then in Update() get rid of the line that's defining it
If you multiply a Banana * a Ford Pickup, what would you get?
ah great that works without errors now. thanks btw:
this is what you get when you multiply a Banana and a Ford Pickup
Answer by getyour411 · Feb 18, 2014 at 03:32 AM
Couple of general issues
1) speed is Vector3, transform.rotation is Quaternion and you are mulitplying the two
2) Move the cc declaration out of Update() so that it doesn't have to do that every frame
3) if your camera is a child of the player, make sure it hasn't been rotated
Glad you got it sorted out (and kudos on the pic) I'll change this to an answer.
only problem now is that for some reason the editer preview shows that the camera is facing backwards but in the game window thingy its facing the right way... Edit: wait never$$anonymous$$d i got it ^^;
If you happen to see this again, please click Accept Solution to close this.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
error CS1061 anybody might help me? 1 Answer
Distribute terrain in zones 3 Answers
C# how to have a part of an object rotate while animation is being played 2 Answers