Зміна кольору або прозорості об'єкта

Доброго дня. В тій чи іншій формі це питання вже задавалося на форуме.Но на жаль для мене так і не прояснився. Завдання таке: - в сцені декілька об'єктів (мешів, вкладений префаб), 10-20шт (деякі вкладені). Потрібно при натисканні миші або приховувати їх вибірково. Або (що набагато краще.) Змінювати матеріал (а краще прозорість) об'єкта. Аж до його ісчезновенія.Одін клік виділення (об'єкт підсвічується), другий клік 50% прозорості, третій клік нульова відімость.Ну і бажано мати можливість повернути видимість всіх таким чином прихованих об'єктів.






Зараз у мене на камері для обертання навколо цієї групи висить знайдений в мережі скрипт:

# 91; AddComponentMenu # 40; "Camera-Control / 3dsMax Camera Style" # 41; # 93;

public class maxCamera. MonoBehaviour

public Transform target;

public Vector3 targetOffset;

public float distance = 5.0f;

public float maxDistance = 20;

public float minDistance = .6f;

public float xSpeed ​​= 200.0f;

public float ySpeed ​​= 200.0f;

public int yMinLimit = - 80;

public int yMaxLimit = 80;

public int zoomRate = 40;

public float panSpeed ​​= 0.3f;

public float zoomDampening = 5.0f;

private float xDeg = 0.0f;

private float yDeg = 0.0f;

private float currentDistance;

private float desiredDistance;

private Quaternion currentRotation;

private Quaternion desiredRotation;

private Quaternion rotation;

private Vector3 position;

void Start # 40; # 41; # 123; Init # 40; # 41; ; # 125;

void OnEnable # 40; # 41; # 123; Init # 40; # 41; ; # 125;

public void Init # 40; # 41;

// If there is no target, create a temporary target at 'distance' from the cameras current viewpoint

if # 40 ;. target # 41;

GameObject go = new GameObject # 40; "Cam Target" # 41; ;

go. transform. position = transform. position + # 40; transform. forward * distance # 41; ;

target = go. transform;

distance = Vector3. Distance # 40; transform. position. target. position # 41; ;

// be sure to grab the current rotations as starting points.

position = transform. position;

rotation = transform. rotation;

currentRotation = transform. rotation;

desiredRotation = transform. rotation;

xDeg = Vector3. Angle # 40; Vector3. right. transform. right # 41; ;

yDeg = Vector3. Angle # 40; Vector3. up. transform. up # 41; ;

* Camera logic on LateUpdate to only update after all character movement logic has been handled.

void LateUpdate # 40; # 41;

// If Control and Alt and Middle button? ZOOM!

if # 40; Input. GetMouseButton # 40; 1 # 41; # 41;

desiredDistance - = Input. GetAxis # 40; "Mouse X" # 41; * Time. deltaTime * zoomRate * 0.125f * Mathf. Abs # 40; desiredDistance # 41; ;

// If middle mouse and left alt are selected? ORBIT

else if # 40; Input. GetMouseButton # 40; 0 # 41; # 41;

xDeg + = Input. GetAxis # 40; "Mouse X" # 41; * XSpeed ​​* 0.02f;

yDeg - = Input. GetAxis # 40; "Mouse Y" # 41; * YSpeed ​​* 0.02f;







// Clamp the vertical axis for the orbit

yDeg = ClampAngle # 40; yDeg, yMinLimit, yMaxLimit # 41; ;

// set camera rotation

desiredRotation = Quaternion. Euler # 40; yDeg, xDeg, 0 # 41; ;

currentRotation = transform. rotation;

rotation = Quaternion. Lerp # 40; currentRotation, desiredRotation, Time. deltaTime * zoomDampening # 41; ;

transform. rotation = rotation;

// otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace

else if # 40; Input. GetMouseButton # 40; 2 # 41; # 41;

// grab the rotation of the camera so we can move in a psuedo local XY space

target. rotation = transform. rotation;

target. Translate # 40; Vector3. right * - Input. GetAxis # 40; "Mouse X" # 41; * panSpeed # 41; ;

target. Translate # 40; transform. up * - Input. GetAxis # 40; "Mouse Y" # 41; * PanSpeed, Space. World # 41; ;

// affect the desired Zoom distance if we roll the scrollwheel

desiredDistance - = Input. GetAxis # 40; "Mouse ScrollWheel" # 41; * Time. deltaTime * zoomRate * Mathf. Abs # 40; desiredDistance # 41; ;

// clamp the zoom min / max

desiredDistance = Mathf. Clamp # 40; desiredDistance, minDistance, maxDistance # 41; ;

// For smoothing of the zoom, lerp distance

currentDistance = Mathf. Lerp # 40; currentDistance, desiredDistance, Time. deltaTime * zoomDampening # 41; ;

// calculate position based on the new currentDistance

position = target. position - # 40; rotation * Vector3. forward * currentDistance + targetOffset # 41; ;

transform. position = position;

private static float ClampAngle # 40; float angle, float min, float max # 41;

if # 40; angle <- 360 )

if # 40; angle> 360 # 41;

return Mathf. Clamp # 40; angle, min, max # 41; ;


Оскільки я дуже слабо (а точніше зовсім ні.) Розбираюся в програмуванні, сам не подужаю. Єдине, що зрозумів з пошуку в форумі - краще це робити через Raycast.
Чим навішувати скрипт на кожен меш.
Підкажіть будь ласка в якому мені напрямку копати. Або вже є якісь готові рішення.
Може досить буде доповнити той скрипт який вже важить на камері і управляє зараз нею.

Знайшов ось такий код

public class DebugScript. MonoBehaviour # 123;
private bool _debug = true;
void Update # 40; # 41;
# 123;
if # 40; Input. GetMouseButtonUp # 40; 0 # 41; # 41;
# 123;
Ray ray = Camera. main. ScreenPointToRay # 40; Input. mousePosition # 41; ;
RaycastHit hit;
if # 40; Physics. Raycast # 40; ray, out hit, 100 # 41; # 41;
# 123;
if # 40; _debug # 41; Debug. DrawLine # 40; ray. origin. hit. point # 41; ;
if # 40; hit. collider. gameObject. tag == "MyTag" # 41;
hit. collider. gameObject. renderer. material. color = Color. green;
# 125;
# 125;
# 125;
# 125;

Але Unity лається на рядок:

Ray ray = Camera. main. ScreenPointToRay # 40; Input. mousePosition # 41; ;

NullReferenceException
UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C: /BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs: 267)

