- Home /
how to create color targeting script for a 2d sidescroll
I would like to create a script that targets a 2d sprite "enemy" and changes their color to red (slightly opaque red if possible) when you hit tab. When you hit tab again It will deselect the target turning the sprite image back to its original state (essentially no color) I have this code from a 3d tutorial hoping the transition would work. But it does not. I only get the script to cycle the enemy tags but never changes the color. I'm very new to coding, and any help would be FANTASTIC! HELP! hahah.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Targetting : MonoBehaviour {
public List<Transform> targets;
public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
myTransform = transform;
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy)
{
targets.Add(enemy);
}
private void SortTargetsByDistance()
{
targets.Sort(delegate(Transform t1,Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
}
private void TargetEnemy()
{
if(selectedTarget == null)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
else
{
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count -1)
{
index++;
}
else
{
index = 0;
}
selectedTarget = targets[index];
}
}
private void SelectTarget()
{
selectedTarget.GetComponent<SpriteRenderer>().color = Color.red;
}
private void DeselectTarget()
{
selectedTarget.GetComponent<SpriteRenderer>().color = Color.blue;
selectedTarget = null;
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
}
Answer by Maerig · Jun 02, 2014 at 09:04 AM
You have a SelectTarget
and a DeselectTarget
method, now you just need to use them.
Answer by aceleeon5 · Jun 03, 2014 at 03:42 AM
Im sorry, I don't understand what that means. The select and deselect work, as in I see in the inspector that the toggle is working. But the color change of the enemy sprite does not toggle to the assigned color in the code.
Just call them in TargetEnemy
private void TargetEnemy()
{
if(selectedTarget == null)
{
SortTargetsByDistance();
selectedTarget = targets[0];
SelectTarget();
}
else
{
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count -1)
{
index++;
}
else
{
index = 0;
}
DeselectTarget();
selectedTarget = targets[index];
SelectTarget();
}
}
THAN$$anonymous$$S! I TOTALLY get what you mean now! your a beast man thanks alot! I'm pretty new to this stuff, so you spelling it out for me really helps me out. have a good one!
Your answer
![](https://koobas.hobune.stream/wayback/20220613150552im_/https://answers.unity.com/themes/thub/images/avi.jpg)