Hello! I’m new here. New to Unity and game development. I’m making a sample 2D game and I got stucked.
The enemy is shooting in the wrong direction. It was supposed to shoot at me obviously. This is the WeaponScript I’m using for the enemy.
1. WeaponScript
using UnityEngine;
/// <summary>
/// Launch projectile
/// </summary>
public class WeaponScript : MonoBehaviour
{
//--------------------------------
// 1 - Designer variables
//--------------------------------
/// <summary>
/// Projectile prefab for shooting
/// </summary>
public Transform shotPrefab;
/// <summary>
/// Cooldown in seconds between two shots
/// </summary>
public float shootingRate = 0.25f;
//--------------------------------
// 2 - Cooldown
//--------------------------------
private float shootCooldown;
void Start()
{
shootCooldown = 0f;
}
void Update()
{
if (shootCooldown > 0)
{
shootCooldown -= Time.deltaTime;
}
}
//--------------------------------
// 3 - Shooting from another script
//--------------------------------
/// <summary>
/// Create a new projectile if possible
/// </summary>
public void Attack(bool isEnemy)
{
if (CanAttack)
{
shootCooldown = shootingRate;
// Create a new shot
var shotTransform = Instantiate(shotPrefab) as Transform;
// Assign position
shotTransform.position = transform.position;
// The is enemy property
ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
if (shot != null)
{
shot.isEnemyShot = isEnemy;
}
// Make the weapon shot always towards it
MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
if (move != null)
{
move.direction = this.transform.right; // towards in 2D space is the right of the sprite
}
}
}
/// <summary>
/// Is the weapon ready to create a new projectile?
/// </summary>
public bool CanAttack
{
get
{
return shootCooldown <= 0f;
}
}
}
2. EnemyShot ( This is the Prefab I’m using for the enemy projectile)
It has 2 scripts:
2a. ShotScript
using UnityEngine;
/// <summary>
/// Projectile behavior
/// </summary>
public class ShotScript : MonoBehaviour
{
// 1 - Designer variables
/// <summary>
/// Damage inflicted
/// </summary>
public int damage = 1;
/// <summary>
/// Projectile damage player or enemies?
/// </summary>
public bool isEnemyShot = false;
void Start()
{
// 2 - Limited time to live to avoid any leak
Destroy(gameObject, 20); // 20sec
}
}
2b. MoveScript
using UnityEngine;
/// <summary>
/// Simply moves the current game object
/// </summary>
public class MoveScript : MonoBehaviour
{
// 1 - Designer variables
/// <summary>
/// Object speed
/// </summary>
public Vector2 speed = new Vector2(10, 10);
/// <summary>
/// Moving direction
/// </summary>
public Vector2 direction = new Vector2(-1, 0);
private Vector2 movement;
private Rigidbody2D rigidbodyComponent;
void Update()
{
// 2 - Movement
movement = new Vector2(
speed.x * direction.x,
speed.y * direction.y);
}
void FixedUpdate()
{
if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
// Apply movement to the rigidbody
rigidbodyComponent.velocity = movement;
}
}
I have found a partial solution. I can rotate the Enemy Sprite to 180 degrees. If I do that the enemy shoots at me. But ofc I don’t want that.
Can somebody help me, please ? I will provide additional info. Just ask! Thank you!