Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- [RequireComponent (typeof(Rigidbody))]
- public class RunningFood : Critter {
- public GameObject foodToInstantiate;
- List<GameObject> critterFlock = new List<GameObject>();
- int amountOfFoods = 9;
- RaycastHit collisionRay;
- public LayerMask critterLayer;
- float percentage;
- float idleTimer;
- float idleMax = 3f;
- float maxRayDistance = 3f;
- float moveSpeed = 2f;
- HelperFunctions helperFunctions;
- Transform targetPos;
- Rigidbody rigidBody;
- Vector3 oldPosition;
- public void Init()
- {
- rigidBody = GetComponent<Rigidbody>();
- targetPos = GameObject.FindGameObjectWithTag("Player").transform;
- state = CritterState.setDirection;
- helperFunctions = GameObject.Find("Main").GetComponent<HelperFunctions>();
- }
- public override void CritterUpdate(ref PlayerData _playerData)
- {
- switch(base.state)
- {
- case CritterState.setDirection:
- if (SetDirection())
- state = CritterState.walk;
- break;
- case CritterState.walk:
- if (Walk())
- state = CritterState.idle;
- break;
- case CritterState.idle:
- idleTimer += Time.deltaTime;
- if (idleTimer > idleMax)
- {
- idleTimer = 0;
- state = CritterState.setDirection;
- }
- break;
- }
- }
- bool SetDirection()
- {
- //ta en random forward
- float randomAngle = Random.Range(0, 359);
- transform.rotation = Quaternion.AngleAxis(randomAngle, new Vector3(0, 1, 0));
- if (Physics.Raycast(transform.position + new Vector3(0, 1, 0), transform.forward, out collisionRay, maxRayDistance))
- {
- if (!collisionRay.collider.isTrigger)
- {
- oldPosition = transform.position;
- return true;
- }
- else
- return false;
- }
- else
- {
- oldPosition = transform.position;
- return true;
- }
- //skjuta en ray i fårvard
- //if träffar, ta annan random fårward, skjut i fårvard
- //om ej träffar, spring 1 - 2 units i fårvard
- }
- bool Walk()
- {
- float tempDist = Vector3.Distance(oldPosition, transform.position);
- if (tempDist < maxRayDistance)
- {
- rigidBody.MovePosition(transform.position + transform.forward * Time.deltaTime * moveSpeed);
- return false;
- }
- else
- return true;
- }
- void FriendCheck()
- {
- }
- void Idle()
- {
- //Play idleAnimation
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment