• 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 BlankSlates · Sep 16, 2018 at 06:50 PM · player2d-platformerenumbegginer

Beginner trying to stop movement function and collision on death ?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Animations;
 using UnityEngine.UI;
 using UnityEngine.SceneManagement;

 public class playerController : MonoBehaviour
 {
 
 [SerializeField]
 private Animator playeranim;
 private Rigidbody2D rb2d;
  // private BoxCollider2D box2d;
 [SerializeField]
 private int thrust = 100;
 [SerializeField]
 private int jumpHeight = 50;
 [SerializeField]
 private int canJump;
 private Vector2 direction;
 enum State { Alive, Death, levelclear}
 State state = State.Alive;
 bool collisionsAreEnabled = true;


 // Use this for initialization
 void Start ()
 {
     rb2d = gameObject.GetComponent<Rigidbody2D>();
    // box2d = gameObject.GetComponent<BoxCollider2D>();
 }
 
 // Update is called once per frame
 void Update ()
     
 {
    // RotateDirection();
     playeranim = gameObject.GetComponent<Animator>();
     if (state == State.Alive)
     {
         rigidbodymovement();
     }

    

    
 }

 private void FixedUpdate()
 {
     rb2d.velocity = new Vector2(rb2d.velocity.x, Mathf.Clamp(rb2d.velocity.y, -5f, 4f));
 }

 private void rigidbodymovement()
 {
     

     if (Input.GetKey(KeyCode.D))

     {

         playeranim.SetInteger("running", 2);


         rb2d.AddForce(transform.right * thrust * Time.deltaTime);

     }

     else if (Input.GetKey(KeyCode.A))
     {
        

         playeranim.SetInteger("running", 2);


         rb2d.AddForce(-transform.right * thrust * Time.deltaTime);



     }

     else
         playeranim.SetInteger("running", 0);
     playeranim.SetInteger("jump", 0);


     if (Input.GetKeyDown(KeyCode.Space))  // todo fix double jump and a glitch where the player turns his body when jumping and controls glitch
     {
         if( canJump > 1)
         {
             canJump = canJump - 1;
             
             rb2d.AddForce(new Vector2(0, 1) * jumpHeight * Time.deltaTime);
         }
         
     }

     
 {
     
 }




 // }

 //else
 //{
 //  playeranim.SetBool("is_Running", false);
 //  playeranim.SetBool("is_Idle", true);
 // }
 }
     private void OnCollisionEnter2D(Collision2D collision)
 {
     if (state != State.Alive || !collisionsAreEnabled)
     {
         return; // ignore collisions when dead
     }
     switch ( collision.gameObject.tag)
     {
         case "water":
             {
                 playeranim.SetInteger("death", 2);
                 Invoke("death", 1f);
                 break;
             }
         case "floor":
             {
                 Debug.Log("i am touching the floor");
                 canJump = 2;
                 break;
             }

         default:
             {
                 // stop the audio if i add background music here
                 state = State.Death;
                 canJump = 1;
                 break;
             }
     }
 }

 
 private void RotateDirection()

  {
  transform.Rotate(direction);

  direction = Vector2.zero;
   
 if (Input.GetKey(KeyCode.A))
  {
         direction += new Vector2(0, 1);
  }
  
 if (Input.GetKey(KeyCode.D))
   {
         direction += new Vector2(0,1 );
  }



 }

  private void death()
 {

     
     SceneManager.LoadScene(0);
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

114 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

Related Questions

What's the Best Structure for a UI Menu? 0 Answers

How to stop player movement during dialog?,I don't know what i can write after "if" to stop player movement during dialog 1 Answer

Drop-down enum pauses player - must it be so? 1 Answer

Player pickup/toggle to win screen not working 0 Answers

Unsupported enum type 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges