x


distance to object + mouse aiming

ok its late and it might be because i'm tired that i dont get this but i'm having some trouble with distance detection. i am currently using the mouse look script and "Screen.lockCursor = true;" command to center the mouse. i can set it up so that when i mouse over / aim at the object it changes a color but i want it to change when i am at a certain distance away from the object. this is what i have pieced together from several sources and attempted to add in a distance calculator

var dist;

var vector; function FixedUpdate () { var dist = GameObject.FindWithTag("Player").transform.position - transform.position; dist.y = 0; vector = dist.magnitude; }

function OnMouseEnter() { if (vector <= .5 ) { renderer.material.color = Color.black; }

else { } } i have a version of my game hosted at my site [http://www.hazardousproductions.net/Testing/Testing.html][1]

with this code in there (the look and highlight command) var dist; var vector; function FixedUpdate () { var dist = GameObject.FindWithTag("Player").transform.position - transform.position; dist.y = 0; vector = dist.magnitude; }

function OnMouseEnter() { renderer.material.color = Color.black; }

function OnMouseExit() { renderer.material.color = Color.black; }

thanks in advance

note:

in the game use the mouse to look around w a s d or arrow keys to walk around and the level the problem is on is level 1

more ▼

asked Jul 12 '11 at 03:14 AM

jpeg gravatar image

jpeg
1 4 6 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

i was over thinking the whole thing lol. i used raycast to detect the distance from the object and ditched the mouseenter idea because it wasnt working out correctly. my somewhat final code

   var dist;
var close;


function Update ()
{
dist = transform.TransformDirection (- Vector3.forward);

if (Physics.Raycast (transform.position, dist, 1))
{
close = 1;
renderer.material.color = Color.black;
}
else
{
close = 0;
renderer.material.color = Color.white;
}
if(Input.GetButton("Use"))
{
if ( close > 0)
{
renderer.material.color = Color.green;
}
}
}
more ▼

answered Jul 12 '11 at 02:52 PM

jpeg gravatar image

jpeg
1 4 6 8

(comments are locked)
10|3000 characters needed characters left

Raycast should do the trick:

var RaycastToCenterHit : RaycastHit;
var cameraTransformRaycast : Transform = Camera.main.transform;
Physics.Raycast(cameraTransformRaycast.position, cameraTransformRaycast.TransformDirection(Vector3.forward), RaycastToCenterHit);
if (RaycastToCenterHit.distance <= minimalDistance){
//Color the switch
}
more ▼

answered Jul 12 '11 at 03:27 AM

nbase gravatar image

nbase
21 1 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x355

asked: Jul 12 '11 at 03:14 AM

Seen: 446 times

Last Updated: Jul 12 '11 at 02:52 PM