- Home /
Trying go get bullet to fire correctly when play turns around in 2D platformer
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (BulletCollision))]
public class Bullet : MonoBehaviour{
public float bulletSpeed = 9;
float gravity = 0;
Vector3 velocity;
Controller2D controller;
BulletCollision bulletController;
int flipNumber = 1;
void Start () {
bulletController = GetComponent<BulletCollision> ();
controller = GetComponent<Controller2D> ();
}
void Update () {
/*
if (controller.collisions.faceDir != 1) {
flipNumber = -1;
} else {
flipNumber = 1;
}*/
velocity.x = bulletSpeed * controller.collisions.faceDir;
velocity.y += gravity * Time.deltaTime;
bulletController.Move (velocity * Time.deltaTime);
}
}
I need to make the bullet coming from the player to fire in the correct direction. My thought was that I need a way to reference the collisions.faceDir variable form the Collisions2d script without having to put it onto my bullet prefab. If anyone could help me with how to do this or with some other solution that would be great!
Answer by Zehru · Jul 17, 2016 at 01:27 AM
@HipsterSoap HI! If you use a "rigidbody2d" on the bullet, it might be simpler to achieve the results you want. You can use "Rigidbody2d.Impulse" to make the bullet fly to the correct direction( use a vector2 if the game is 2d, so you can do: right, left, up, down and diagonals easily). To check the direction, you should use "transform.localrotation.z" There are some good tutorials about this on a youtube channel called CasanisPlays( in a 2d game prototyping), I also learned there ^_^ I hope this answer was useful. cheers
I've thought about that but ive been using raycasting for everything. Im not sure that I could keep all the stuff ive built so far if i made the bullets a rigid body
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
bullets kill all enemies, not just the ones hit. 1 Answer
animation script for 2D platformer issue 2 Answers