How do I instantiate an objects direction and speed seperately?
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
}
}
}
Answer by NoseKills · Jan 18, 2016 at 05:29 PM
Naturally, if you set the distance vector from shooter's position to target's position as the velocity, it will be the greater the farther the target is.
You need to normalize the direction vector ( make its length 1) before multiplying with 'speed' if you want the velocity to be of the same magnitude every time.
clone.velocity = transform.TransformDirection(direction.normalized*speed);
Wow, it was that easy... I've been trying several different solution today without any success, and all that was needed was to add "normalized" to it all. Thanks a lot!
Your answer
Follow this Question
Related Questions
Instantiate position for shooting projectile is way off. Could I get some help? 0 Answers
How does one continuously move an object forward without using velocity? 1 Answer
Bullet has wrong direction on client 0 Answers
Instantiate vs Instantiate as gameobject 1 Answer
Aiming and shooting to the same point in third person 1 Answer