- Home /
Can't make a bullet move in the direction the actor is facing
Hey people, as the title says, I'm having problems moving bullets in the right way. I have an actor with fixed position but variable rotation. When it fires, the bullets always move in a straight line upwards instead of moving in the direction that the actor is facing. The bullet changes its rotation with the actor but it still moves upwards.
Here is the WeaponScript attached to the actor. It instantiates the bullet, setting the starting position and the bullet rotation.
public class WeaponScript : MonoBehaviour {
private float fireCooldown;
// Use this for initialization
void Start () {
fireCooldown = 0f;
}
// Update is called once per frame
void Update () {
if (fireCooldown > 0f) { fireCooldown = fireCooldown - Time.deltaTime; }
}
public void Fire () {
if (fireCooldown<=0f)
{
fireCooldown = WeaponStatsScript.instance.fireRate;
BulletScript bullet;
var shotObject = Instantiate(Resources.Load(WeaponStatsScript.instance.bulletPrefab)) as GameObject;
shotObject.transform.position = transform.position;
shotObject.transform.rotation = transform.rotation;
}
}
}
And here is the BulletScript. Speed is (10,10).
public class BulletScript : MonoBehaviour {
private int damage;
private Vector2 speed;
private Vector2 direction;
private Vector2 movement;
private Rigidbody2D rigidbodyComponent;
// Use this for initialization
void Start () {
Destroy(gameObject, 10);
damage = WeaponStatsScript.instance.bulletDamage;
speed = WeaponStatsScript.instance.bulletSpeed;
direction = new Vector2(0, 1);
rigidbodyComponent = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
movement = new Vector2(speed.x * direction.x, speed.y * direction.y);
}
private void FixedUpdate()
{
if(rigidbodyComponent==null)
{
Debug.LogError("00003 - BulletScript - rigidbodyComponent not initialised!");
rigidbodyComponent = GetComponent<Rigidbody2D>();
}
rigidbodyComponent.velocity = movement;
}
}
The weird thing is that a very similar code worked in another project, I'm unsure why it does not behave correctly anymore. Anyone can help? Thanks a lot in advance!
Anyone else that can help and suggest possible causes and solutions? As suggested by other I tried adding shotObject.transform.rotation = transform.rotation;
in the WeaponScript after instantiating the bullet and changing the movement in the BulletScript to movement = new Vector2(speed.x * direction.x, speed.y * direction.y);
but as a result the bullet spawns and doesn't move at all. Creating a public static PlayerScript playerObject;
in the PlayerScript, then doing direction = PlayerScript.playerObject.transform.eulerAngles
also had the same result.
Answer by cbjunior · Jun 25, 2017 at 06:08 PM
Because you have the direction = new Vector2 (0, 1)
line, you are completely nullifing the horizontal movement value and keeping the vertical movement value, thus your bullets are only flying upwards. Try changing the line to use the actual rotation of the GameObject as opposed to a static value.
I tried adding shotObject.transform.rotation = transform.rotation;
in the WeaponScript after instantiating the bullet and changing the movement in the BulletScript to movement = new Vector2(speed.x * direction.x, speed.y * direction.y);
but as a result the bullet spawns and doesn't move at all.
Answer by Litleck · Jun 25, 2017 at 06:43 PM
Add a new Public GameObject Character;
and set it to your character then use this which will make the bullet go in the direction of the Character. direction = Character.transform.eularAngles
That should work.
I did what you said and created a public static PlayerScript playerObject;
in the PlayerScript, then did direction = PlayerScript.playerObject.transform.eulerAngles;
as you said, keeping movement the same as posted. The result is that bullets spawn with the right rotation (which I had before as well) but now they don't move at all.
Use this at the very start of the bullet life then have a function to move it forward and thats it. Also ins$$anonymous$$d of using a constant Update function you should make a custom function that runs when the bullet is shot and ends when the bullet collides with something.
Your answer
Follow this Question
Related Questions
making bullet go travel at angle fired and limit gun rotation in Unity C# version 5.2 2D 1 Answer
Spawn bullet in front of object then apply force? 1 Answer
Shoot an object and have it move based on rotation 1 Answer
Player rotating towards direction of movement in 3D 1 Answer
Problem with Shooting Accuracy 0 Answers