- Home /
Advanced Targetting
Hello! I've been trying to create my own targeting system for a while now, but I'm having a tad bit of trouble creating script. I don't have a script to post now since I'm in school, but I'll post the scripts I have already in the later hours today, so I'll describe it the best way I can.
So I have a script attached to a GUITexture I believe. The crosshair replaces the mouse, and moves around. BTW, my camera has the mouse orbit script. What I needed, is for when the crosshair(mouse) is over a gameobject with a tag("Enemy") and is within a certain distance of the Enemy, the Player(also tagged "Player", which BTW is a Platform Controller), must look at the enemy. I know that the LookAt may not work quite well with a Platform Controller, but it'll work for another idea I have. This has to work for all enemies in the world.
If you could help me out a bit, that be GREAT! :)
var nativeRatio = 1.3333333333333;
private var lastPosition : Vector3;
var normalAlpha = .5;
var fadeTo = .2;
var fadeRate = .22;
private var cursorIsFading = true;
private var fadeValue : float;
function Start() { var currentRatio : float = (0.0 + Screen.width) / Screen.height; transform.localScale.x *= nativeRatio / currentRatio; Screen.showCursor = false; fadeValue = normalAlpha; lastPosition = Input.mousePosition; }
function Update() { var mousePos = Input.mousePosition;
if (mousePos != lastPosition) {
lastPosition = mousePos;
$$anonymous$$ove$$anonymous$$ouse(mousePos.x/Screen.width, mousePos.y/Screen.height);
}
if (cursorIsFading) {
guiTexture.color.a = fadeValue;
fadeValue -= fadeRate * Time.deltaTime;
if (fadeValue < fadeTo) {
fadeValue = fadeTo;
cursorIsFading = false;
}
}
}
function $$anonymous$$ove$$anonymous$$ouse(mousePosX : float, mousePosY : float) {
guiTexture.color.a = fadeValue = normalAlpha;
guiTexture.texture = normalCursor;
cursorIsFading = true;
if (mousePosX < .005) {
mousePosX = .005;
guiTexture.texture = leftCursor;
}
if (mousePosX > .995) {
mousePosX = .995;
guiTexture.texture = rightCursor;
}
if (mousePosY < .005) {
mousePosY = .005;
guiTexture.texture = downCursor;
}
if (mousePosY > .995) {
mousePosY = .995;
guiTexture.texture = upCursor;
}
transform.position.x = mousePosX;
transform.position.y = mousePosY;
}
This is what I have for the crosshair.
Answer by Berenger · Jan 27, 2012 at 03:29 AM
You need to take a look at Raycast, and ScreenPoinToRay.
Your answer
Follow this Question
Related Questions
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
Move GUI elements. 0 Answers
GUITexture Button? 1 Answer
GUI Transparency Changing automatically 2 Answers
Write a Shader for GUI (GUITexture or scripted GUI) 0 Answers