TurnBase System course when bullet close to target

Hey.
I’m watching the turnBase System course and I’ve got a problem. When I shoot the bullet it immediately destroy the bullet.
How can I fix that? I have the same code but it’s seems mine is a bit different in game then in the video.

One more interesting this is when I don’t destroy the bullet and shoot the enemy in the video the bullet goes to the enemy legs but in my project its shoot the bullet above the enemy.
I know in the video it will change but just wanted to tell you.

Is the bullet displaying at all? What happens if you lower the MoveSpeed?
Paste in your BulletProjectile.cs and we’ll take a look.

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

public class BulletProjectile : MonoBehaviour
{

private Vector3 targetPosition;

public void Setup(Vector3 targetPosition)
{
    this.targetPosition = targetPosition;
}

private void Update()
{
    Vector3 moveDir = (targetPosition = transform.position).normalized;

    float distanceBeforeMoving = Vector3.Distance(transform.position, targetPosition);

    float bulletSpeed = 200f;
    transform.position += moveDir * bulletSpeed * Time.deltaTime;

    float distanceAfterMoving = Vector3.Distance(transform.position, targetPosition);

    if(distanceBeforeMoving < distanceAfterMoving)
    {
        Destroy(gameObject);
    }
}

}

This is the bulletProjectile script. I made bulletspeed 0.001 and other value but It changes nothing. The bullet destoryed immediately

Typo here: you are setting targetPosition = to the transform.position so distanceBeforeMoving will always be 0 and therefore always < than distanceAfterMoving.
Should be

Vector3 moveDir = (targetPosition - transform.position).normalized;

(sorry, stole @Brian_Trotter’s thread)

1 Like

Oh my god what a dumb mistake sorry for that. Thanks for the help!!

It’s an honest mistake. It happens to us all at some point

1 Like

I never complain if the solution is reached. :wink:

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms