Search Unity

Problems with code

Discussion in 'Scripting' started by phayezer, Apr 20, 2015.

  1. phayezer

    phayezer

    Joined:
    Apr 17, 2015
    Posts:
    13
    Hello All, I am making an upgraded version of the demo space shooter game, and I am having trouble with a certain line or two of code, the whole thing being attached.

    My problem is, I am trying to put in power-ups, but when I collect them, it changes nothing. It should make the fire rate, as well as the actual shot, change. the fire rate should be increased, and the single bolts should become a multibolt (name of object) I created and tested.

    Also, I am having trouble with the timeing of the powerup, not being able to figure out how to time it for 10 seconds.

    The code is attached; thank you!
     

    Attached Files:

  2. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    just to make a start:

    your methods are named wrong.

    it has to be void Start
    and
    void OnTriggerEnter(Collider _other)

    otherwise they won't be called by the UnityEngine
     
  3. phayezer

    phayezer

    Joined:
    Apr 17, 2015
    Posts:
    13
    Sorry, that might not be able to be opened. Here is the code:

    using UnityEngine;
    using System.Collections;

    [System.Serializable]
    public class boundary
    {
    public float xMin, xMax, zMin, zMax;
    }
    public class PlayerController : MonoBehaviour
    {
    public float speed;
    public float tilt;
    public boundary bounds;

    public GameObject shot;
    public bool poweredup;
    public GameObject multishot;
    public Transform ShotSpawn;
    private float fireRate;
    private float nextFire;
    void start()
    {
    fireRate = .5f;
    poweredup = false;
    }
    void Update()
    {
    if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;
    if(poweredup == true)
    {
    Instantiate(multishot, ShotSpawn.position, ShotSpawn.rotation);
    }
    Instantiate(shot, ShotSpawn.position, ShotSpawn.rotation);
    }
    }

    void ontriggerenter (GameObject other)
    {
    if(other.tag == "powerup")
    {
    poweredup = true;
    fireRate = .001f;
    }
    }

    void FixedUpdate ()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    rigidbody.velocity = movement * speed;

    rigidbody.position = new Vector3 (Mathf.Clamp(rigidbody.position.x, bounds.xMin, bounds.xMax), 0.0f, Mathf.Clamp(rigidbody.position.z, bounds.zMin, bounds.zMax));
    rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.x * tilt, 90.0f, 0.0f);
    }
    }
     
  4. Jami_2

    Jami_2

    Joined:
    Apr 22, 2013
    Posts:
    19
    Two functions have lowercase, needed uppercase.

    Change start() to Start() and ontriggerenter() to OnTriggerEnter (). Ontrigger returns a collider not a gameobject. write this OnTriggerEnter (Collider other)
     
  5. phayezer

    phayezer

    Joined:
    Apr 17, 2015
    Posts:
    13
    I got those, what else?
     
  6. Jami_2

    Jami_2

    Joined:
    Apr 22, 2013
    Posts:
    19
    You mean for your Powerups?

    I have on my Office same that works. But im far away from my office came back in a few hours. Can show you a example.

    For now i can give you a Tip. I make it with a Coroutine.
     
  7. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Is this game 2d? If so, why are you using 3D rigidbodies....
     
  8. phayezer

    phayezer

    Joined:
    Apr 17, 2015
    Posts:
    13
    because I am using 3d models that bank when moving left and right, and torpedoes that tumble towards you.
     
  9. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Ahhh, okay. Well, the powerup system should be fairly easy to achieve if you use coroutines; that will allow you to set a WaitForSeconds(x) to control the duration of the powerup. I would use another coroutine with a specified WaitForSeconds to control the fire rate as well. I try to avoid putting things like that in the update function and often lean toward coroutines because you have much more control over them.

    I could write this for you, but that would not teach you anything, and given that coroutines are such a big part of game development you really should familiarize yourself with them and their functionality. Here are some links to a few resources that will help you out with this. I hope this helps and best of luck to you! If you have any more questions please feel free to respond again to this thread or contact me personally at hamsterbytellc@gmail.com. Cheers!



     
  10. phayezer

    phayezer

    Joined:
    Apr 17, 2015
    Posts:
    13
    Thank you, my friend. I will implement that in class.

    And yes, I am in high school.
     
  11. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    Good on ya. The earlier you start the more natural it feels when you have to learn new things! :) Again, if you need any more help you can get at me personally via my website or my Twitter account; links in my signature.