• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by epicjosh · Oct 15, 2014 at 12:18 PM · movementplayerstopdie

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( $$anonymous$$t : Collider )
  
 {
     if($$anonymous$$t.gameObject.tag == "enemyProjectile")
     {
         animation.Play("MyCharacterRig|Hit");
         Instantiate(explode, transform.position, transform.rotation);
     }
     
     if($$anonymous$$t.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 .

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SaraCecilia · Oct 15, 2014 at 08:51 AM 1
Share

Perhaps adding a

 canMove = false; 

in your

 if (curHealth <=0 )
     {
         animation.CrossFade("MyCharacterRig|Die");
     }





2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by NadhmiPrince · Oct 15, 2014 at 01:13 PM

I t$$anonymous$$nk you should disable the character movement script It would be somet$$anonymous$$ng 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

Comment
Kamil_L_Unity

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image epicjosh · Oct 15, 2014 at 01:54 PM 0
Share

Hey thanks . It works :)

avatar image

Answer by paulbuck86 · Feb 23, 2019 at 04:47 AM

Hello,

T$$anonymous$$s is an old question, but I recently ran into t$$anonymous$$s 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 t$$anonymous$$s 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 != t$$anonymous$$s)
         {
             Destroy(gameObject);
         }
         else
         {
             Instance = t$$anonymous$$s;
             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 t$$anonymous$$s 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 w$$anonymous$$le stopping movement. T$$anonymous$$s script is a skeleton of my script, but it gives the main code that makes it work and I hope it helps someone.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I stop the enemy ai from following the player after it dies on unity3d properly in C# 0 Answers

Lock player position 1 Answer

How can i do: When the button "play"is pressed, the player can start to move, but if not pressed the player cannot move. 1 Answer

Cannot stop character movement upon trigger 2 Answers

Movement script breaks colliders 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges