Hi guys,
I use the following code to instantiate bullets which should fly into the direction of the mouse cursor. It does not work, as you can see on the image below, the bullets fly exactly at the opposite side of the mouse pointer!
I tried to multiply with -1 in every way, it didnt help!
Can anybody help? I am using Unity 5 right now and I would like to create a little 2D mobile App.
void Shoot () {
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint(Input.mousePosition).y);
Vector2 firePointPosition = new Vector2 (firePoint.position.x, firePoint.position.y);
RaycastHit2D hit = Physics2D.Raycast (firePointPosition, mousePosition-firePointPosition, 100, whatToHit);
if (Time.time >= timeToSpawnEffect) {
//Hier Sound Effekt
//AudioSource.PlayClipAtPoint(shoot,transform.position);
if(GameMaster.flipActive==false){
Instantiate (BulletTrailPrefab, firePoint.position, firePoint.rotation);
//Effect ();
}
else if (GameMaster.flipActive==true){
Vector3 firePosi = new Vector3 (firePoint.position.x,firePoint.position.y,firePoint.position.z);
Quaternion fireRota = new Quaternion(firePoint.rotation.x,firePoint.rotation.y,firePoint.rotation.z,firePoint.rotation.w);
Debug.Log(firePosi);
Debug.Log(fireRota);
//import ab hier
Instantiate (BulletTrailPrefab, firePosi, fireRota);
}
timeToSpawnEffect = Time.time + 1/effectSpawnRate;
}
Debug.DrawLine (firePointPosition, (mousePosition-firePointPosition)*100, Color.cyan);
if (hit.collider != null) {
Debug.DrawLine (firePointPosition, hit.point, Color.red);
Enemy enemy = hit.collider.GetComponent<Enemy>();
if(enemy != null){
enemy.DamageEnemy(Damage);
Debug.Log ("We hit " + hit.collider.name + " and did " + Damage + " damage.");
}
}
}