Commit 3f9e2707 authored by mmaluschnig's avatar mmaluschnig
Browse files

Characters now turn to face the player when the player gets close

parent 59da1156
......@@ -320,7 +320,7 @@ Prefab:
objectReference: {fileID: 0}
- target: {fileID: 4057871787717216, guid: a71e7603ca1b4481291e1bfd11bf57d6, type: 2}
propertyPath: m_LocalPosition.y
value: 0
value: -1.25
objectReference: {fileID: 0}
- target: {fileID: 4057871787717216, guid: a71e7603ca1b4481291e1bfd11bf57d6, type: 2}
propertyPath: m_LocalPosition.z
......@@ -408,6 +408,10 @@ Prefab:
propertyPath: sprintSpeed
value: 8
objectReference: {fileID: 0}
- target: {fileID: 1015016174570030, guid: 72a0d040ea21c4eebab30d68d3311671, type: 2}
propertyPath: m_Layer
value: 9
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 72a0d040ea21c4eebab30d68d3311671, type: 2}
m_IsPrefabAsset: 0
......@@ -529,7 +533,7 @@ Prefab:
objectReference: {fileID: 0}
- target: {fileID: 4662323854503996, guid: c26ff785dae05414ab438c90c1da3022, type: 2}
propertyPath: m_LocalPosition.y
value: -0.1269083
value: -1.01
objectReference: {fileID: 0}
- target: {fileID: 4662323854503996, guid: c26ff785dae05414ab438c90c1da3022, type: 2}
propertyPath: m_LocalPosition.z
......@@ -646,7 +650,7 @@ Prefab:
objectReference: {fileID: 0}
- target: {fileID: 4490612100453362, guid: 49c8478baca8d47be993db0c6427da2e, type: 2}
propertyPath: m_LocalPosition.y
value: 0
value: -1.14
objectReference: {fileID: 0}
- target: {fileID: 4490612100453362, guid: 49c8478baca8d47be993db0c6427da2e, type: 2}
propertyPath: m_LocalPosition.z
......
......@@ -7,16 +7,36 @@ public class CharacterMovement : MonoBehaviour {
private float groundLevel;
private float colliderBottom;
// Use this for initialization
void Start () {
private GameObject player;
private BoxCollider2D thisCollider;
private ContactFilter2D filter = new ContactFilter2D();
private bool facingRight = true;
private bool wandering = true;
// Use this for initialization
void Start () {
thisCollider = GetComponent<BoxCollider2D>();
groundLevel = DaytimeManager.groundLevel;
}
player = GameObject.FindWithTag("Player");
filter.SetLayerMask(LayerMask.GetMask("Player"));
}
// Update is called once per frame
void Update()
{
//walk around randomly. if character enters their hitbox stop and face character.
keepOnGround();
checkNearByPlayers();
}
void findColliderBottom(){
float colliderPositionY = transform.position.y + thisCollider.offset.y;
colliderBottom = colliderPositionY - (thisCollider.size.y / 2);
}
void keepOnGround(){
findColliderBottom();
//if feet(bottom of collider) on ground or bellow
if (colliderBottom <= groundLevel)
......@@ -24,13 +44,41 @@ public class CharacterMovement : MonoBehaviour {
//reset position to the ground;
Vector2 newPos = transform.position;
newPos.y += (groundLevel - colliderBottom);
transform.position = newPos;
transform.position = newPos;
}
}
void findColliderBottom(){
BoxCollider2D thisCollider = GetComponent<BoxCollider2D>();
float colliderPositionY = transform.position.y + thisCollider.offset.y;
colliderBottom = colliderPositionY - (thisCollider.size.y / 2);
void checkNearByPlayers(){
CapsuleCollider2D playerCol = player.GetComponent<CapsuleCollider2D>();
Collider2D[] foundPlayers = new Collider2D[1];
if (thisCollider.OverlapCollider(filter, foundPlayers) > 0)
{
wandering = false;
lookAtPlayer();
}else{
wandering = true;
}
}
void lookAtPlayer(){
//player on right
if ((transform.position.x - player.transform.position.x) < 0 && !facingRight)
{
flipDirection();
}
//player on left
else if ((transform.position.x - player.transform.position.x) > 0 && facingRight)
{
flipDirection();
}
}
void flipDirection()
{
//flip rotation without having to mess with quaternions
facingRight = !facingRight;
Vector2 newScale = transform.localScale;
newScale.x *= -1;
transform.localScale = newScale;
}
}
......@@ -17,7 +17,7 @@ TagManager:
-
-
- Key
-
- Player
-
-
-
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment