简体   繁体   中英

How do i make a bullet heading towards mouse position never stop? Unity2D

I've been trying to make player of my game able to shoot bullets every 0.6 seconds towards the mouse point. I managed to create a function that shoots a bullet towards the mouse but there is still one problem. Its speed depends on how far the mouse point is. What i mean is, if I move my mouse to the almost edge of my screen the bullet will go faster, and if i move it close to the player it just stops and destroys itself when it reaches the position of the mouse when the bullet was shot

Here's my code

    void Shoot() {
        Vector3 pos = transform.position;
        pos.y +=0.5f;
        Bullet newBullet = Instantiate(bullet, pos, bullet.transform.rotation) as Bullet;

        Vector3 mousePos = Input.mousePosition;
        mousePos.z = Camera.main.nearClipPlane;
        Vector3 heading = Camera.main.ScreenToWorldPoint(mousePos) - pos;
        newBullet.Direction = heading / heading.magnitude;     
    }

It belongs to the player

Bullet code(the part that you might need):

private Vector3 dir;
public Vector3 Direction { set {dir = value; }}
private void Update() {
    transform.position = Vector3.MoveTowards(transform.position, transform.position+dir*10f, 
    speed*Time.deltaTime);
}

This is probably because i use MoveTowards() function. As you can see, I tried multiplying dir variable because I thought the distance would get longer as well. It did not work at all; I could actually increase the heading.magnitude (in the player's code) i guess but still the speed would vary depending on how far the player would have their mouse pointer away from the player object.

The object is traveling at a constant rate, however, it is traveling in 3D space, not 2D space. The reason that the "apparent" speed is different relative to the position of the mouse is because you are using 3D vectors for the direction and the z component has a value other than zero. Input.mousePosition returns a vector with only x and y set, z is zero (you don't need the 'z' component). However, ScreenToWorldPoint is going to do 3D work and include the z direction.

The reason that the bullet disappears is because it is traveling in 3D space and it disappears as soon as it gets to the near clipping plane. If you select an instance of a bullet in the editor, you can see the x, y and z values change (instead of just the x and y).

The easiest fix is to not set the z component (it's not needed). So, remove this line:

mousePos.z = Camera.main.nearClipPlane;

And zero-out the z component in the calculations. Then pass the normalized vector to the bullet's direction.

heading = new Vector3( heading.x, heading.y, 0.0f );
newBullet.Direction = heading.normalized; 

So, your final code will look like.

void Shoot()
{
    Vector3 pos = transform.position;
    pos.y +=0.5f;
    Bullet newBullet = Instantiate(bullet, pos, bullet.transform.rotation) as Bullet;

    Vector3 mousePos = Input.mousePosition;
    Vector3 heading = Camera.main.ScreenToWorldPoint(mousePos) - pos;
    heading = new Vector3( heading.x, heading.y, 0.0f );
    newBullet.Direction = heading.normalized;     
}

There are other ways to do this by just using Vector2 (Vector2 and Vector3 have translation methods). You should explore them. If you're a pure 2D app, then Vector2 would work.

As a note, your Bullet.Update should like like this:

void Update()
{
    transform.Translate( dir * speed * Time.deltaTime );
}

And you should add an off-of-screen check in your Bullet.Update so that you can destroy the instance when it is off the screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM