-2

I am a newbie in Unity and game development. I want to make a mobile stack jump game with primitives. I have a player in the center of the screen. The enemy is approaching the player from the right and left of the screen. The player tries to sit on the enemy by jumping as in Mario Game. If the enemy hits the player from the side, the game is over. I create enemies with a SpawnMan. There is a prefab for an enemy game object. SpawnMan creates them at certain time intervals with the Instantiate() method. I want to store data on whether the enemy is approaching from right or left. I even want to store this information by creating an enum type. In summary, I want to have enemy objects with the left and the right types. How do I set this property (left enemy or right enemy) when calling the Instantiate() method in SpawnMan as in OOP constructor with parameters.

enum EnemyType
{
    Left,
    Right
}

// ...
    
Instantiate(enemyPrefab(EnemyType.Left), spawnPos, enemyPrefab.transform.rotation);
Fx Games
  • 1
  • 4

4 Answers4

0

Assign a GameObject while instantiating first : GameObject enemy1 = Instantiate(...) Then, we can use the player transform and the enemy transform vectors to get a direction (+ value and a minus value)?

Chandradhar
  • 37
  • 1
  • 9
0

As Chandradhar said, addition to that, you can use Mathf.Sign(player.position.x- enemy.position.x); to see if the enemy comes from the left or the right of the player.

0

You can't really pass it in exactly that way, as Instantiate doesn't call a constructor on the instantiated prefab's scripts. Or I guess enemyPrefab could be a method that returns an actual prefab object with set script values, but that would be a somewhat odd way to get what you want.

But presumably you just want the enemy object to have that information about left/right, in that case the easiest would be to get the attached script and then assign the left/right property. So you would add something like this to your enemy script (the one that is attached to enemy prefab)

public class MyEnemyScript : MonoBehaviour
{
    public EnemyType Type;
    
    ... your enemy code here...
}

And then use it like that:

var enemyGameObj = Instantiate(enemyPrefab, spawnPos, enemyPrefab.transform.rotation);
enemyGameObj.GetComponent<MyEnemyScript>().Type = EnemyType.Left;

Note that this is not really good practice, as GetComponent is an expensive call, but then again neither is Instantiating all those enemies - it would be nicer to use pooling.

defaultUsernameN
  • 365
  • 5
  • 14
0

I'm assuming an enum for left and right is overkill, but if you have some design reason that the enemies need to do have some special logic depending on what side they're on, you can still handle that in a simpler fashion.

In your MyEnemyScript(), you have Awake() and OnEnable() methods that will be automatically called by Unity just like Start and Update. In one of them (use OnEnable if you use object pooling), put something like this:

public EnemyType type;
private void Awake()
{
    if (transform.position.x > PlayerInfo.playerPosition.x)
        type = EnemyType.right;
    else
        type = EnemyType.left;
}

Now you can just spawn enemies as normal without thinking about it. PlayerInfo can be a static class or a singleton or however you prefer making that information available.

Then in your update loop, just have two logic paths (which I assume is similar to what you're doing anyway).

private void Update()
{
    if(type = EnemyType.left)
        //do left enemy stuff
    else
        //do right enemy stuff
}
Salvatore Ambulando
  • 402
  • 1
  • 6
  • 13