iam following the official unity space shooter tutorials but in 2d space/orthographic setup and my player is just a 2d sprite/.png
the shots fire only when i drag the shot prefab into heirarchy in game mode..
its very frustrating
**this is the player/jet script** :
`using UnityEngine;
using System.Collections;
[System.Serializable]
public class Boundary
{
public float xMin,xMax,yMin,yMax;
}
public class PlyrScript : MonoBehaviour
{
public float speed;
public Boundary boundary;
public GameObject shot; //linked the bullet/projectile prefab in the shot parameter
public Transform shotSpawn; // linked the transform of the shotspawn gameobject to it
public float fireRate;
private float nextFire;
void update ()
{
if (Input.GetButton("Fire1")) && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
//GameObject clone =
//print("space key was pressed");
Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
}
}
void FixedUpdate ()
{
float mHzntl = Input.GetAxis ("Horizontal");
float mvrtcl = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (mHzntl, mvrtcl, 0.0f);
rigidbody.velocity = movement * speed ;
rigidbody.position = new Vector3
(
Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
Mathf.Clamp (rigidbody.position.y, boundary.yMin, boundary.yMax),
0.0f
);
}
}`
**and this is the shot/bullet script** :
using UnityEngine;
using System.Collections;
public class mover : MonoBehaviour
{
public float speed;
void Start ()
{
rigidbody.velocity = transform.up * speed ;
}
}
↧