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 Robomaster · Jan 15, 2013 at 01:47 AM · targettargeting

Targeting Script

Okay so what im trying to do is make it so that when the raycast hits an object its able to determine what it is. So like Mage, Warrior, Archer, etc. And then code it to say if Mage(for example) was selected activate this script. I already know the code of how to activate and deactivate scripts, so all i really need help with is on how to make the script recognize what was selected. And then how to say if-this-object-was-selected then... Thanks in advance!!!

 using UnityEngine;
 using System.Collections;
 
 public class HeroTargeting : MonoBehaviour {
     
     public Transform selectedTarget;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
          if (Input.GetMouseButtonUp(1)){ // when button clicked...
       RaycastHit hit; // cast a ray from mouse pointer:
       Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       // if enemy hit...
       if (Physics.Raycast(ray, out hit) && hit.transform.CompareTag("Hero")){
         DeselectTarget(); // deselect previous target (if any)...
         selectedTarget = hit.transform; // set the new one...
         SelectTarget(); // and select it
       
         
         
             }
     }
   }
 
   private void SelectTarget(){
    
             
     }
     
 
   private void DeselectTarget(){
     if (selectedTarget){ // if any guy selected, deselect it
      
       selectedTarget = null;
     
         }
     
         }
     
     }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cdrandin · Jan 15, 2013 at 02:06 AM

You could have a basic class which contains the player's stats. like,, hp,mana,class(warrior,etc),etc. class this BaseChar script.

Create your multiple objects which could be multiple players. Something like..

 player1 = BaseChar()
 player2 = BaseChar()

then, you could use a Ray to check collision. Create a ray when mouse button is pressed. Have this ray 'point' out to into the game world which will make an invidsble line and have another script check for collision. Then when a collision has occurred.

 OnCollisionEnter( object : Collider ){
    object.GetComponent(BaseChar)   // <-- this gives you the character stats

    // so say you have a function to display all the properties of the class like..
    object.GetComponent(BaseChar).printAll() // which could print every variable you want
 }


You will need to use Raycasting to check when the collision hits some object http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-distance.html

I am not 100% sure on the exact code needed. You will need to look up Ray and RaycastHit.

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 Robomaster · Jan 15, 2013 at 02:12 AM 0
Share

Im sorry i think i might have worded it wrong. But like what im trying to do is make it so that when the raycast hits an object its able to deter$$anonymous$$e what it is. So like $$anonymous$$age, Warrior, Archer, etc. And then code it to say if $$anonymous$$age(for example) was selected activate or do this.

avatar image
0

Answer by robertbu · Jan 16, 2013 at 01:09 AM

@cdrandin's answer does answer your questions, but he is also showing you a good way of structuring the data and routines assiciated with a specific character. Here is a simpler example (done in C# sorry): Make a script that is empty for single global variable called type.

 public class CharacterType : MonoBehaviour
 {
     public int type = 0;
 }

In Javascript I think you can do this in a single line like "var type : int = 0;" Attach this script to all your character objects. Set the value of type in the inspector to different values for different character types: 0 - Mage, 1 - Warrior, 2 - Archer, etc;

Now to get that data from a collision you do something like this (C# again)

 RaycastHit hit;
 if (Physics.Raycast(ray, out hit))
 {
     CharacterType ct = hit.collider.gameObject.GetComponent<CharacterType>();
     if (ct != null)
         Debug.Log ("Character Type = "+ct.type);
 }

Using the RaycastHit and GetComponent(), you can get access to any script attached to the game object, and the information about your character can be stored with the object.

Comment
Add comment · Show 3 · 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 youngapprentice · Jan 16, 2013 at 01:15 AM 0
Share

Additionally he could simply create a CharacterType enum and have it accessible from the inspector. That is how I created bullet prefabs for my game.

avatar image Robomaster · Jan 16, 2013 at 01:37 AM 0
Share

well like its not exactly storing the information about a player its more activating a certain script only when a certain hero is selected. I tried using

mage = GameObject.Find("$$anonymous$$age");

if(target == mage){

mage = true;

}

but it doesnt seem to be working when ever i click on $$anonymous$$age in the game. is it coded wrong?

avatar image youngapprentice · Jan 16, 2013 at 01:58 AM 0
Share

I just wrote a humongous answer then actually deleted it by accident when I wanted to edit it. -_- I am pretty sloppy after an afternoon run.

What I wanted to say is that robertbu and crandin have already answered your questions.

Two EXTRE$$anonymous$$ELY useful things in your case are CLASSES and ENU$$anonymous$$S. Read up on them. Enums pretty much let you declare a custom variable type. (You can make an enum called CharacterType with $$anonymous$$age, Wizard, Warrior, etc as possible types.

You can then make a class like crandin said. You should read up on classes. They are very useful and make coding much easier. A great video on classes can be found here:

http://www.youtube.com/watch?v=Z9EgWdwqebw&list=PL212CA7BA21E27F5E

($$anonymous$$ake sure to watch part 2 as well). Actually, that entire tutorial is pretty good. He goes very in-depth with his explanations.

These two things tie in because you can make a class, and have a variable in that class be of type CharacterType, and then when you go to the checking code, you can just do myObject.type == CharacterType.$$anonymous$$age. Problem solved.

avatar image
0

Answer by deltamish · Jan 16, 2013 at 02:03 AM

HI,You can do it two ways one is by creating an enum other by using tags.if you want to use tag Here is the script By the way what is "out hit"i didn't see you declare it any where and dont use && hit.transform.CompareTag("Hero")) after Physics.Raycast because if you do so it only casts a ray only when its an object tagged("Hero").Since there is no raycasting it cant check wether it has hit an object with tag(Hero) or not The ray shooting script(Alter these in your script)

 Transform selectedTarget;
 
 
 void Update(){
     if (Input.GetMouseButtonUp(1)){ // when button clicked...
           RaycastHit hit; // cast a ray from mouse pointer:
           Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
           // if enemy hit...
               if (Physics.Raycast(ray,hit,10000)){
     if(hit.collider.tag == "Hero"){
     DeselectTarget(); // deselect previous target (if any)...
                  selectedTarget = hit.transform; // set the new one...
     
        SelectTarget(selectedTarget); // and select it
     }
         }
       }
     }
     function SelectTarget(Transform obj){
     obj.GetComponent<Hero_Script>().selected = true;/////you can replace Hero_Script with your script name
     }
     Transform[] Army;///Add your hero and all other AI to this array
     
     DeselectTarget(){
     foreach(int obj =0;obj< Army.Length;obj++){
     if(Army[obj]!= selectedTarget){
     Army[obj].GetComponent<Your AI Script name>().selected = false;
     }
     }
     }

Now just add this to your Hero or Mage script

 bool selected;
 
 //your other code
 void Update(){
 if(bool){
 ///your code goes here
 
 }else{
 ///your code to make the Ai go to idle animation 
 }
 }
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

12 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

Related Questions

Camera re-target 2 Answers

How to use Float as Name in GameObject.Find command? 1 Answer

Why don't my send messages work? 2 Answers

Changing Head Look Controller Target 1 Answer

Choose a Random Tag 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