- Home /
Targeting an enemy trouble
Ill make it simple:
I would like a code that when i click on my enemy , my enemy gets targeted.
Cant find it online :/
I got a 3rd person camera and my main camera is removed.
This is incredibly vague. Please try to ask specific questions if you'd like a decent answer.
What do you mean by target? Do you want to make something like a lock-on feature as in fighter jet games?
Answer by FLASHDENMARK · Jan 14, 2012 at 07:21 PM
Since this is a really vague question my options of helping you are limited. But you could use ScreenPointToRay to cast a ray from the Input.mousePosition and attach the enemy as a target:
//Attatch to camera
var target : GameObject; //The target you want to attack
function Update (){
var hit : RaycastHit;
if(Input.GetButtonDown("Fire1")){
var ray : Ray = gameObject.camera.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
if(hit.collider.tag == "Enemy"){
target = hit.collider.gameObject;
}
}
}
//Continue logic
}
Basic example. Hope it gets you going.
Your answer
Follow this Question
Related Questions
Homing Missile not maintaining forward velocity 1 Answer
Floor Halo Target System 0 Answers
Track objects in camera view using GUI? Like a HUD for flight sim lockon targeting. 1 Answer
Circle strafing/Z-Targeting 1 Answer
Camera re-target 2 Answers