- Home /
How do i stop player to move when dead
Hi. Does anyone know to stop player from moving and shooting?
Here's my script for my health bar:
var curHealth = 300;
var healthTexture : Texture2D;
var explode:Transform;
var canMove = true;
function Start ()
{
animation["MyCharacterRig|Die"].wrapMode = WrapMode.Once;
animation["MyCharacterRig|Die"].layer = 1;
}
function Update ()
{
if (curHealth <=0 )
{
animation.CrossFade("MyCharacterRig|Die");
}
}
function OnTriggerEnter( hit : Collider )
{
if(hit.gameObject.tag == "enemyProjectile")
{
animation.Play("MyCharacterRig|Hit");
Instantiate(explode, transform.position, transform.rotation);
}
if(hit.gameObject.tag == "enemyProjectile")
{
curHealth -= 15;
}
if(curHealth < 0)
{
curHealth = 0;
}
if (curHealth <= 0)
{
yield WaitForSeconds(1.3);
Application.LoadLevel(2);
}
}
function OnGUI() {
GUI.DrawTexture(Rect(10,10,curHealth,30), healthTexture);
}
i did make it looked like he's dead but when the dead animation is playing i can still move and shoot .
Perhaps adding a
can$$anonymous$$ove = false;
in your
if (curHealth <=0 )
{
animation.CrossFade("$$anonymous$$yCharacterRig|Die");
}
Answer by NadhmiPrince · Oct 15, 2014 at 01:13 PM
I think you should disable the character movement script It would be something like that:
var player : GameObject;
function Update () {
if (curHealth == 0) {
player.GetComponent("CharacterController").enbaled = false;
}
}
Add that part of script to your health bar script
Select the player
If not your player's movement script is "CharacterController" make sure you change it
Answer by paulbuck86 · Feb 23, 2019 at 04:47 AM
Hello,
This is an old question, but I recently ran into this problem. Since I did not find a valid solution scouring the web. All Get commands did not properly grab the component or the CharacterController for that matter and setting .enabled = false; just didn't seem to actually work. So for anyone running into this problem here's my solution:
using System.Collections;
using UnityEngine;
public class P_Animator : MonoBehaviour
{
ExtendedBehavior wait;
P_Controller playerMovement;
private Animator anim;
protected void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
{
Instance = this;
anim = GetComponent<Animator>();
playerMovement = GetComponent<P_Controller>();
DontDestroyOnLoad(gameObject);
}
}
public void Death()
{
if (!isDead)
{
StartCoroutine(StopPlayer(.1f));
anim.SetTrigger("isDead");
//playerAudio.clip = deathClip;
//playerAudio.Play();
isDead = true;
Revival();
}
}
private IEnumerator StopPlayer(float time)
{
playerMovement.enabled = false;
yield return new WaitForSeconds(time);
}
public void Revival()
{
// How the character revives
}
You will also need to make a Coroutine script to run this correctly. Here is an example script:
using UnityEngine;
using System.Collections;
using System;
public class ExtendedBehavior : MonoBehaviour
{
public void Wait(float seconds, Action action)
{
StartCoroutine(_wait(seconds, action));
}
IEnumerator _wait(float time, Action callback)
{
yield return new WaitForSeconds(time);
callback();
}
}
The Coroutine wasn't necessary to stop the player's movement, but it was so for allowing the animation time to finish while stopping movement. This script is a skeleton of my script, but it gives the main code that makes it work and I hope it helps someone.
Your answer
Follow this Question
Related Questions
Lock player position 1 Answer
How do I stop the enemy ai from following the player after it dies on unity3d properly in C# 0 Answers
Player Controller moves automatically forward. cant stop. 1 Answer
Rotate game object and then return to its original rotation 1 Answer
Stop moving GameObject 1 Answer