• 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 hedgy134117 · Sep 18, 2018 at 12:16 AM · movementbeginnermovetowards

Script affecting all prefabs

Im currently working on a top down shooter, and right now I'm working on making the shooting work. I have the bullet shoot when the left mouse button is pressed, and the bullet should go to the mouse. The only issue is, if I move the mouse, the bullet also moved to go towards the new position.

Bullet Movement Script (What makes the bullet go to the mouse):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletMovement : MonoBehaviour {
 
     public float lifetime;
     public float speed;
     private Vector2 targetPos;
     public GameObject bullet;
     
     // Update is called once per frame
     void Update () {
         lifetime -= Time.deltaTime;
         targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         bullet.transform.position = Vector2.MoveTowards(bullet.transform.position, targetPos, speed * Time.deltaTime);
 
         if(lifetime <= 0.0f)
         {
             Destroy(bullet);
         }
     }
 }


Bullet Shooting Script (What makes the bullet appear):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerShooting : MonoBehaviour {
 
     public GameObject bullet;
     public GameObject player;
     private Vector2 playerPosition;
 
     // Update is called once per frame
     void Update () {
         playerPosition = new Vector2(player.transform.position.x, player.transform.position.y);
 
         if (Input.GetMouseButtonDown(0))
         {
             Debug.Log(playerPosition);
             Instantiate(bullet, playerPosition, Quaternion.Euler(0, 0, 0));
         }
     }
 }
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
Best Answer

Answer by AdityaViaxor · Sep 18, 2018 at 04:03 AM

yes this will happen because you have written movement code in update method and target position is also in update so as you move mouse target position get changes and the new value pass to target in moveToward what you have to do is get the position ones only when player click or press button get value or position and pass that value and remove target line from update. write it down in playerShooting script. inside if ().

Comment
Add comment · Show 2 · 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 hedgy134117 · Sep 18, 2018 at 11:50 PM 0
Share

Got it! All i had to do was move the targetPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); into the Start() method of the Bullet$$anonymous$$ovement Script.

avatar image AdityaViaxor hedgy134117 · Sep 19, 2018 at 03:59 AM 0
Share

yes you can do that also :)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

148 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 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

Make object jump to fixed y position 1 Answer

Position of all spawned prefabs being effected by MoveTowards (beginner question) 1 Answer

I cant figure out how to change my simple Movement controls to touch, can anyone help? 1 Answer

Using MoveTowards on button click 0 Answers

Interrupt a MoveTowards when triggering 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