The question is answered, right answer was accepted
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.
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);
}
}