на жаль мені це ні про що не говорить
example.Update () (at Assets / example.cs: 10)

Може це від того що код писався під більш ранню версію Unity. У мене зараз версія 3.4

тепер теж є) Але не вистачало виявляється коллайдера на об'єкті. Уже запрацювало. Велике спасибі.
Тепер би для повноти початкової завдання навчиться також - відключати виділення у перш обраного об'єкта коли обраний наступний. включати прозорість у об'єктів, приховувати їх і бажано unhide all робити.
Можливо, використовуючи скажімо цей же скрипт, але доповнивши його?

Запіался на курс програмування + html!


мені більше підходить по простоті, і працює, але не можу знайти як повертати вихідний матеріал. Так само не виходить повернути вихідний матеріал і за допомогою: -

public class DebugScript. MonoBehaviour # 123;
private bool _debug = true;
void Update # 40; # 41;
# 123;
if # 40; Input. GetMouseButtonUp # 40; 0 # 41; # 41;
# 123;
Ray ray = Camera. main. ScreenPointToRay # 40; Input. mousePosition # 41; ;
RaycastHit hit;
if # 40; Physics. Raycast # 40; ray, out hit, 100 # 41; # 41;
# 123;
if # 40; _debug # 41; Debug. DrawLine # 40; ray. origin. hit. point # 41; ;
if # 40; hit. collider. gameObject. tag == "MyTag" # 41;
hit. collider. gameObject. renderer. material. color = Color. green;
# 125;
# 125;
# 125;
# 125;

Це варіант зручніше тим що не треба навішувати скрипт на кожен меш.