Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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( 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 .

Comment
Add comment · 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

 can$$anonymous$$ove = false; 

in your

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





2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · 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
0

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.

Comment
Add comment · 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.

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

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


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