So 'im trying to do a lock system for a 2d sidescrolling game. The player can grip the wall or the ceil. When the player is gripping and enemies come close enough i want him allow to lock a target and switch betweens them. I’m a beginnner in scripting so it’s kinda lot of work for me.
So for now i was thinking to use a raycast to detect if an enemy is close enough. I want multiple raycast under the player from -180 degree (behind the player) to 0 (front the player). Or maybe do some radar with a raycast which rotate betwen those angles and stop when he hit an enemy. Evrything i tried didn’t work. Can anyone give me some help plz ?
Any help plz ? I’m kinda desperate, i can’t figure out a good way to achieve what i want. If you d’ont understand what i’m trying to do i can draw you a sketch ^^
ok. So there is a picture. Raycast in red, So i want my rascast to rotate to reach all directions under the black line, and detect enemy in range, stop when hit one, and switch to the next enemy in range when i press a key. I tried differents ways to achieve this but i always got problems. In fact when a raycast is lock onto an enemy when i press attack i want my char jump and attack the enemy, but this come after.
I dun know if it’s the right way to achieve what i need. So plz if you can lead me, i’ll be gratefull.
If I understood right how your game is supposed to work, this script is the start:
var targetsList : Transform[];
var selectedTarget : int = 0;
var distance : float = 20.0;
function Update(){
//is target under the player?
if(targetsList[selectedTarget].transform.position.y <= transform.position.y){
//check if your targets distance from player is more than distance variable
if(Vector3.Distance(targetsList[selectedTarget].transform.position,transform.position) < distance){
print("Target id: "+selectedTarget+" is in range");
}
}
//if player presses tab target another enemy
if(Input.GetKeyDown(KeyCode.Tab))
selectedTarget++;
}
if you wanted to only target enemies then they enter your players visibility area you can do some looping or use triggers
Sry for not answering faster i was absent the last 2 weeks. Thx for the help.
So i did something a little different :
var distance : float = 10.0;
function Update (){
Lock();
}
function Lock(){
var targetsList : GameObject[];
targetList = GameObject.FindGameObjectsWithTag ("Enemy");
var selectedTarget : GameObject;
for (selectedTarget in targetList) {
//is target under the player?
if(selectedTarget.transform.position.y < transform.position.y){
//check if your targets distance from player is more than distance variable
if(Vector3.Distance(selectedTarget.transform.position,transform.position) < distance){
print("Target id: "+selectedTarget.name+" is in range");
}
}
}
}
There is a way to stock my var targetList just once instead of calling it in lock script all the time ? Cause i tried to put it at the begining and when i want to call it in my lock script it can’t find the var.
Also i want the selectedTarget var to be the closest gameObject in range. How can i do this ? How do i compare all the targetList gameObject distance to arrange them from the smalest to the largest.
Then my fisrt selectedTarget will be the closest one and when i’ll press tab the selectedTarget will be the next closest one…etc
Any help ?