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 safak93 · Mar 23, 2014 at 05:15 PM · inventoryattackinventory system

Need help with my player attack script

Hey guys.

I have a PlayerAttack with bullets. Currently if I collect a bullet prefab, in the InventoryManager-Inspector the bullet float rises up. If the bullet in InventoryManager greater then 0 is and I press the keycode f then it shoots. So far so good. Now I want that in the InventoryManager-Inspector the bullet float should fall down if I shoot once. I don't know how to do that.

PlayerControllerScript with bulletattack on line 73 and 89:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControllerScript : MonoBehaviour 
 {
     public InventoryManager inventoryManager;
 
     public float maxSpeed = 5f; 
     public bool facingRight = true;
     
     Animator anim;
     
     public bool grounded = false;            
     public Transform groundCheck;
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;  
     public float jumpForce = 650f;
 
     HealthController healthController;
 
     //Player Attack
     public Rigidbody2D bulletPrefab;
     float attackRate = .5f;
     float coolDown;
     
     // Use this for initialization
     void Start () 
     {
         anim = GetComponent<Animator>();
         healthController = GetComponent<HealthController> ();
     }
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         if(healthController.currentHealth > 0)
         {
         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround); //Here we are in the ground
         anim.SetBool("Ground", grounded); 
         
         anim.SetFloat ("vSpeed", rigidbody2D.velocity.y); 
         
         
         //if (!grounded)return;  
         
         float move = Input.GetAxis ("Horizontal");
         
         anim.SetFloat ("Speed", Mathf.Abs (move));
         
         rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
         
         if(move > 0 &&!facingRight)
             Flip ();
         else if (move < 0 && facingRight)
             Flip ();
         }
         else
         {
             gameObject.renderer.enabled = false;
         }
     }
     
     void Update()
     {
         if(grounded && Input.GetKeyDown(KeyCode.Space)) 
         {
             anim.SetBool("Ground", false); 
             rigidbody2D.AddForce(new Vector2(0, jumpForce)); 
         }
 
         if(Time.time >= coolDown)
         {
             if(Input.GetKeyDown(KeyCode.F) && inventoryManager.bullet > 0)
             {
                 BulletAttack();
                 inventoryManager.bullet = -1;
             }
         }
     }
     
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     void BulletAttack()
     {
         if(facingRight)
         {
             Rigidbody2D bPrefab=    Instantiate (bulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
             bPrefab.rigidbody2D.AddForce (Vector3.right * 500);
             coolDown = Time.time + attackRate;
         }
         else
         {
             Rigidbody2D bPrefab=    Instantiate (bulletPrefab, transform.position, Quaternion.identity) as Rigidbody2D;
             bPrefab.rigidbody2D.AddForce (-Vector3.right * 500);
             coolDown = Time.time + attackRate;
         }
     }
 }
 


InventoryManager:

 using UnityEngine;
 using System.Collections;
 
 public class InventoryManager : MonoBehaviour {
 
     static public Inventory inventory;
     
     public float coin = 0;
     public float bullet = 0;
 
     public float initHealth = 5;
     public float initLifePoint = 3;
     public bool initValues = false;
 
     private string healthText = "Health";
     private string lifePointsText = "LifePoint";
     private string maxHealthText = "MaxHealth";
 
     void Awake()
     {
 
         if (inventory == null)
         {
             inventory = (Inventory) ScriptableObject.CreateInstance (typeof(Inventory));
         }
 
         if (initValues)
         {
             inventory.Clear();
             inventory.SetItems(healthText, initHealth);
             inventory.SetItems(maxHealthText, initHealth);
             inventory.SetItems(lifePointsText, initLifePoint);
         }
     }
 
     void Update()
     {
         coin = inventory.GetItems("coin");
         bullet = inventory.GetItems ("bullet");
     }
 }
 
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 getyour411 · Mar 23, 2014 at 09:33 PM

"...Now I want that in the InventoryManager-Inspector the bullet float should fall down if I shoot once."

I take that to mean you want bullet var to decrease. You already do this:

  BulletAttack();
 inventoryManager.bullet = -1;

but it sounds like you meant for that last line to be

 inventoryManager.bullet--;

(what you have now is a hard set to negative one)

Comment
Add comment · Show 8 · 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 safak93 · Mar 23, 2014 at 09:59 PM 0
Share

I have tried it with inventory$$anonymous$$anager.bullet = -1; but that doesn't work. I don't know why..

avatar image getyour411 · Mar 23, 2014 at 10:06 PM 0
Share

Unless your post includes a typo, you are hard-coding the bullet value to -1 (literally), that's why I suggested you use -- (literally $$anonymous$$us $$anonymous$$us), the basic decrement. If you mean something else by "doesn't work", be specific.

avatar image safak93 · Mar 23, 2014 at 10:12 PM 0
Share

I mean that the bullet var doesn't decrease if i shoot. I have also tried with inventory$$anonymous$$anager.bullet--; it's the same result as above.

avatar image getyour411 · Mar 23, 2014 at 10:15 PM 0
Share

You presented bullet as if it were just a float, I see now you are setting the value in Update() for it via inventory.GetItems("bullet") so mucking around with the bullet value does nothing.

What is GetItems - it's not shown here.

avatar image safak93 · Mar 23, 2014 at 10:24 PM 0
Share

GetItems is from Inventory Script.

on line 41:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class Inventory : ScriptableObject {
     
     Dictionary<string,float> items = new Dictionary<string, float> ();
 
     public void AddItems(string itemName, float quantity)
     {
         float oldValue = 0;
 
         itemName = itemName.ToLower ();
 
         if (items.TryGetValue(itemName, out oldValue))
         {
             quantity += oldValue;
             items[itemName] = quantity;
         }
         else
         {
             items.Add(itemName,quantity);
         }
     }
 
     public void SetItems(string itemName, float quantity)
     {
         float oldValue = 0;
         
         itemName = itemName.ToLower ();
         
         if (items.TryGetValue(itemName, out oldValue))
         {
             items[itemName] = quantity;
         }
         else
         {
             items.Add(itemName,quantity);
         }
     }
 
     public float GetItems(string itemName)
     {
         float currentValue = 0;
         float result = 0;
         itemName = itemName.ToLower ();
         
         if (items.TryGetValue(itemName, out currentValue))
         {
             result = currentValue;
         }
 
         return result;
     }
 
     public void Clear()
     {
         items.Clear ();
     }
 }
 

And for the bullet Prefab there is a InventoryCollider Script.

InventoryCollider:

 using UnityEngine;
 using System.Collections;
 
 public class InventoryCollider : $$anonymous$$onoBehaviour {
 
     public string itemName = "bullet";
     public float val = 1;
     public string tag = "Player";
     public string id = "";
     public bool oneLife = false;
 
     void Awake()
     {
         if(oneLife)
         {
             float val = Inventory$$anonymous$$anager.inventory.GetItems(id);
 
             if (val > 0)
                 Destroy (gameObject);
         }
     }
 
     void OnTriggerEnter2D(Collider2D collider)
     {
         if (tag == "" || collider.gameObject.tag == tag)
         {
             Inventory$$anonymous$$anager.inventory.AddItems(itemName,val);
             Destroy(gameObject);
 
             if (oneLife)
                 Inventory$$anonymous$$anager.inventory.SetItems(id,1);
         }
     
Show more comments

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

20 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

Related Questions

create a Inventory system ? 2 Answers

Need help with my 2D inventory slot system 1 Answer

using a rigidbody or softbody backpack as an inventory 0 Answers

Hotbar for Diablo style inventory 0 Answers

Adventure Game tutorial, Replacing models in the game world? 0 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