• 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 /
avatar image
0
Question by moniclecatman · May 08, 2018 at 08:00 PM · 2d gametop down shooter

2D 360 Shooting Is not working (Top Down, Shooting in all directions)

I am trying to make a top down in 2d and I have tried an unbelievable amount of scripts. Where I am now is a dot that is attached to the barrel but really far away. That dot is where I want the bullet to go and stop at that point. Everything works except for the actual moving of the bullet. Heres my code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class BulletsFiring : MonoBehaviour {

 public Rigidbody2D projectile;
 public Transform projectileSpawnPoint;
 public float projectileVelocity;
 public float timeBetweenShots;
 private float timeBetweenShotsCounter;
 private bool canShoot;
 public GameObject Target;

 // Use this for initialization
 void Start () {
     canShoot = false;
     timeBetweenShotsCounter = timeBetweenShots;
 }

 // Update is called once per frame
 void Update (){
     if (Input.GetMouseButtonDown(0) && canShoot)
     {
         Rigidbody2D bulletInstance = Instantiate(projectile, projectileSpawnPoint.position, Quaternion.Euler(new Vector3(0, 0, transform.localEulerAngles.z))) as Rigidbody2D;
         projectile.transform.position = Vector3.MoveTowards (transform.position, Target.transform.position, projectileVelocity);
         canShoot = false;


     }
     if (!canShoot)
     {
         timeBetweenShotsCounter -= Time.deltaTime;
         if(timeBetweenShotsCounter <= 0)
         {
             canShoot = true;
             timeBetweenShotsCounter = timeBetweenShots;
         }
     }
 }

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TanselAltinel · May 08, 2018 at 08:47 PM

Hi

In order for the projectile to move, it should be called each update. However, in your script, you are calling MoveTowardsonly once.

Look here: https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html

Also, I'd suggest to move projectile with its own script instead of moving it from a manager.

Another thing I'd suggest is that, if your projectile is using RigidBody2D, then you should use RigidBody2D functions to move it.

This might work for you: https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html

Comment
Add comment · 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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

92 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D top down shooter border. 1 Answer

I need help with a top down 2d shooter 3 Answers

How to make shooting in top down 2d shooter game? Unity 2d 1 Answer

How do I invert mouse movement for a specific section of my game? 3 Answers

Spawning 2 bullets when I want 1 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