i am trying to handle with a crosshair, currently as a 3DText
one of my problems are the increasing distance by moving it.
An other, the placement on the correct position
// ##Controling guns##
//Requirements
var cross:Transform;
//placement
private var crossDistance = 10;
private var crossHeigh = 10;
private var crossPosition:Vector3;
//movingSpeed
private var sensitivity = 5;
function Start () {
Screen.showCursor = false;
//place cross
cross.position = transform.position;
cross.Translate(transform.position.x,crossDistance,crossHeigh);
crossPosition = cross.position;
}
//control cross with mouse
function Update () {
cross.Translate(Input.GetAxis("Mouse X") * sensitivity*-1,Input.GetAxis("Mouse Y") * sensitivity,0);
cross.LookAt(transform);
}
You should not move the GUIText’s transform position, you should only affect the GUIText’s pixeloffset in order to moving your cross hair in the screen.
Example Script C#
using UnityEngine;
using System.Collections;
public class TestCursor : MonoBehaviour {
private Vector2 half, pos;
void Start () {
Screen.showCursor = false;
guiText.anchor = TextAnchor.MiddleCenter;
half = new Vector2( Screen.width/2, Screen.height/2 );
pos = Vector2.zero;
}
void Update () {
pos.x = Input.mousePosition.x - half.x;
pos.y = Input.mousePosition.y - half.y;
guiText.pixelOffset = pos;
}
}
If you work with GUITexture, you need to do a few more calculations to include the offset of the image itself, this is because GUITexture uses PixelInset
in oppose to the PixelOffset
used by the GUIText
why are you using 3d text instead 2d texture?
2d texture is the easiest way to achieve a crosshair
just draw the texture with OnGUI function