I'm making 2d game and i want my enemy to turn around when he's at the end of platform
For this i was doing vector2 linecast position
I dont know how to make it in the corner, not from the centre of the enemy which is made of different parts of the body
![alt text][1]
[1]: /storage/temp/87340-screen-shot-2017-02-04-at-101332.png
Here it is a script for it
Enemy shaking and spin around for some how
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour
{
public LayerMask enemyMask;
public float speed = 1;
Rigidbody2D myBody;
Transform myTrans;
float myWidth;
void Start ()
{
myTrans = this.transform;
myBody = this.GetComponent();
myWidth = this.GetComponent().bounds.extents.x;
}
void FixedUpdate ()
{
//Check to see if there's ground in front of us before moving forward
Vector2 lineCastPos = myTrans.position - myTrans.right * myWidth;
Debug.DrawLine(lineCastPos, lineCastPos + Vector2.down);
bool isGrounded = Physics2D.Linecast (lineCastPos, lineCastPos + Vector2.down, enemyMask);
// If there's no ground turn around
if (!isGrounded)
{
Vector3 currRot = myTrans.eulerAngles;
currRot.y += 180;
myTrans.eulerAngles = currRot;
}
//Always move forward
Vector2 myVel = myBody.velocity;
myVel.x = -myTrans.right * speed;
myBody.velocity = myVel;
}
}
↧