- Home /
Instantiating a camera halts my script?
Hi, I'm pretty new to 3d programming and I wrote a simple script for my player.
I wrote a kill method that strips his camera, gets another one to look at him falling, takes away player control of him & gravity, then knocks him over.
But, when I call this kill method, for some reason my script halts after the deathCam is instantiated and moved. The "check" debug is never reached in the script. Any information would be very helpful, thanks. public class Player : MonoBehaviour { public float moveSpeed = 80; public float mouseSensitivity = 3.0f; public float verticalRange = 60.0f; public float jumpSpeed = 30.0f; float verticalRotation = 0f;
float verticalVelocity = 0f;
Animation move;
Camera deathCam = new Camera();
bool dead = false;
// Use this for initialization
void Start ()
{
Screen.lockCursor = true;
move = GetComponent<Animation>();
}
void Awake()
{
dead = false;
}
// Update is called once per frame
void Update ()
{
if(dead == false)
{
CharacterController cc = GetComponent<CharacterController>();
//Rotation
float rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
transform.Rotate (0, rotLeftRight, 0);
verticalRotation-= Input.GetAxis ("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp (verticalRotation, -verticalRange, verticalRange);
Camera.main.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
//Movement
float forwardSpeed = Input.GetAxis ("Vertical") * moveSpeed;
float sideSpeed = Input.GetAxis ("Horizontal") * moveSpeed;
verticalVelocity += Physics.gravity.y * Time.deltaTime;
if(Input.GetButtonDown ("Jump") && cc.isGrounded)
{
verticalVelocity = jumpSpeed;
}
Vector3 speed = new Vector3(sideSpeed, verticalVelocity, forwardSpeed);
speed = transform.rotation * speed;
cc.Move(speed * Time.deltaTime);
if(forwardSpeed > 1 || sideSpeed > 1)
{
move.Stop ("IDLEE");
move.Play ("Move");
}
else
{
move.Stop ("Move");
move.Play ("IDLEE");
}
//Testing
if(Input.GetButtonDown ("Fire1"))
{
kill();
}
}
}
void kill()
{
if(!dead)
{
GetComponent<CharacterController>().enabled = false;
GetComponent<Animation>().enabled = false;
Camera.main.enabled = false;
Instantiate (deathCam);
deathCam.transform.position = new Vector3(this.transform.position.x, this.transform.position.y + 10, this.transform.position.z);
GetComponent<Rigidbody>().AddForce (transform.forward * 5);
Debug.Log("check");
dead = true;
}
}
void respawn()
{
}
}
Your answer
Follow this Question
Related Questions
Needs help to instantiate camera as child of player 0 Answers
Locking the Camera to Instantiated Player 3 Answers
Instantiated object is not visible in game 2 Answers
Animation automatique personnage 0 Answers
Locking world rotation of child 1 Answer