Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by ArminAnderson · Sep 06, 2013 at 12:58 AM · arraystargetselectionenemies

Trying to make gameObjects the target on right click

I have an array that adds enemies.

using UnityEngine;

using System.Collections;

using Pathfinding;

public class LookAt : MonoBehaviour {

 //public Transform target;
 public GameObject tank;
 public GameObject turret;
 public float NoDelay = 0;
 public float fireDelay = 5.0f;
 public float speed = 100;
 public float slightDelay = 3f;
 public GameObject bullet;
 public float radius = 50.0f;
 public GameObject bulletSpawn;
 public LayerMask mask;
 public float fireRate = 0;
 //public Transform target;

 
 private GameObject target;
 
 void OnTriggerEnter(){
     
 }

 void Update(){
     
     Collider[] enemyArray = Physics.OverlapSphere(transform.position, radius, mask);
     if(enemyArray.Length > 0){
         target = enemyArray[0].gameObject;
         InvokeRepeating("targetingSystem", NoDelay, NoDelay);
         InvokeRepeating("Shoot", fireRate, slightDelay);
         
     }
     if(enemyArray.Length < 1){
         turret.transform.rotation = tank.transform.rotation;
         target = null;
         Invoke("OutOfRange", NoDelay);
     }
 
 }
 void targetingSystem(){
         turret.transform.LookAt(target.transform.position);
 }
 
 void Shoot(){    
         
         GameObject instance = Instantiate(bullet, bulletSpawn.transform.position, turret.transform.rotation) as GameObject;
         if(instance.rigidbody)
         {
             instance.rigidbody.AddRelativeForce((Vector3.forward).normalized*speed*Time.deltaTime,ForceMode.VelocityChange);
         }
         Invoke("OutOfRange", NoDelay);
         

 }
 
 void OutOfRange(){
     CancelInvoke("targetingSystem");
     CancelInvoke("Shoot");
 }
 

}

Id like to make it so if you right click on an enemy, they become the target. Anyone know how to do this?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by perchik · Sep 06, 2013 at 01:34 AM

In general, I know the shape of the answer, but I'm not entirely sure about the code.

What you want to do is on Update check to see if they right clicked. If they did, do a raycast from the mouse position to see if they hit any of the objects that can become the target. Unfortunately, Raycasting is something I know conceptually, but has always been handled by someone else on my team...

 void Update(){
 
 if( Input.GetMouseButton(1)) //rightclick
 {
     //raycast 
 }
Comment
Add comment · Show 1 · 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
avatar image ArminAnderson · Sep 08, 2013 at 07:08 PM 0
Share

thats what i thought, but wasnt sure on how to do it.

avatar image
0

Answer by RyanZimmerman87 · Sep 08, 2013 at 07:22 PM

Hard to tell if this is what you want exactly but here is an example:

 //user clicks
 
 if( Input.GetMouseButton(1)) //rightclick
 {
 
 //creates a ray from camera to mouse position
 //creates a rayHit to detect the object hit
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit rayHit;
 
 //casts ray 150 meters with rayHit to detect object hit
 //could add layer masks as an additional condition after the distance
 if (Physics.Raycast(ray, out rayHit, 150))
 {
 //can get position of ray hit for transforms/vector calculations
 rayVector = rayHit.point;
 
 
 //get the ray hit object 
 rayHitObject = rayHit.collider.gameObject;
 
 //get the ray name or could also get the tag
 stringRayName = rayHitObject.name;
 
 }
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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Instantiation array of enemies, c# ? 2 Answers

Why can't I set the Target 1 Answer

Cycle through an array? Have Camera Follow different Targets. 1 Answer

faster target selection 2 Answers

Removing objects from array in reverse order 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