keep a 2d sprite in an area via color
ok so I'm making a top down game of sorts, I have it so the enemy sprites will move straight towards your base and when they collide it takes health away. how ever only the planes/jets should be able to move over the solid ground (green and tan) however I have a ship sprite that should only be able to move in the water (blue). I looked on the majestic google but found nothing or even a hint at doing this. below is the movement code I'm not sure how to make it so the ship is bound in the water. using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour {
private Transform target;
public int moveSpeed;
GameObject go;
private Transform myTransform;
// Use this for initialization
void Awake() {
}
void Start () {
go = GameObject.FindGameObjectWithTag("Base");
myTransform = transform;
target = go.transform;
}
// Update is called once per frame
void Update () {
//Move Towards Target
myTransform.position += (target.position - myTransform.position).normalized * moveSpeed * Time.deltaTime;
if(myTransform.position.x <= go.transform.position.x){
go.GetComponent <Health> ().DecrementHealth(5);
Destroy (this.gameObject);
}
}
}
I would say you should research colliders. If you make the land area have an edge collider, then the ship will hit the "beach" and stop because it hits the collider.
Your answer
Follow this Question
Related Questions
Forcibly halt movement of slow rigidbodies, but not falling ones. (2D) 1 Answer
When using transform.Translate and Rotate It moves weared. 0 Answers
Making 2d grid movement 0 Answers
How do I create a 2D movement script that will cause prefabs to drift around the screen? 0 Answers
Move 2D object forward 1 Answer