HanKuSung
HanKuSung

Reputation: 47

How to create a projectile Script in Unity

so recently my friends and I are trying to make a game for fun. Currently, I hit a wall and not sure how to do this. I am trying to create a simple base script where I have the character move and attack with right click. If it hits the ground, it will move there and and if in range of a target, it will send a projectile. So the game is creating the projectile but its not actually moving. Can anyone tell me what I should probably do. At first, I thought to just make it all one script but now I am thinking it be best to make another script for the projectile.

void Update()
{
    if (Input.GetMouseButtonDown(1))
    {
        RaycastHit hit;

        if(Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit) && hit.transform.tag == "Minion")
        {
            if(Vector3.Distance(this.transform.position, hit.point) <= atkRange)
            {
                GameObject proj = Instantiate(bullet) as GameObject;
                proj.transform.position = Vector3.MoveTowards(this.transform.position, target, atkSpd);                                  
            }                             
        }
        else if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, speed))
        {
            agent.destination = hit.point;
        }
    }
}

So this is what I originally had. I am pretty sure I did something wrong here. Also I am not sure if I should have another script for the projectile itself or if it is not necessary. Thank you for any help or tips on what to do.

Upvotes: 0

Views: 2216

Answers (2)

Foggzie
Foggzie

Reputation: 9821

For starters, I'd advize using a Rigidbody component and letting physics handle movement but if you'd like to use Vector3.MoveTowards, it'll be a bit of work:

Vector3.MoveTowards is something that needs to be called every frame. I'm guessing bullet is your prefab so you'll want to make a new script for the movement and place it on that prefab:

public class MoveToTarget : MonoBehaviour
{
    private float _speed;
    private Vector3 _target;

    public void StartMovingTowards(Vector3 target, float speed)
    {
        _target = target;
        _speed = speed;
    }

    public void FixedUpdate()
    {
        // Speed will be 0 before StartMovingTowards is called so this will do nothing
        transform.position = Vector3.MoveTowards(transform.postion, _target, _speed);
    }
}

After you've attached this to your prefab, make sure you grab a reference and get it started when you instantiate a copy of your prefab:

GameObject proj = Instantiate(bullet) as GameObject;
var movement = proj.GetComponent<MoveToTarget>();
movement.StartMovingTowards(target, atkSpd);

If you instead go the physics route, add a Rigidbody component to your bullet prefab, and get a reference to that instead of making the MoveToTarget script:

GameObject proj = Instantiate(bullet) as GameObject;
var body = proj.GetComponent<Rigidbody>();

Then you can just apply a force and let physics take over:

body.AddForce(target - transform.position, ForceMode.Impulse);

Upvotes: 4

user10976548
user10976548

Reputation:

Don't set the position like you currently are

proj.transform.position = Vector3.MoveTowards(this.transform.position, target, atkSpd);

Instead, add either a characterController or a rigidbody and use rb.addVelocity.

Upvotes: 0

Related Questions