Updates to my Skybox Portal Reign Game

Any feed back is welcome I trying to get enemies I added to game to move toward the playership as if to attach and follow once I can get this done with the movement I will get them to shoot at the playership. right now all the new enemies I added are spinning really fast in circles.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BasicEnemy : MonoBehaviour
{
    public Transform target;
    public float speed = 3f;
    public float attack1Range = 1f;
    public int attack1Damage = 1;
    public float timeBetweenAttacks;


    // Use this for initialization
    void Start()
    {
        Rest();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void MoveToPlayer()
    {
        //rotate to look at player
        transform.LookAt(target.position);
        transform.Rotate(new Vector3(0, -90, 0), Space.Self);

        //move towards player
        if (Vector3.Distance(transform.position, target.position) > attack1Range)
        {
            transform.Translate(new Vector3(speed * Time.deltaTime, 0, 0));
        }
    }

    public void Rest()
    {

    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class EnemyTerritory : MonoBehaviour
{
    public BoxCollider territory;
    GameObject player;
    bool playerInTerritory;

    public GameObject enemy;
    BasicEnemy basicenemy;

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        basicenemy = enemy.GetComponent<BasicEnemy>();
        playerInTerritory = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (playerInTerritory = true)
        {
            basicenemy.MoveToPlayer();
        }

        if (playerInTerritory = false)
        {
            basicenemy.Rest();
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == player)
        {
            playerInTerritory = true;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject == player)
        {
            playerInTerritory = false;
        }
    }
}

You may recall that your enemy ship movement is controlled via Timeline, as is your player on the rails etc.

You could have the enemy ships move back and forth towards the player as they get to specific points, but you’d probably have to make the player ship stop for this to serve any useful purpose and as you’ve already seen from where you stop the player from moving briefly at the end of your circuit, it doesn’t really work. A better approach may be to have the enemy ships swoop down over the player as they are moving around, they could come from the sides, or from below or from above, but the action would keep going.

Hi Rob so if I use the timeline with the new enemies I added for the purpose of shooting at the player.if I move them to get l closer to the playership. Then expand the playership timeline at some points when they meet will these enemies mover as the code suggest towards the playership as to follow and attack?

No, I’m saying don’t use the code, instead use Timeline.

Okay I will delete the code I will keep the enemies ship and create a new timeline add them to it. My question is can I get them to shoot at the playership I can make the movement look and feel like I want it to but I want to shoot back. Can you provide me guidance once I add the guns to all the enemies I added that I want to shooting at the playership?

A suggestion below but I would create/test this in a separate scene just using a couple of cubes, one that represents the player and one that represents the enemy, test, refine, and then apply to the actual game if appropriate.

I’d probably approach it like this;

  • add a sphere collider to the an enemy ship, the size of this would determine the range at which you want to detect the player
  • use an OnTriggerEnter / OnCollision enter method to determine a collision
    • collisions will occur with other GameObjects / Terrain as well, so you’ll need to filter that within the method so that you only action when the collision is with the player GameObject
  • Use Debug.Log to output a simple message “Enemy firing” as a test initially

Using the above you should be able to run the game and then move one of the cubes, an enemy, until the sphere collider collides with the player cube, at this point you’ll see the output in the console.

If you get this part working then the next step would be to;

  • add a Particle System to emit lasers, assuming you want it to be done in the same way as the player ship
  • at the point of the collision, set the GameObject for the enemy’s gun to look at the current position of the player
  • emit a projectile

Again, you should be able to test all of these in the separate, simple, scene using just a couple of cubes. You could probably grab a copy of the gun(s) GameObjects from the other scene to use on a cube if that’s easier, along withe particles.

Once you have this working you should be able to move the enemy cube so that the sphere collider colliders with the player cube, at which point the GameObject for the gun(s) would look at the player cube and then emit a particle.

You’ll have a few things to work out when testing this, for example, you may find that the enemy cube will fire at the player when the enemy ship has moved passed the player. This would be because the player cube is still within the sphere collider. It would probably look a bit odd for the enemy to be shooting backwards at the player, also, the player wouldn’t necessary know that that was about to happen and thus have no chance to avoid the particles. Depending on the Timeline setup you have you may find that the enemy ships are disabled shortly after passing the player anyway, so this may not be a big issue, something to look for though.

1 Like

Hi Rob, I will work on this much later I’m doing my homework now for my master degree class I will catch up to you when I’m done. Thank you for your support. :slight_smile:

Done with my homework will start on test now. Thank you Rob :):man_technologist::pray:

Hi Rob, This is what I got done so far I receive no syntax error :
I need to get the enemy hip to shoot when player ship passes the enemy ship with the sphere collider I added to the enemy ship. Enjoy your weekend…


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyAttack : MonoBehaviour
{
    [SerializeField] GameObject[] enemyGuns;

    // Start is called before the first frame update
    void Start()
    {
        AddSphereCollider();
    }

    private void AddSphereCollider()
    {
        Collider sphereCollider = gameObject.AddComponent<SphereCollider>();
        sphereCollider.isTrigger = false;
    }
        private void FireEnemyGunsActive(bool isActive)
    {
        foreach (GameObject enemyGun in enemyGuns)
        {
            var emissionModule = enemyGun.GetComponent<ParticleSystem>().emission;
            emissionModule.enabled = isActive;
        }
    }
}

The error is caused because it doesn’t understand what EnemyShip is, do you actually have a class called EnemyShip?

Okay I got the enemy ship to follow the player ship I also got the player to explode when collide with the enemy no player ship explosion so though. But I still working on the projectile laser enemy guns to follow and kill the player. Let me know if I 'm accurate thus far?

{

    [SerializeField] GameObject deathFX;
    [SerializeField] Transform parent;

    void Start()

    {
        AddBoxCollider();
    }
    private void AddBoxCollider()
    {
        Collider boxCollider = gameObject.AddComponent<BoxCollider>();
        boxCollider.isTrigger = false;
    }

// Start is called before the first frame update
public Transform target;

    void Update()
    {
        transform.LookAt(target);
    }

}

Hi, Rob, this is what I got up to the player starts cloning in spawning when I add the projectiles.cs to the player I also added an enemymanagement.cs to the enemy. Finally, I created a health.cs for the player Thank you

This is the error I receive: when I play the game the enemy follows the player shoots the player and then the player clones in the spawn. when I click on the error the error leads to the player controller images below: Any feedback is appreciated :confused:


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Projectiles : MonoBehaviour
{
    public Transform playerShip;
    public float range = 20.0f;
    public float enemyGunImpulse = 10.0f;
    bool onRange = false;
    public Rigidbody projectile;
    

    private void Start()
    {
        float rand = Random.Range(1.0f, 2.0f);
        InvokeRepeating("Shoot", 2, rand);
      
    }
    void Shoot()
    {

        if (onRange)
        {

            Rigidbody enemyGun = (Rigidbody)Instantiate(projectile, transform.position + transform.forward, transform.rotation);
            enemyGun.AddForce(transform.forward * enemyGunImpulse, ForceMode.Impulse);
        }
    }

    void Update()
    {
        onRange = Vector3.Distance(transform.position, playerShip.position) < range;

        if (onRange)
            transform.LookAt(playerShip);
    }
}


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyManagement : MonoBehaviour
{
    // The target marker.
    [SerializeField] Transform target;

    // Angular speed in radians per sec.
    [SerializeField] float speed;

    // Start is called before the first frame update
    void Start()
    {
        AddSphereCollider();
    }

    private void AddSphereCollider()
    {
        Collider sphereCollider = gameObject.AddComponent<SphereCollider>();
        sphereCollider.isTrigger = false;
    }

    void Update()
    {
        Vector3 targetDir = target.position - transform.position;

        // The step size is equal to speed times frame time.
        float step = speed * Time.deltaTime;

        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
        Debug.DrawRay(transform.position, newDir, Color.red);

        // Move our position a step closer to the target.
        transform.rotation = Quaternion.LookRotation(newDir);
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealth : MonoBehaviour
{

    public int currentHealthPlayer = 1500;
    [SerializeField] GameObject deathFX;
    [SerializeField] Transform parent;
    void Update()
    {
        if (currentHealthPlayer <= 0)
        {
            currentHealthPlayer = currentHealthPlayer - 1;
           
        }
    }
    public void ApplyDamage(int damageToTake)
    {
        
        currentHealthPlayer -= damageToTake;
        GameObject fx = Instantiate(deathFX, transform.position, Quaternion.identity);
        fx.transform.parent = parent;
     
    }
}

Privacy & Terms