- Home /
ThirdPersonShooter-Camera-Script Optimization
So I've build a thirdpersonshooter-camera-script and I need some advice on how to optimize it. I mean it works but it's not smooth. I am still a beginner and this is the solution i came up with.
problems:
the motion of the camera doesn't seem smooth enough I know there is Lerp() and Slerp() but i'm not sure how to implement it
i need a better way to limit the camera angle
CameraController:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject pivot;
public float mousespeed;
public float lookspeed;
public Text mouseText;
public float height;
public float distance;
public Texture2D crosshairImage;
CursorLockMode wantedMode;
Quaternion targetlook;
float smoothlook = 0.5f;
RaycastHit hit;
float rayHit = 0.1f;
void Start () {
wantedMode = CursorLockMode.Locked;
Cursor.lockState = wantedMode;
Cursor.visible = false;
transform.position = pivot.transform.position + (pivot.transform.rotation * (new Vector3 (0.5f,height,-distance)));
pivot.transform.Rotate(Vector3.right + new Vector3(280,0,0));
}
void LateUpdate () {
//escapes MouseLook
if (Input.GetKey (KeyCode.Escape)) {
wantedMode = CursorLockMode.None;
Cursor.lockState = wantedMode;
Cursor.visible = true;
}
//rotation of the pivot-object
if (Input.GetAxisRaw("Mouse Y") < 0) {
if(pivot.transform.localEulerAngles.x < 357.0f){
pivot.transform.Rotate(Vector3.right * mousespeed);
}
}
if (Input.GetAxisRaw("Mouse Y") > 0) {
if(pivot.transform.localEulerAngles.x > 270.0f){
pivot.transform.Rotate(Vector3.right * -mousespeed);
}
}
//camera collision
if (Physics.Raycast (pivot.transform.position, transform.position - pivot.transform.position, out hit, Vector3.Distance (pivot.transform.position, transform.position))) {
transform.position = Vector3.Lerp (hit.point, pivot.transform.position, rayHit);
}
//rotation of the camera with pivot
transform.position = pivot.transform.position + (pivot.transform.rotation * (new Vector3 (0.5f,height,-distance)));
targetlook = Quaternion.LookRotation (pivot.transform.position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, targetlook, smoothlook * Time.deltaTime);
transform.LookAt (pivot.transform);
}
//crosshair
void OnGUI()
{
float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
GUI.DrawTexture(new Rect(xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
}
}
there's a second part of the script in the CharacterController Where it rotates the character per Mouse but its basically the same as in the CameraController
feel free to use this script
Your answer
Follow this Question
Related Questions
Controlling MouseLook script with keys/joystick 1 Answer
The sprite that is being followed by thr camera is very jittery and doesn't look very nice at all. 2 Answers
How to render severals cameras at differents Frame Rate ? 2 Answers
Can a iOS game camera have the Bloom optimized effect with clear flags = depth only? 0 Answers
Is there a way to write a Demosaicing fragment shader? 1 Answer