- Home /
Old question never really answered
I am very new to unity and I'm looking for advice on a targeting system( or if i'm setting myself up for failure)
So I have just recently started using unity. I'm learning through a combination of video tutorials and digging into the scripting documentation.
I am working on a little space flight type of project, where you fly around the scene. I have gotten the movement, a health system, a working fuel mechanic for boosting, and the beginnings of a weapon system.
I can destroy enemies through collision and shooting the laser at them, but now I want to add a targeting system for mouse click. Ideally it would Pop a targeting box around whatever I click, and set that as a "target" which i can call for when I want to add different weapons and effects ( next step is a homing missile to target for instance)
right now I have it setup to instantiate a targeting box on the enemy ship when the player right clicks on them. That works as intended but i have no idea how to only be able to target one thing at a time, and more importantly I'm wondering if the way i'm doing this is going to come back to bite me in the butt later. Here's the code for the player
public class PlayerShip : MonoBehaviour {
public GameObject lazerBolt;
public Transform lazerBoltSpawn;
public enum WeaponType { Lazer, Missile, Bullet};
public WeaponType myWeapon;
Rigidbody2D rigi;
[SerializeField] float rotationSpeed = 200f;
[SerializeField] float engineThrust = 100;
[SerializeField] ParticleSystem engineParticle;
[SerializeField] float fuel = 100;
private float _canFire = 0.0f;
public float fireRate = 0.2f;
public float health = 100;
public Text fuelCount;
public Collider2D target;
// Use this for initialization
void Start () {
rigi = GetComponent<Rigidbody2D>();
myWeapon = WeaponType.Lazer;
}
private void OnCollisionEnter2D(Collision2D collision)
{
health = (health - 50);
}
private void RespondToThrustInput()
{
if (Input.GetKey(KeyCode.W))
{
applyThrust();
}
}
void applyThrust()
{
if (Input.GetKey(KeyCode.Space)&& fuel > 0)
{
rigi.AddRelativeForce(Vector2.up * engineThrust * 2 * Time.deltaTime);
fuel -= 10 * Time.deltaTime;
}
else
{
rigi.AddRelativeForce(Vector2.up * engineThrust * Time.deltaTime);
}
}
private void Update()
{
ClickSelect();
}
void FixedUpdate () {
RespondToThrustInput();
RespondToRotateInput();
CheckHealth();
RespondToFire();
fuelCount.text = "Fuel:" + fuel.ToString();
}
void RespondToFire()
{
if (Input.GetButton("Fire1") && Time.time > _canFire)
{
// Create the Bullet from the Bullet Prefab
var lazer = (GameObject)Instantiate(
lazerBolt,
lazerBoltSpawn.position,
lazerBoltSpawn.rotation);
_canFire = Time.time + fireRate;
lazer.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.up * 2f * Time.deltaTime);
Destroy(lazer, 2.0f);
}
}
private void CheckHealth()
{
if(health <= 0)
{
SceneManager.LoadScene(0);
}
}
private void RespondToRotateInput()
{
if (Input.GetKey(KeyCode.A))
{
transform.Rotate(Vector3.forward *rotationSpeed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D))
{
transform.Rotate(-Vector3.forward *rotationSpeed * Time.deltaTime);
}
}
void ClickSelect()
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (hit.collider != null)
{
Debug.Log(hit.collider.name);
target = hit.collider;
hit.collider.SendMessage("CreateTargetBox");
}
}
}
}
and here is the code for the enemy ship.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy1 : MonoBehaviour {
public int healthPoints = 100;
public GameObject targetLock;
PlayerShip.WeaponType weaponType;
public GameObject targetBox;
private bool isTarget = false;
// Use this for initialization
void Start () {
}
public void CreateTargetBox()
{
Instantiate(targetBox, transform.position, transform.rotation, transform.parent);
}
private void OnTriggerEnter2D(Collider2D collider)
{
print("Triggered!");
if(weaponType == PlayerShip.WeaponType.Lazer && collider.gameObject.tag == "projectile")
{
healthPoints -= 20;
}
if (collider.gameObject.tag == "projectile")
{
Destroy(collider.gameObject);
}
if (collider.gameObject.tag == "Player")
{
healthPoints -= 50;
}
}
private void Update()
{
}
// Update is called once per frame
void FixedUpdate () {
if (healthPoints <= 0)
{
Destroy(this.gameObject);
}
}
}
Sorry for the long post and terrible formatting i am having a hard time with markdown.
Answer by Diukrone · Jan 19, 2018 at 11:59 PM
Maybe it could help you, if you to read the code will see the logic!
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour
5. {
public GameObject original;
void Awake()
{
10. GameObject clone = (GameObject)Instantiate (original, transform.position, Quaternion.identity);
Destroy (clone, 1.0f);
}
}
Follow this Question
Related Questions
Why Is there A slight jitteryness on player movement and camera? 0 Answers
My player is not rotating upwards when it moves upwards. Any advice? 1 Answer
Can anybody help me with this? Character bodying is falling apart. 0 Answers
How to Continue Enemy Movement Without Updating the Position 0 Answers
How to create soil or sand?? 1 Answer