- Home /
Question by
Crocus · Aug 22, 2014 at 09:25 PM ·
collisionboxcolliderdeath
How can I make my character "die"?
I'm trying to get my character to appear as if it was dead. I'm currently following a tutorial and the caracter ar supposed to stop when "dead is true".
This is my code:
using UnityEngine;
using System.Collections;
public class PlayerMove : MonoBehaviour {
Vector3 velocity = Vector3.zero;
public Vector3 gravity;
public Vector3 FlyVelocity;
public float maxSpeed = 5f;
public float forwardSpeed = 1f;
bool didFly = false;
bool dead = false;
Animator animator;
// Use this for initialization
void Start () {
animator = GetComponentInChildren<Animator>();
}
// Do Graphic & Input updates here
void Update(){
if (dead)
return;
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) ) {
didFly = true;
animator.SetTrigger ("DoFly");
}
}
void OnCollisionEnter2D(Collision2D collision) {
animator.SetTrigger ("Death");
dead = true;
}
// Do physics engine updates here
void FixedUpdate () {
velocity += gravity * Time.deltaTime;
velocity.x = forwardSpeed;
if(didFly == true) {
didFly = false;
velocity += FlyVelocity;
}
velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
transform.position += velocity * Time.deltaTime;
}
}
Comment
Well, it depends on what you want to happen when said character is "dead" One way is. Set state to dead. Then play death animation and stop controls to player.
Answer by Petrol · Aug 23, 2014 at 12:01 AM
Hello,
To solve your problem, you should test your "dead" boolean in FixedUpdate() like you did in Update() :
//Add this at the beggining of FixedUpdate()
if (dead)
return;
FixedUpdate is where you are moving your player so returning the function when dead is true will prevent your script from moving it.