- Home /
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;
}
}
}
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.
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.
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.
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.
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?
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.
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
}
}
Your answer
Follow this Question
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