Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 SolidStudio · Jan 18, 2019 at 04:10 PM · playerbulletinventorypick up objecthealth

Do not pick item when item full

Im looking a way for my player does not pick medikit or ammo during he has a full health and a full ammo. What is the best way to do? Should I use loops to solve the problems?

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerPickUpBullet : MonoBehaviour {
 
     PlayerShoot playerShoot;
 
     public int bulletPickUp;
     
     void Start ()
     {
         playerShoot = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerShoot>();
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.gameObject.tag == "Player")
         {
             other.gameObject.GetComponent<PlayerShoot>().PickUpBullet(bulletPickUp);
             Destroy(gameObject);
             Debug.Log(" bullet " + playerShoot.currentBullet);
         }
     }
 }
 
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

2 Replies

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

Answer by SolidStudio · Jul 31, 2019 at 01:58 AM

I have found a solution for a long time ago. First, we write the PickUp Item script. For this case, we are going to write the PlayerPickBullet.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerPickUpBullet : MonoBehaviour {
 
     PlayerShoot playerShoot;
 
     public int bulletPickUp;
     
     void Start ()
     {
         playerShoot = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerShoot>();
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.gameObject.tag == "Player")
         {
             other.gameObject.GetComponent<PlayerShoot>().PickUpBullet(bulletPickUp);
             Destroy(gameObject);
             //Debug.Log(" bullet " + playerShoot.currentBullet);
         }
     }
 }


Now we write/modify a player's stats in the PlayerShoot script.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class PlayerShoot : MonoBehaviour {
 
     PlayerPickUpBullet playerPickUpBullet;
 
     Animator animator;
     Rigidbody2D bullet;
 
     public Transform rifle;
     public GameObject projectile;
     public Slider bulletSlider;
 
     [HideInInspector] public Vector2 offset = new Vector2(0.4f,0f);
     public Vector2 velocity;
     public float cooldown = 1f;
     public float bulletdied = 1f;
 
     [HideInInspector]
     public bool canShoot = true;
 
     public int maxBullet;
     [HideInInspector] public int currentBullet;
 
     void Start ()
     {
         playerPickUpBullet = GetComponent<PlayerPickUpBullet>();
 
         animator = GetComponent<Animator>();
         bullet = GetComponent<Rigidbody2D>();
         currentBullet = maxBullet;
     }
 
     void Update ()
     {
         if(currentBullet <= 0)
         {
             return;
         }
         Shoot();    
     }
 
     public void PickUpBullet(int pickupbullet)
     {
         currentBullet += pickupbullet;
         bulletSlider.value = currentBullet;
         //set the bullet limit
         if (currentBullet > maxBullet)
         {
             currentBullet = maxBullet;
         }
 
     }
 
     public void Shoot()
     {
         if (Input.GetMouseButtonDown(0) && canShoot)
         {            
             GameObject go = Instantiate(projectile, (Vector2)rifle.transform.position + offset * transform.localScale.x, Quaternion.identity);
             go.GetComponent<Rigidbody2D>().velocity = new Vector2(velocity.x * transform.localScale.x, velocity.y * transform.localScale.y);
             currentBullet--;
             bulletSlider.value = currentBullet;
             Destroy(go, bulletdied);
             animator.SetTrigger("shoot");
             //Debug.Log(" player bullet left " + currentBullet);
         }
     }
 }
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
avatar image
0

Answer by kolboch · Jan 18, 2019 at 04:44 PM

OOP here is not the best, so the thing you need obviously is the state of the player. Inside player you would have value of health and bullet, if it is equal to defined max then you won't pick or pick a part depends on your requirements. ex.

     PlayerController {
         int ammo = 0;
         int health = 100;
     }
     
     // wherever you have collission handling you would have to do sth like
    // MAX_AMMO is another variable which you  have to place somewhere, like GameConstants static 
    // class or sth like that. It is your choice
     if ( ammo == MAX_AMMO) {
      // do not destroy object - not picking up
     } else {
     // pick ammo - add desired number to ammo player - and destroy ammo object in the scene
     }

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

119 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

Related Questions

How to keep health from going over 100% when heath pickup is used and why does the item effect stop working after I instantiate it from the vendor. 2 Answers

String to Text Mesh Problem 1 Answer

How to add Player health and ability to take damage from a cube? 2 Answers

Healthpickup isn't working 1 Answer

Collision Detection between certain objects 2 Answers


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