I’m trying to create a simple third person controller with a camera script but suddenly the game crashes on me every time. The crashes started after I tried to get the camera to follow the player, but I have no idea why it crashes.
Camera script:
using UnityEngine;
using System.Collections;
public class CameraScript : MonoBehaviour {
public GameObject player;
public float cameraSensitivity = 100;
private float xRotationValue;
private float yRotationValue;
private float distance;
void Start () {
}
void FixedUpdate () {
xRotationValue = Input.GetAxis("Mouse X") * cameraSensitivity;
yRotationValue = Input.GetAxis("Mouse Y") * cameraSensitivity;
transform.RotateAround(player.transform.position, Vector3.up, xRotationValue * Time.deltaTime);
distance = Vector3.Distance(transform.position, player.transform.position);
while (distance > 10)
{
transform.Translate(Vector3.forward*Time.deltaTime);
}
}
}
Character script:
using UnityEngine;
using System.Collections;
public class MoveCharacter : MonoBehaviour {
public GameObject mainCamera;
public float moveSpeed = 0.1f;
public float sprintSpeed = 0.3f;
Vector3 moveVector;
void Start () {
}
void Update () {
//Set movement
moveVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (Input.GetButton("Sprint"))
moveVector = moveVector * sprintSpeed;
else
moveVector = moveVector * moveSpeed;
//Apply movement
transform.Translate(moveVector, Space.Self);
//transform.eulerAngles = new Vector3(0, mainCamera.transform.eulerAngles.y, 0);
}
}