- Home /
Bullet Not Firing in Correct Direction
Im making a 2D bullet hell, and Ive added a feature on the weapons for it to fire three bullets at a time in a shotgun style spread. My code instatiates the bullet from another script and then isdie the script on the bullet below, it should first set its rotation using the faceMouse() function. Since the function sets the transform.up to face the mouse, I assumed that using transform.tralsate and moving towards it would work. And while if you aim straight up it works fine, as you move your mouse to the left and right of the play (in a top down game) the bullets start firing in drastically wrong angles, but it seems to be consistantly wrong. I already made sure the bullets I instatiate are at 0,0,0 on their rotation to begin with, so I honeslty have no idea why this isn't working. Heres the bullet script, any ideas would be much appricated.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Projectile : MonoBehaviour {
public float bulletLife;
public float turnSpeed;
Vector3 moveDirection;
public enum bulletDirection{straight, left, right};
[HideInInspector]
public bulletDirection dir;
void Start()
{
StartCoroutine(destroyAfterTime());
if (dir == bulletDirection.straight) {
moveDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
faceMouse();
} else if (dir == bulletDirection.left){
moveDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
faceMouse();
transform.Rotate(0,0,10);
//moveDirection = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x - 2, Camera.main.ScreenToWorldPoint(Input.mousePosition).y - 2) - transform.position;
} else if (dir == bulletDirection.right){
moveDirection = (Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position);
faceMouse();
transform.Rotate(0,0,-10);
//moveDirection = new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x + 2, Camera.main.ScreenToWorldPoint(Input.mousePosition).y + 2) - transform.position;
}
moveDirection.z = 0;
moveDirection.Normalize();
}
private void Update()
{
//transform.position = transform.position + moveDirection * WeaponManager.instance.currentWeapon.shootSpeed * Time.deltaTime;
transform.Translate(transform.up * WeaponManager.instance.currentWeapon.shootSpeed * Time.deltaTime);
}
public void faceMouse(){
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector2 direction = new Vector2(mousePosition.x - transform.position.x, mousePosition.y - transform.position.y);
transform.up = direction;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "wall")
{
VisualEffects.spawnHitEffect(transform);
Destroy(gameObject);
} else if (other.tag == "enemy")
{
VisualEffects.spawnHitEffect(transform);
Destroy(gameObject);
}
}
IEnumerator destroyAfterTime()
{
yield return new WaitForSeconds(bulletLife);
VisualEffects.spawnHitEffect(transform);
Destroy(gameObject);
}
}
I think the problem is when you get the world position of mousePosition
on line 47
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
as say here, The z position is in world units from the camera, if you use mouse position directly z is 0 ( Input.mousePosition
converted from vector2 to vector3), this probably is creating some weird values (on z = 0 all the points have the same value... is the start clipping plane) to get a clumsy approximation you can try the distance between the camera and the player
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = (Camera.main.transform.position- transform.position).magnitude;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
of course, if this is a fixed value, as usual on 2d games, the camera always follow the player at the same distance from scene, you can store it on starting scene and evade the magnitude cost of cpu each frame
This didn't seem to change anything... although I think your right in saying that the problem is when it gets the position of the mouse. Ive noticed that the bullets point towards the mouse when facing straigh up, but as you move left and right with the mouse the angle is more skewed. It seems like the problem occurs when I set transform.up to the position of the mouse, is there some other way I should have the bullets face the mouse? All I want to do is set that transform.up angle to to face the mouse, then rotate other bullets based off of that. Lets go from there.
maybe setting the direction it on screen space?
Vector3 playerPosition = Camera.main.WorldToScreenPoint(transform.position);
Vector2 direction = Input.mousePosition - playerPosition;
transform.up = direction.normalized;
Your answer
Follow this Question
Related Questions
How to get a proper ramming effect? 1 Answer
Dropping an object behind player in respect to rotation,How to drop an object behind the player 1 Answer
How to instantiate on custom rotation? 1 Answer
Why can't I get the transform from an instantiated object? 1 Answer
Planes not instantiating properly on gameobject location 2 Answers