system
1
I use to objects (lights)… when I go near them and hold click them will follow us… and when I release it it won’t any more… it work perfectly … but when there is two light (gameobject) are near each other and we get near them and hold left click they bth will follow us… but I just want that one of them follow… I used bool inhand == true… but it doesn’t work…
additional info :
lf = lightflare
the first and second lines in update related to the brightness of flare… doesn’t related to this…
instead of setting distance… I sayed if the brightness get higher than X (the flare will get more bright when we get near them )
using UnityEngine;
using System.Collections;
public class LightColor : MonoBehaviour{
public LensFlare lf;
public Transform tr;
SmoothFollow SF;
public Flare newlf;
bool inhand = false;
public Flare origlf;
void Start(){
SF = (SmoothFollow)gameObject.GetComponentInChildren();
}
void Update(){
if (inhand == false){
lf.brightness = 45 / Vector3.Distance(lf.transform.position, tr.position);
}
if (lf.brightness > 6 & inhand == false){
if (Input.GetMouseButtonDown(0)){
inhand = true;
lf.brightness = 1;
lf.flare = newlf;
SF.enabled = true;
}
}
if (inhand == true){
if (Input.GetMouseButtonUp (0)){
SF.enabled = false;
lf.flare = origlf;
inhand = false;
}
}
}
}
Declare inhand a public static variable, like this:
public static bool inhand;
Static variables are created at program start and survive while the program is running. They are unique: there will exist only one inhand variable - even if you have several instances of the same script running at the same time. All of these instances will read and write the same inhand variable, so when one of the lights set it all the others will be blocked due to your logic.
EDITED:
When the mouse button is pressed, all lights up to 7.5 m from the player are candidates to be grabbed - but the one whose script executes first sets inhand and follows the player. When the mouse button is released, however, the light whose script executes first resets inhand - no matter at which distance it is, or if any other light was following the player, and this avoid other lights to be released.
The solution is to have one more flag - the non-static variable isMe - which shows if this light is selected. The logic thus is modified to only allow grabbing lights when inhand is false; when any light is grabbed, inhand and isMe are set; when the mouse button is released, only the light with isMe set will be released.
This is the script with the modified logic:
using UnityEngine;
using System.Collections;
public class LightColor : MonoBehaviour{
public LensFlare lf;
public Transform tr;
SmoothFollow SF;
public Flare newlf;
public static bool inhand = false; // true if some light in hand
public bool isMe = false; // true if THIS light is in hand
public Flare origlf;
void Start(){
SF = (SmoothFollow)gameObject.GetComponentInChildren();
}
void Update(){
if (inhand == false){ // if no lights grabbed...
lf.brightness = 45 / Vector3.Distance(lf.transform.position, tr.position);
if (lf.brightness > 6 && Input.GetMouseButtonDown(0)){ // and mouse button down...
isMe = true; // I'm in hand now
inhand = true; // avoid grabbing other lights
lf.brightness = 1;
lf.flare = newlf;
SF.enabled = true;
}
}
// if I'm in hand and mouse button is released...
if (isMe && Input.GetMouseButtonUp (0)){
SF.enabled = false; // stop following
lf.flare = origlf;
isMe = false; // I'm not in hand anymore
inhand = false; // allow other lights to be grabbed
}
}
}