Hi, guys.
I’ve been working on a little project and i’ve gotten to a bit of a problem.
Basically i have a character that moves around the screen and rotates based on where the mouse is pointed. The problem i have is that the camera view does not rotate (this is a first person game which is why i want the camera to roatate too).
Here’s the script i have so far from the Camera, if anyone can help please do.
using UnityEngine;
using System.Collections;
public class FPCamera : MonoBehaviour {
public GameObject player;
private Vector3 offset;
int floorMask;
public GameObject MainCamera;
float camRayLength = 100f;
// Use this for initialization
void Start () {
offset = transform.position - player.transform.position;
//rotoffset = transform.Rotate - player.transform.Rotate;
floorMask = LayerMask.GetMask ("Floor");
}
// Update is called once per frame
void Update () {
transform.position = player.transform.position + offset;
//transform.Rotate = player.transform.Rotate + rotoffset;
}
void FixedUpdate(){
Turning ();
}
void Turning ()
{
// Create a ray from the mouse cursor on screen in the direction of the camera.
Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);
// Create a RaycastHit variable to store information about what was hit by the ray.
RaycastHit floorHit;
// Perform the raycast and if it hits something on the floor layer...
if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
{
// Create a vector from the player to the point on the floor the raycast from the mouse hit.
Vector3 playerToMouse = floorHit.point - transform.position;
// Ensure the vector is entirely along the floor plane.
playerToMouse.y = 0f;
// Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
Quaternion newRotation = Quaternion.LookRotation (playerToMouse);
// Set the player's rotation to this new rotation.
MainCamera.MoveRotation (newRotation); //(This is where the script should my character.)
}
}
}
I have a ‘floor’ setup and i know it works cause the character turns.
Thanks for any help.
I haven’t been doing this long so i’m not sure i called the camera view correctly but any help would be appreciated.