Hi,
I’m new to Unity and trying to program a script for my TopDown-Shooter I’m working on. I have some issues concerning the direction the bullet is spawning and flying. I made a Prefab of my Bullet, named it “Bolt” and added the following Script:
using UnityEngine;
using System.Collections;
public class Mover : MonoBehaviour {
public float speed;
void Start ()
{
GetComponent<Rigidbody2D> ().velocity = transform.forward * speed;
}
}
And here’s the script, which handles the character movement and shooting part:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public float speed1;
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.transform.position, shotSpawn.transform.rotation);
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate (Vector2.right * speed1);
}
if (Input.GetKey (KeyCode.A)) {
transform.Translate (-Vector2.right * speed1);
}
if (Input.GetKey (KeyCode.W)) {
transform.Translate (Vector2.up * speed1);
}
if (Input.GetKey (KeyCode.S)) {
transform.Translate (-Vector2.up * speed1);
}
}
}
There are 2 problems with this script:
Pressing the Firebutton results in “laying” shots, they’re not flying, they only spawn and remain at the same position.
The other problem is, that the shots don’t spawn in the direction my character is facing.
What am I doing wrong?