Projectile is not going to the player

checked up my code whith the provided, it’s the same, don’t know what is happening, please someone can help me?

Hi,

I’m assuming that the player is the grey model and that the green projectiles are supposed to be hitting him.

Looks like you have a rotation issue with the transform on the projectile, by about 180 degrees if I had to guess from the screenshot.

https://github.com/UnityRPG/2-Core-Combat/commit/e9da087a3403085c0ff22a28625041d35f29c193 thats the code, i don’t know ho to solve, but i really don’t think its a rotation problem, but yes is 180 degrees if it is, but i have any var or something to deal with it

I was thinking more along the lines of the actual transform on the GameObject / Prefab. You have a projectile socket I believe, where-ever you have GameObjects parented to other GameObjects it only takes you to miss resetting a transform to have problems.

I would start by checking your GameObject/prefabs.

it’s the only conection with the projectile that my enemy has( my shocket is the projectle spawn in the first picture)

In the previous lecture (#57) - at 06:12 you can see that the rotation for the Y component of the enemy is set to 140. What do you have on your enemy prefab?

Can you copy/paste your code for enemy.cs into your reply as well, rather than the course version, just in case there are any differences.


See also;

I did it, but seems to be the same, case the problem still here

Can you copy/paste your enemy.cs script into a reply please.

[SerializeField]float maxHealthPoints = 100f;
[SerializeField]float chaseRadius = 6f;

[SerializeField]float attackRadius = 4f;
[SerializeField]float damagePerShot = 9f;
[SerializeField]float secondsBetweenShots = 0.5f;
[SerializeField]GameObject projectileToUse;
[SerializeField] GameObject projectileSocket;

bool isAttacking = false;
float currentHealthPoints = 100f;
AICharacterControl aiCharacterControl = null;
GameObject player = null;

public float healthAsPercentage { get { return currentHealthPoints / maxHealthPoints; } }

public void TakeDamage(float damage)
{
    currentHealthPoints = Mathf.Clamp(currentHealthPoints - damage, 0f, maxHealthPoints);
}

void Start()
{
    player = GameObject.FindGameObjectWithTag("Player");
    aiCharacterControl = GetComponent<AICharacterControl>();
}

void Update()
{
    float distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);
    if (distanceToPlayer <= attackRadius && !isAttacking)
    {
        isAttacking = true;
        InvokeRepeating("SpawnProjectile", 0f, secondsBetweenShots); // TODO switch to coroutines
    }

    if (distanceToPlayer > attackRadius)
    {
        isAttacking = false;
        CancelInvoke();
    }

    if (distanceToPlayer <= chaseRadius)
    {
        aiCharacterControl.SetTarget(player.transform);
    }
    else
    {
        aiCharacterControl.SetTarget(transform);
    }
}

void SpawnProjectile()
{
    GameObject newProjectile = Instantiate(projectileToUse, projectileSocket.transform.position, Quaternion.identity);
    Projectile projectileComponent = newProjectile.GetComponent<Projectile>();
    projectileComponent.SetDamage(damagePerShot);

    Vector3 unitVectorToPlayer = (player.transform.position - projectileSocket.transform.position).normalized;
    float projectileSpeed = projectileComponent.projectileSpeed;
    newProjectile.GetComponent<Rigidbody>().velocity = unitVectorToPlayer * projectileSpeed;
}

void OnDrawGizmos()
{
    // Draw attack sphere 
    Gizmos.color = new Color(255f, 0, 0, .5f);
    Gizmos.DrawWireSphere(transform.position, attackRadius);

    // Draw chase sphere 
    Gizmos.color = new Color(0, 0, 255, .5f);
    Gizmos.DrawWireSphere(transform.position, chaseRadius);
}

}

do you think my problem can be the quaternion?

Can you select the enemy which is firing backwards, then select the Projectile To Use field in the Inspector, this should highlight in yellow either a GameObject in your Hierarchy, or a prefab in your Assets.

Can you let me know which, and then select it, and take a screenshot of the details in the Inspector for it please.

And please repeat this for the Projectile Spawn Point for the same enemy.

the enemy it’s the same i posted above and the spawn point has nothing

Quick question. Do the projectiles fall to the floor or remain in the air?


Updated Thu May 03 2018 03:18

I think your issue is that you have Is Kinematic still enabled on the projectile. I assumed from the screenshot they the projectiles were being fired backwards, but I think its actually your enemy chasing the player which is leaving a trail of projectiles in the air.

If I’m right, then turn off Is Kinematic on the Rigidbody component on the projectile and this should resolve the issue.

Lecture 57 - Spawning Enemy Projectiles - @04:10

IT WORKED, THANK YOU , really, i’m doing my tcc based on this course so thanks you saved my life(i think i can solve this but my enemy is killing him self with the projectile, it colliders with the enemy collider?)

1 Like

You are very welcome. :slight_smile:

Regarding the second issue, you could raise the spawn point up a little so that it doesn’t make contact with the enemy collider, or, better still, add a little code so that enemy projectiles don’t kill enemies for example. :slight_smile:

Glad you can move forward again :slight_smile:

1 Like

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

Privacy & Terms