- Home /
How to set distance?
(JAVASCRIPT) I want to make it so you have to turn off a transformer. So far I have this:
#pragma strict
function Start () {
light.color = Color.cyan;
}
function OnMouseOver ()
{
if
if ( Input.GetMouseButtonDown(0))
{
light.color = Color.red;
}
}
This script is of course incorporated with the light, not the transformer itself. (Check images for before and after)
Notice how far I am. I want to make it so you stand, for instance, a yard away, at most.
I don't really understand, do you want to only change the light when you're close to the object?
if so use if(Vector3.Distance(transform.position , player.transform.position){"your code"}
what do you mean by transformer? do you mean gameobject.transform or do you mean the step up transformers used in electrical distribution?
also please format your code... sometimes the editor does a really poor job of doing it the first time so it can be necessary to re-paste it.
so far all I know is you want something red and at a distance you want something to turn off..
Okay... Sorry for confusing you guys... @gjf I do not understand what you mean by code tags, I am new to this.
@vlames I want to change it when I click the left mouse button A$$anonymous$$A what I have, but I want it to change color, when I click it, WHILE close.
@Fornoreason1000 I do mean transformer to distribute electricity. That's part of my game, where you have to turn it off. Re-format as in the coding?
~~~ START SCRIPT ~~~
function Start () { light.color = Color.cyan; }
function On$$anonymous$$ouseOver () { if ( Input.Get$$anonymous$$ouseButtonDown(0))
{
light.color = Color.red;
}
}
~~~ END SCRIPT ~~~
Easier? I know what you mean, haha, it didn't paste in well.
Ok now we are on the same page...
This what i meant I edited you code... you said 1 yard... so i set it to work within exactly one yard
function Start () {
light.color = Color.cyan;
}
function On$$anonymous$$ouseOver () {
if (Input.Get$$anonymous$$ouseButtonDown(0) && (Vector3.Distance(transform.position , player.transform.position) < 0.9144f )
{
light.color = Color.red;
}
}
Answer by FirePlantGames · Jul 16, 2014 at 01:52 AM
var player : Transform;
var minDistance : float;
function Start () {
light.color = Color.cyan;
}
function OnMouseOver ()
{
if ( Input.GetMouseButtonDown(0) && Vector3.Distance(transform.position, player.position) <= minDistance)
{
light.color = Color.red;
}
}
that should work, let me know if it works!
Your answer
Follow this Question
Related Questions
Saving data to xml file (I have the loading down) 0 Answers
Customize a scripted GUI 1 Answer
adding multiple values to single function/variable. 0 Answers
UnityScript to C# conversion help? 1 Answer
Coordinates of GUITexture 0 Answers
