Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 simonevaira · Mar 10 at 07:04 PM · player movementplayer settingsplayer damage

The player slips when he dies from the enemy

Hi, I'm making a 2d game, basically when the enemy hits the player he has to die, standing still and the controls don't go. I have a problem, I can't understand why but once the player is dead he slips and continues to slide indefinitely. How can I make him stay still? thank you If you can help me, I'll pass the code to you if needed.

 [RequireComponent(typeof(Rigidbody2D), typeof (CapsuleCollider2D), typeof(Animator))]
 public class PlayerController : MonoBehaviour
 { 
     public static class GlobalVariables{
      public static bool isAlive = true;
  }
 
     [SerializeField] FixedJoystick _joystick;
     [SerializeField] float _moveSpeed;
     [SerializeField] Vector2 deathKick = new Vector2(20f, 20f);
     Animator anim;
     Transform transf;
     
     CapsuleCollider2D myBodyCollider;
     Rigidbody2D myRigidbody;
 
 
     private void Start() {
         anim = GetComponent<Animator>();
         transf = GetComponent<Transform>();
         myBodyCollider = GetComponent<CapsuleCollider2D>();
         myRigidbody = GetComponent<Rigidbody2D>();
     }
     public void Attack(){
        
         anim.SetBool("attack", true);
     }
   private void FixedUpdate() {
       if(!GlobalVariables.isAlive) {return; }
       run();
       flipAnim();
       Die();
 
     }
 
     void run(){
         if(!GlobalVariables.isAlive) {return; }
         myRigidbody.velocity = new Vector3(_joystick.Horizontal * _moveSpeed * Time.deltaTime, _joystick.Vertical * _moveSpeed * Time.deltaTime, myRigidbody.velocity.y * Time.deltaTime);
     }
 
     void flipAnim(){
         if(!GlobalVariables.isAlive) {return; }
         if(_joystick.Horizontal > 0){
             anim.SetBool("run", true);
             transf.localScale =  new Vector3(_joystick.Horizontal < 0 ? 1 : 0.1751192f, 0.1842967f, 0.2667874f);
         } else if(_joystick.Horizontal < 0 ) {
             anim.SetBool("run", true);
             transf.localScale =  new Vector3(_joystick.Horizontal > 0 ? 1 : -0.1751192f, 0.1842967f, 0.2667874f);
         } else {
              anim.SetBool("run", false);
         }
     }
 
     void Die(){
         if(myBodyCollider.IsTouchingLayers(LayerMask.GetMask("Enemies"))){
             GlobalVariables.isAlive = false;
             anim.SetTrigger("dead");
             myRigidbody.velocity = deathKick;
         }
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HellsHand · Mar 10 at 10:02 PM

From what I see, unless I'm missing it, in your Die method you are setting your players velocity to deathKick, but I don't see where that velocity is being removed. Also, nothing I see sets GlobalVariables.isAlive back to true after so run wouldn't be called which looks like it would change the velocity again.

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 simonevaira · Mar 11 at 07:53 AM 0
Share

Hi, thanks for your answer, I solved the problem. that is, putting in the Die method "myRigidbody.bodyType = RigidbodyType2D.Kinematic;" so it stands still

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

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 2D Camera Player Boundary 1 Answer

How do I set the player's movement speed on Oculus OVRPlayerController? 3 Answers

How do I stop Infinte jumping? 3 Answers

Cant link animation to Player Controller 0 Answers

my character movement act weirdly when i activate my camera follow script 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