Revisiting Unity and C#
What’s new
I added three enemies/mobs one that follows the player slowly, I added a homing missile, and an enemy that just follows the outline of the map.
Slow Follow
For the slow follow it’s just a classic code using the distance calculation and the Vector2.MoveTowards function.
public float Speed;
public float radius;
public bool FoundPlayer;
private GameObject player;
public float distance;
void Start(){
try{
player = GameObject.FindWithTag("Player");
FoundPlayer = true;
}
catch{
FoundPlayer = false;
}
}
void Update(){
if(FoundPlayer){
distance = Vector2.Distance(transform.position, player.transform.position);
Vector2 direction = player.transform.position - transform.position;
direction.Normalize();
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
if(distance > radius){
transform.position = Vector2.MoveTowards(this.transform.position, player.transform.position, Speed * Time.deltaTime);
transform.rotation = Quaternion.Euler(Vector3.forward * angle);
}
}
}
Path Finder
For this mob I kinda feel it’s just a scam, If I had more time I’d look into linear math to make this look more professional. I’m just using raycasts to see which wall am I on, and I just base movement of that.
public Transform up;
public Transform down;
public Transform left;
public Transform right;
public Transform TopRight;
public Transform TopLeft;
public Transform BottomRight;
public Transform BottomLeft;
private Rigidbody2D rb;
public float length;
public float Speed;
public LayerMask ground;
void Start(){
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate(){
RaycastHit2D UpHit = Physics2D.Raycast( up.transform.position, Vector2.up, length, ground );
RaycastHit2D DownHit = Physics2D.Raycast( down.transform.position, Vector2.down, length, ground);
RaycastHit2D RightHit = Physics2D.Raycast( right.transform.position, Vector2.right, length, ground);
RaycastHit2D LeftHit = Physics2D.Raycast( left.transform.position, Vector2.left, length, ground);
RaycastHit2D BottomRightCorner = Physics2D.Raycast( BottomRight.transform.position, new Vector2(1,-1) , length*5, ground);
RaycastHit2D BottomLeftCorner = Physics2D.Raycast( BottomLeft.transform.position, new Vector2(-1,-1) , length*5, ground);
RaycastHit2D TopRightCorner = Physics2D.Raycast( TopRight.transform.position, new Vector2(1,1) , length*5, ground);
RaycastHit2D TopLeftCorner = Physics2D.Raycast( TopLeft.transform.position, new Vector2(-1,1) , length*5, ground);
if(UpHit){
rb.velocity = Vector2.left * Speed;
}
if(DownHit){
rb.velocity = Vector2.right * Speed;
}
if(RightHit){
rb.velocity = Vector2.up * Speed;
}
if(LeftHit){
rb.velocity = Vector2.down * Speed;
}
if(UpHit && RightHit){
rb.velocity = Vector2.left * Speed;
}
if(DownHit && LeftHit){
rb.velocity = Vector2.right * Speed;
}
if(BottomRightCorner && !UpHit && !DownHit && !RightHit && !LeftHit){
rb.velocity = Vector2.right * Speed;
}
if(BottomLeftCorner && !UpHit && !DownHit && !RightHit && !LeftHit){
rb.velocity = Vector2.down * Speed;
}
if(TopLeftCorner && !UpHit && !DownHit && !RightHit && !LeftHit){
rb.velocity = Vector2.left * Speed;
}
}
Homing Missile
Foe the missile it’s important that you add a Rigidbody2D, for the other I’m pretty sure you’d be ok with just colliders. Now this one I just straight up got the code from brackeys, awesome channel.
public float Speed = 5f;
public float rotateSpeed = 200f;
private GameObject player;
private Rigidbody2D rb;
void Start(){
rb = GetComponent<Rigidbody2D>();
player = GameObject.FindWithTag("Player");
}
// Update is called once per frame
void FixedUpdate(){
Vector2 direction = (Vector2)player.transform.position - rb.position;
direction.Normalize();
float rotate = Vector3.Cross(direction, transform.up).z;
rb.angularVelocity = -rotate * rotateSpeed;
rb.velocity = transform.up * Speed;
}