CharacterMovement.cs 2.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterMovement : MonoBehaviour {


    private float groundLevel;
    private float colliderBottom;
10
11
12

    private GameObject player;
    private BoxCollider2D thisCollider;
13
    private CharacterInteractions interactions;
14
15
    private ContactFilter2D filter = new ContactFilter2D();
    private bool facingRight = true;
16
    private bool wandering = true;
17
18
19
    // Use this for initialization
    void Start () {
        thisCollider = GetComponent<BoxCollider2D>();
20
        groundLevel = DaytimeManager.groundLevel;
21
        player = GameObject.FindWithTag("Player");
22
        interactions = GetComponent<CharacterInteractions>();
23
24
        filter.SetLayerMask(LayerMask.GetMask("Player"));
    }
25
26
27
28
29

    // Update is called once per frame
    void Update()
    {
        //walk around randomly. if character enters their hitbox stop and face character.
30
        keepOnGround();
31
        checkNearbyPlayers();
32
33
34


    }
35

36
37
38
39
40
41
    void findColliderBottom(){
        float colliderPositionY = transform.position.y + thisCollider.offset.y;
        colliderBottom = colliderPositionY - (thisCollider.size.y / 2);
    }

    void keepOnGround(){
42
43
44
45
46
47
48
        findColliderBottom();
        //if feet(bottom of collider) on ground or bellow
        if (colliderBottom <= groundLevel)
        {
            //reset position to the ground;
            Vector2 newPos = transform.position;
            newPos.y += (groundLevel - colliderBottom);
49
            transform.position = newPos;
50
51
52
        }
    }

53
    void checkNearbyPlayers(){
54
55
56
57
        CapsuleCollider2D playerCol = player.GetComponent<CapsuleCollider2D>();
        Collider2D[] foundPlayers = new Collider2D[1];
        if (thisCollider.OverlapCollider(filter, foundPlayers) > 0)
        {
58
            Wandering = false;
59
60
            lookAtPlayer();
        }else{
61
62
63
64
65
66
67
68
69
70
71
72
73
74
            Wandering = true;
        }
    }
    // accessor for variable wandering(does extra things when its changed)
    public bool Wandering{
        set{
            if(wandering != value){
                //transition interactable
                interactions.setInteractable(!value);
            }
            wandering = value;
        }
        get {
            return wandering;
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        }
    }

    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;
98
99
    }
}