Search Unity

Why Don't my Bullets move???

Discussion in 'Scripting' started by ColouredPixelsUnity, Jan 30, 2015.

  1. ColouredPixelsUnity

    ColouredPixelsUnity

    Joined:
    Aug 10, 2014
    Posts:
    78
    I was copying the Unity3d Space Shooter tutorial. I made the script to make the bullets shoot,

    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 boundary;

    public GameObject shot;
    public Transform shotSpawn;
    public float fireRate;

    private float nextFire;

    void Update ()
    {
    if (Input.GetButton("Fire1") && Time.time > nextFire)
    {
    nextFire = Time.time + fireRate;
    Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    audio.Play ();
    }
    }

    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, boundary.xMin, boundary.xMax),
    0.0f,
    Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    );

    rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    }
    }

    And when I shoot my bullet, they spawn but don't move, any help? I made the fire rate 0.25, I dragged on the Bullet spawn and bolt.
     
  2. ColouredPixelsUnity

    ColouredPixelsUnity

    Joined:
    Aug 10, 2014
    Posts:
    78
    Woops, I fixed it, sorry :D
     
  3. whiteskull24

    whiteskull24

    Joined:
    Oct 10, 2015
    Posts:
    3
    how did you fix it???
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It looks like a classic noob logic error. As far as I can tell, the movement code was on the player controller class. It should have been on the bullet prefab.