Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hey so as mentioned yhe bullet should follow the enemy till it hits it. I will appreciate if someone could help me

Here my Bullet script:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerFire : MonoBehaviour
{
float moveSpeed = 25f;

Rigidbody2D rb;

Monster target;
Vector2 moveDirection;

public float firespeed;
// Start is called before the first frame update
void Start()
{
}

// Update is called once per frame
void Update()
{
    transform.Translate(Vector2.right * firespeed * Time.deltaTime, Space.Self);
}

private void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log(collision.transform.name);
    Destroy(gameObject);
}
}


What I have tried:

I really don't know what to do. If someone could help me, it would be very helpful for me.
Posted
Updated 11-Jan-22 11:06am
v2
Comments
j snooze 11-Jan-22 17:15pm    
Its been awhile since I did anything in Unity, and I was doing 3D but it looks like there is a Vector2.MoveTowards function in Unity. Try telling the bullet object to move towards the location of the object you want it to hit.

https://docs.unity3d.com/ScriptReference/Vector2.MoveTowards.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900