I have created a bullet spawner class that can shoot bullets in a set direction, but now I am trying to add a mode where it gets the players position and fires a bullet towards the player.
I have managed to make it fire towards the player, but the bullets speed is way to high when the player is far away and way to slow when it is close to the spawner. How do i instantiante the bullets speed seperately from its direction?
Or is there a better way to get the correct vector towards the player?
below is my class as a whole.
using UnityEngine;
using System.Collections;
public class bulletSpawner : MonoBehaviour
{
public GameObject Bulletprefab; //Which bullet to use
public GameObject spawner; //the spawner that creates the bullet
public Rigidbody2D rigidbody2D; //Create the rigidbody variable
public float speed; // The starting speed of the bullet
public Rigidbody2D clone; //Gives acces to the rigigbody of the clone created
public Vector2 direction; //the direction of the bullet
public float gravityScale; //how much gravity shall affect the bullet. 0 if no gravity
public bool findTarget; //if the bullet should fire towards the player on instantiate
public Transform Player; //the players position
public float maxRange; //if player is closer than the maxRange, it can fire
public float timer; //how many seconds until the spawner starts shooting
public float restart; //How many seconds between each shot
// Use this for initialization
void Start ()
{
rigidbody2D = Bulletprefab.GetComponent<Rigidbody2D>(); //get the rigidbody2D component attached to the object
}
// Update is called once per frame
void Update ()
{
timer = timer - 1 * Time.deltaTime; //withdraws 1 from the timer each second
if (timer <= 0.0f)
{
//Rigidbody2D clone;
if (findTarget)//if it shall fire towards the player
{
if (Vector2.Distance (transform.position, Player.position) <= maxRange)//check if the player is closer than maxRange
{
//get the angle between player and spawner
direction = new Vector2 (Player.position.x - spawner.transform.position.x, Player.position.y - spawner.transform.position.y);
//Create a clone of the bullet as a Rigidbody2D
clone = Instantiate (rigidbody2D, spawner.transform.position, spawner.transform.rotation) as Rigidbody2D;
//Set the speed and direction of the object.x
clone.velocity = transform.TransformDirection(direction*speed);
//Sets the value on how much the gravity shall affect the object. 0 if there is no gravity
clone.gravityScale = gravityScale;
timer = restart; //restart the timer
} else
timer = restart; //restart the timer
return;
}
else//if findTarget is false, fire towards a predefined direction
//Create a clone of the bullet as a Rigidbody2D
clone = Instantiate (rigidbody2D, spawner.transform.position, spawner.transform.rotation) as Rigidbody2D;
//Set the speed and direction of the object
clone.velocity = transform.TransformDirection (direction * speed);
//Sets the value on how much the gravity shall affect the object. 0 if there is no gravity
clone.gravityScale = gravityScale;
timer = restart; //restart the timer
}
}
}