Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

How to Make NPC Rotate Towards the Player With the Bullets Being Rotated as Well

Hello. I have a NPC that, detect the player when its in a certian range, however I need it to rotate towards the player and aim the bullets at the player. Right now when the player is within range it starts shooting straight but not at the player.

Here is the code for the NPC to shoot

//position of bullet
Vector3 position = new Vector3(farRangeNPC.transform.position.x, farRangeNPC.transform.position.y, farRangeNPC.transform.position.z);
var bullets = MonoBehaviour.Instantiate(prefab, position, Quaternion.identity);

The movement of the shooting is done in another script attached to the bullet prefab.

If you need more information I can definitely provide.
 
Code:
  //position of bullet
            Vector3 position = new Vector3(farRangeNPC.transform.position.x, farRangeNPC.transform.position.y, farRangeNPC.transform.position.z);
            var bullets = MonoBehaviour.Instantiate(prefab, position, Quaternion.identity);
Sorry did not realize I needed to put code inside the BBCode.
 
I dont have a clue about unity, though logic is same.

Do the NPC have a point of view, or can it see 360 around it?
Anyway, you need to calculate the angle from NPC to player, turn the NPC (or its aim) to that angle before starting to shoot.

** edit: fixed typos
 
I dont have a clue about unity, though logic is same.

Do the NPC have a point of view, or can it see 360 around it?
Anyway, you need to calculate the angle from NPC to player, turn the NPC (or its aim) to that angle before starting to shoot.

** edit: fixed typos
I appreciate your reply.

I am not good at maths, however I will look at what you have provided. I found this online:

Code:
            var offset = 90f;
            Vector2 direction = target.position - transform.position;
            direction.Normalize();
            float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
               transform.rotation = Quaternion.Euler(Vector3.forward * (angle + offset));

If I was to use this piece of code for the mean time, I would need to then find a way to make the bullets follow this logic. Any ideas?
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom