Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jul 08, 2019 at 08:01 AM by nubbinownz for the following reason:

Old question never really answered

avatar image
0
Question by nubbinownz · Jan 19, 2018 at 10:52 PM · 2d game2d-physics2d spritesspacespace shooter

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

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);
      }
  }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

85 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges