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 /
This question was closed Jan 13, 2017 at 01:31 AM by XyloShrike for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by XyloShrike · Jan 12, 2017 at 03:52 PM · 2d gameprojectile

Firing a projectile change the direction of the other projectiles.

First of all: Sorry for my bad english, it is not my native tongue.

The problem is that: when i shoot a projectile with my character, the last shots changes their direction in order to follow the new shoot.

If I shoot to the left side and later i shoot to the right side, the first bullet will change its direction to the opposite side in order to follow the first shot. It happens to every single projectile.

This is my BulletController's code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class BulletController : MonoBehaviour {
 
     public Rigidbody2D theRigidbody;
 
     void Start () 
     {
         theRigidbody = GetComponent<Rigidbody2D>();
     }
 
     void Update () 
     {
         if (Input.GetMouseButton (0)) 
         {
             Vector3 sp = Camera.main.WorldToScreenPoint (transform.position);
             Vector3 dir = (Input.mousePosition - sp).normalized;
             theRigidbody.AddForce (dir * 200);
         }
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         Destroy (gameObject);
     }
 }

And this is my PlayerController's code (the shooting system):

 void Update ()
     {
         // CODIGO POINT TO LOOK
         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);
         float angle = AngleBetweenPoints(transform.position, mouseWorldPosition);
         transform.rotation =  Quaternion.Euler (new Vector3(0f,0f,angle));
 
         // CODIGO PARA DISPARAR
         if (Input.GetMouseButtonDown (0)) 
         {
             Instantiate (bullet, firePoint.position, firePoint.rotation);
         }
     }

Once again, sorry for my english... I tried to make it clear.

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

  • Sort: 
avatar image
1
Best Answer

Answer by PizzaPie · Jan 12, 2017 at 07:01 PM

If i understand correctly the first script is attached to all bullets, if so the problem is on the Update() of that script and exactly on the << if (Input.GetMouseButton (0)) >> . Now because of that line every time you Left Click it will change the direction of every active bullet in your game and not only the direction of the bullet you lastly instatiated. To fix that move the code of the Update() to the Start() without the if statement like this

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class BulletController : MonoBehaviour {
  
      public Rigidbody2D theRigidbody;
  
      void Start () 
      {
          theRigidbody = GetComponent<Rigidbody2D>();
 
              Vector3 sp = Camera.main.WorldToScreenPoint (transform.position);
              Vector3 dir = (Input.mousePosition - sp).normalized;
              theRigidbody.AddForce (dir * 200);
      }
 
      void OnTriggerEnter2D(Collider2D other)
      {
          Destroy (gameObject);
      }
  }
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 XyloShrike · Jan 13, 2017 at 01:30 AM 0
Share

Works perfect! Thanks a lot <3 !

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

Getting projectile to leave marks at where its hit, for example different spot of a building of a 2D game? 0 Answers

make an enemy spell move towards player 1 Answer

Firing Projectile on path,Firing Rockets on path 0 Answers

How do you make one game object follow the shape of another game object? 0 Answers

Rotate projectile sprite in the direction it is flying (like an arrow) 2D. 1 Answer


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