Added icons on right side of the screen, for coming animals( lions, coyotee).

posted in: Weekly Progress | 0

 

Indication icons for lion and coyote is added .

This code is added to know in which lane coyote or lion is coming .

 

 

using UnityEngine;
using System.Collections;

public class AnimalIconController : MonoBehaviour
{
public LayerMask LionLayerMask;
public float RayDistance;
public Lion AnimalLion;
private const int TotalHorizontalRays = 10;
private Vector3 _rayCastTopLeft;
private Vector3 _rayCastBottomLeft;
private float _verticalDistanceBetweenRays;
private Transform _transform;
private Vector3 _localeScale;
private BoxCollider2D _boxCollider2D;
public GUISkin Skin;
public GameObject IndicationLionPrefab;
private GameObject _indicationlionprefab;
private float pos = Screen.width;
private Vector3 LionIconPosition;
private double life;
private float _camWidth;
public void Start()
{
_transform = transform;
_localeScale = transform.localScale;
float TransformHeight = _localeScale.y;
_verticalDistanceBetweenRays = TransformHeight / (TotalHorizontalRays – 1);
LionIconPosition = new Vector3(0,0,0);
Camera cam = GameObject.FindWithTag(“MainCamera”).GetComponent<Camera>();
_camWidth = cam.orthographicSize;
}

public void Update()
{
// calculate Rays Origin
CalcRayOrigin();
// cast horizontal rays
CastHorizontalRays();
UpdateIconPosition();
}

private void UpdateIconPosition()
{
if (!AnimalLion)
return;
else
{
LionIconPosition.y = AnimalLion.transform.position.y;
LionIconPosition.z = AnimalLion.transform.position.z;
LionIconPosition.x = transform.position.x + _camWidth;
//LionIconPosition.x = transform.position.x + Screen.width – IndicationLionPrefab.transform.localScale.x;
_indicationlionprefab.transform.position= LionIconPosition;
}
}
private void CastHorizontalRays()
{
var rayDistance = RayDistance;
var rayDirection = Vector2.right;
var rayOrigin = _rayCastBottomLeft;

for (int i = 0; i < TotalHorizontalRays; i++)
{
var rayVector = new Vector2(rayOrigin.x, rayOrigin.y + (i * _verticalDistanceBetweenRays));
Debug.DrawRay(rayVector, rayDirection * rayDistance, Color.red);
var rayCastHit = Physics2D.Raycast(rayVector, rayDirection, rayDistance, LionLayerMask);
if (!rayCastHit)
continue;
if (rayDistance < _localeScale.x)
break;

if (AnimalLion)
return;

// check if lion hit
AnimalLion = rayCastHit.collider.gameObject.GetComponent<Lion>();

if (!AnimalLion)
break;
else
DetectLionIcon();
}
}

private void DetectLionIcon()
{

if (AnimalLion.CurrentLane == LevelManager.eThreeLane.TopLane)
{
Debug.Log(“top lane lion”);
CreateLionIndicationIcon(AnimalLion);
//OnGUI();
}
else if (AnimalLion.CurrentLane == LevelManager.eThreeLane.MiddleLane)
{
Debug.Log(“middle lane lion”);
CreateLionIndicationIcon(AnimalLion);
// LionAnimal = true;
}
else if (AnimalLion.CurrentLane == LevelManager.eThreeLane.BottomLane)
{
Debug.Log(“bottom lane lion”);
CreateLionIndicationIcon(AnimalLion);
//LionAnimal = true;
}
}

private void CreateLionIndicationIcon(Lion animal)
{
LionIconPosition.y = animal.transform.position.y;
LionIconPosition.z = animal.transform.position.z;
LionIconPosition.x = transform.position.x + _camWidth;
_indicationlionprefab = (GameObject)Instantiate(IndicationLionPrefab, LionIconPosition, animal.transform.rotation);
}

private void CalcRayOrigin()
{
_rayCastTopLeft = _transform.position + new Vector3 (-25, _transform.position.y + _localeScale.y);
_rayCastBottomLeft = _transform.position + new Vector3 (-25, _transform.position.y – _localeScale.y);
}
}

Leave a Reply