Hi,
My situation is that I have a 2D platformer. The shooting works, but only towards the right direction. I have attached the script to a empty game object to position the shooting position exactly to where I want. I have tried several ways; trying to add another direction and trying to flip the child, all which I couldn’t do.
I have 3 different scripts that I use to do this…
My First:
public class InfitityMovement : MonoBehaviour {
public Vector2 speed = new Vector2(10, 10);
public Vector2 direction = new Vector2(-1, 0);
private Vector2 movement;
// Update is called once per frame
void Update () {
movement = new Vector2(
speed.x * direction.x,
speed.y * direction.y);
}
void FixedUpdate()
{
// Apply movement to the rigidbody
rigidbody2D.velocity = movement;
}
}
The Second:
public class PlayerShooting : MonoBehaviour {
public Transform bullet;
public float shootingRate = 0.25f;
private float shootCooldown;
void Start()
{
shootCooldown = 0f;
}
void Update()
{
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
public void Attack(bool isEnemy)
{
if (CanAttack)
{
shootCooldown = shootingRate;
// Create a new shot
var shotTransform = Instantiate(bullet) as Transform;
// Assign position
shotTransform.position = transform.position;
// The is enemy property
ShootingControl shot = shotTransform.gameObject.GetComponent<ShootingControl>();
if (shot != null)
{
shot.isEnemyShot = isEnemy;
}
// Make the weapon shot always towards it
InfitityMovement move = shotTransform.gameObject.GetComponent<InfitityMovement>();
if (move != null)
{
move.direction = this.transform.right; // towards in 2D space is the right of the sprite
}
}
}
public bool CanAttack
{
get
{
return shootCooldown <= 0f;
}
}
}
Lastly…
public class ShootingControl : MonoBehaviour {
public int damage = 1;
public bool isEnemyShot = false;
// Use this for initialization
void Start () {
Destroy (gameObject, 10);
}
}
I have tried playing around with the transform.right section, didn’t work. I tried changing the Vector2 direction variable to different numbers, didn’t work. Ive just been clueless all day!