- Home /
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");
}
}
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)
I have tried it with inventory$$anonymous$$anager.bullet = -1; but that doesn't work. I don't know why..
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.
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.
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.
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);
}
Your answer
