Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Mkadmii · Aug 21, 2016 at 05:45 PM · beginnerprojectilenewbieshootbullets

{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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Aug 21, 2016 at 11:04 PM 0
Share

please post the code you have so far so we can add to it

avatar image Mkadmii toddisarockstar · Aug 22, 2016 at 11:31 AM 0
Share

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
         );
     }


 }

}

avatar image Mkadmii Mkadmii · Aug 22, 2016 at 11:32 AM 0
Share

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 :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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'

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mkadmii · Aug 22, 2016 at 11:33 AM 0
Share

Thank you! I keep hearing these terms however I don't understand them. Thanks for the clarification :)

avatar image
0

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;
                 }
     }
 
 } 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mkadmii · Aug 25, 2016 at 04:30 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Shoot Projectile in 3D from A to B 0 Answers

I need help with learning... 1 Answer

Okay so i followed a some what old video on how to jump and for some reason my character just goes up and i cant find out why 0 Answers

Hi, Im trying to make a OnTriggerStay2D but with some delay, and I dont know how is, someone can help me please? I share mi code below. 0 Answers

Unity Hub won't open my Projects (MacOS) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges