Hey guys,
I have coded a small crosshair script and now i want to add so that my crosshair changes to another texture when you hover over any rigidbody object in my scene. I am not getting any error messages which is a good sign but neither am i getting any progress.
Heres my script:
var CrosshairTexture : Texture2D;
var InteractIcon : Texture2D;
var Crosshairisenabled = true;
var CrosshairsizeX : int = 64;
var CrosshairsizeY : int = 64;
var hit : RaycastHit;
function OnGUI(){
GUI.DrawTexture (Rect(Input.mousePosition.x-CrosshairsizeX/3, (Screen.height-Input.mousePosition.y)-CrosshairsizeY/3, CrosshairsizeX, CrosshairsizeY),CrosshairTexture);
}
Screen.lockCursor = true;
function Update() {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
InteractIcon == true;
}
}
InteractIcon is a Texture2D type, not a boolean, so I’m not sure what InteractIcon == true is supposed to do. You’re also testing for equality (==) instead of assignment (=), so that’s another issue.
You’re probably looking for something like this:
var crosshairTexture : Texture2D;
var interactIcon : Texture2D;
private var currentTexture : Texture2D; // The current texture to draw
var crosshairsizeX : int = 64;
var crosshairsizeY : int = 64;
function Start() {
currentTexture = crosshairTexture; // Set default texture;
}
function OnGUI(){
GUI.DrawTexture (Rect(Input.mousePosition.x-crosshairsizeX/3, (Screen.height-Input.mousePosition.y)-crosshairsizeY/3, crosshairsizeX, crosshairsizeY), currentTexture);
}
function Update() {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
currentTexture = interactIcon; // Set interact texture
} else {
currentTexture = crosshairTexture; // Set default texture
}
}
Thanks for the quick responses! I have somewhat managed to make it work by adding some additional lines of code but now the icon appears on ANY object at 2 meters distance (which makes sense according to the code) and not ONLY rigidbodies… any ideas? I know you can use tags to rule out colliders for this but there must be a way without them. Sorry im more of a modeler than a scripter…
var crosshairTexture : Texture2D;
var interactIcon : Texture2D;
private var currentTexture : Texture2D; // The current texture to draw
var crosshairsizeX : int = 32;
var crosshairsizeY : int = 32;
function Start() {
currentTexture = crosshairTexture; // Set default texture;
}
function OnGUI(){
GUI.DrawTexture (Rect(Input.mousePosition.x-crosshairsizeX/3, (Screen.height-Input.mousePosition.y)-crosshairsizeY/3, crosshairsizeX, crosshairsizeY), currentTexture);
}
function Update() {
var fwd = transform.TransformDirection (Vector3.forward);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (transform.position, fwd, 2)) {
currentTexture = interactIcon; // Set interact texture
} else {
currentTexture = crosshairTexture; // Set default texture
}
}
You can check the hit if it has a rigidbody.
if (Physics.Raycast (transform.position, fwd, hit, 2)) {
if (hit.rigidbody != null) { // Does the hit have a rigidbody?
currentTexture = interactIcon; // Set interact texture
} else {
currentTexture = crosshairTexture; // Set default texture
}
} else {
Also, if you don’t use the ray for anything
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
is not needed.
Also
transform.TransformDirection(Vector3.forward);
is the same as
transform.forward
I added
if(hit.rigidbody!=null){
and it seems to work, yet it seems like the icon is a bit glitchy.
If i look at the rigidbody from the distance that i entered it does transform the crosshair yet when i look at it and walk backwards and then look away the interacticon stays on screen…
Do you set the interacticon both when you miss any colliders and when you hit, but not a rigidbody?