{Newbie here} Anybody know a SIMPLE way to make my sprite/object shoot bullets?
So I'm quite new here and I managed to program a sprite that moves when any of the arrow keys/WASD keys are pressed. Now I want to make it shoot bullets, but in a really simple code that I could understand, preferably with some explanations for the hard coding terms. Thanks! <3 xx
please post the code you have so far so we can add to it
using UnityEngine; using System.Collections;
public class ShipController : $$anonymous$$onoBehaviour {
float speed = 0.05f;
void Update () {
//Ship $$anonymous$$ovement
if(Input.Get$$anonymous$$ey("down") || Input.Get$$anonymous$$ey("s"))
{
this.transform.position = new Vector3 (
this.transform.position.x,
this.transform.position.y - speed,
this.transform.position.z - speed
);
}
if (Input.Get$$anonymous$$ey ("up") || Input.Get$$anonymous$$ey("w"))
{
this.transform.position = new Vector3 (
this.transform.position.x,
this.transform.position.y + speed,
this.transform.position.z + speed
);
}
if (Input.Get$$anonymous$$ey ("left") || Input.Get$$anonymous$$ey ("a"))
{
this.transform.position = new Vector3 (
this.transform.position.x - speed,
this.transform.position.y,
this.transform.position.z - speed
);
}
if (Input.Get$$anonymous$$ey ("right") || Input.Get$$anonymous$$ey ("d"))
{
this.transform.position = new Vector3 (
this.transform.position.x + speed,
this.transform.position.y,
this.transform.position.z + speed
);
}
}
}
I am aware that there is a shorter and quicker method to make the object move (using the GetAxis.Horizontal or Vertical method however since I am a beginner I decided to stretch out and learn the easier and longer method first :)
Answer by Firedan1176 · Aug 22, 2016 at 12:12 AM
You can instantiate a Prefab you make of your Sprite, which in this case, would be your bullet. On your bullet prefab, you can have a script that will add to its position: transform.position += transform.forward * Time.deltaTime * 20; //20 can be considered your 'speed'
Thank you! I keep hearing these terms however I don't understand them. Thanks for the clarification :)
Answer by toddisarockstar · Aug 24, 2016 at 01:35 AM
make a new object in the scene and name it "bullet".
using UnityEngine;
using System.Collections;
public class NewBehaviourScript1 : MonoBehaviour {
public GameObject thebullet;
public Vector3 dir = new Vector3(0,1,0);// i added a variable to remember what direction player pushed last
float speed = 0.05f;
bool fire=false;
void Start () {
// find the object in the scene and asign it to a variable;
thebullet=GameObject.Find("bullet");
}
void Update () {
//always multiply speed by time. Otherwise things move faster on faster computers!!!!
speed = 10 * Time.deltaTime;
//Ship Movement
if(Input.GetKey("down") || Input.GetKey("s"))
{print ("down");
transform.position = new Vector3 (
transform.position.x,
transform.position.y - speed,
transform.position.z - speed
);
dir= new Vector3(0,-180,0);
}
if (Input.GetKey ("up") || Input.GetKey("w"))
{
this.transform.position = new Vector3 (
this.transform.position.x,
this.transform.position.y + speed,
this.transform.position.z + speed
);
dir = new Vector3(0,0,0);
}
if (Input.GetKey ("left") || Input.GetKey ("a"))
{
this.transform.position = new Vector3 (
this.transform.position.x - speed,
this.transform.position.y,
this.transform.position.z - speed
);
dir = new Vector3(0,-90,0);
}
if (Input.GetKey ("right") || Input.GetKey ("d"))
{
this.transform.position = new Vector3 (
this.transform.position.x + speed,
this.transform.position.y,
this.transform.position.z + speed
);
dir = new Vector3(0,90,0);
}
if (Input.GetKeyDown("space")){
thebullet.transform.eulerAngles=dir;
thebullet.transform.position=transform.position;
fire=true;
}
if (fire) {
thebullet.transform.position=thebullet.transform.position+thebullet.transform.forward*speed*2;
}
}
}
Thank you very much! I really do appreciate it; will try and add the new variable to my script right now :) thank you <3 xx
Your answer
Follow this Question
Related Questions
Shoot Projectile in 3D from A to B 0 Answers
I need help with learning... 1 Answer
Unity Hub won't open my Projects (MacOS) 2 Answers