Soni
Soni

Reputation: 77

Top down shooting in Unity 2D don't work as I want it to

I am making a 2D top down shooter in Unity. Everything has worked fine, the problem came when I tried to implement the shooting.

I am not experienced with Unity nor C# at all, so I mainly used tutorials to get this code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Bullet : MonoBehaviour
{
    public float speed = 20f;

    private Vector2 target;

    void Start()
    {
        target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }

    void Update()
    {
        transform.position = Vector2.MoveTowards(transform.position, target, speed * Time.deltaTime);
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (!collision.name.Equals("Player") && !collision.name.Equals("Bullet(Clone)"))
        {
            Destroy(gameObject);
        }
    }
}

This is everything related to the bullet, apart from summoning it when clicking. This script is an object on the bullet prefab, so it will get run when the bullet is summoned.

The result I get from this is as expected - the bullet spawns at the player and travels to the point where I clicked, then it stops. However, what I want it to do, is continuing in the same direction until it hits something.

I have tried multiplying the Vector2 target with 10 (or some random number), but when doing this something very weird happens. When standing where the player spawns and shooting, it works completely fine. But when I start to move, the bullets go in the wrong direction. If I Debug.Log(); the target, it looks completely as it should, but the bullets travel in the wrong direction. So this won't work.

Sorry for my bad English and little knowledge, any help would be appreciated :)

Upvotes: 1

Views: 3255

Answers (2)

BlackBrain
BlackBrain

Reputation: 1029

You are just moving the bullet to the clicked position thus it stops there. You should instead give it a speed vector.

public class Bullet : MonoBehaviour
{
  public float speed = 20f;

  private Vector2 direction;

void Start()
{
    direction= Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
    direction = direction.normalized;
}

void Update()
{
    transform.position+=  = direction * speed * Time.deltaTime;
}

void OnTriggerEnter2D(Collider2D collision)
{
    if (!collision.name.Equals("Player") && !collision.name.Equals("Bullet(Clone)"))
    {
        Destroy(gameObject);
    }
}
}

Upvotes: 0

andresantacruz
andresantacruz

Reputation: 1724

I don't know the specifications of Unity but I think something like this would work as you want:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class Bullet : MonoBehaviour
{
    public float speed = 20f;

    private Vector2 m_Direction;

    void Start()
    {
        // save direction by offsetting the target position and the initial object's position.
        m_Direction= Camera.main.ScreenToWorldPoint(Input.mousePosition) - this.transform.position;
    }

    void Update()
    {
        // this will cause to every frame the object's transform to be move towards the target position direction.
        transform.position = Vector2.MoveTowards(this.transform.position, this.transform.position + m_Direction, speed * Time.deltaTime);
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (!collision.name.Equals("Player") && !collision.name.Equals("Bullet(Clone)"))
        {
            Destroy(gameObject);
        }
    }
}

Upvotes: 2

Related Questions