Guest User

Untitled

a guest
May 23rd, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [RequireComponent (typeof(Rigidbody))]
  5. public class RunningFood : Critter {
  6.  
  7.     public GameObject foodToInstantiate;
  8.     List<GameObject> critterFlock = new List<GameObject>();
  9.     int amountOfFoods = 9;
  10.     RaycastHit collisionRay;
  11.     public LayerMask critterLayer;
  12.     float percentage;
  13.     float idleTimer;
  14.     float idleMax = 3f;
  15.     float maxRayDistance = 3f;
  16.     float moveSpeed = 2f;
  17.     HelperFunctions helperFunctions;
  18.     Transform targetPos;
  19.     Rigidbody rigidBody;
  20.     Vector3 oldPosition;
  21.  
  22.     public void Init()
  23.     {
  24.         rigidBody = GetComponent<Rigidbody>();
  25.         targetPos = GameObject.FindGameObjectWithTag("Player").transform;
  26.         state = CritterState.setDirection;
  27.         helperFunctions = GameObject.Find("Main").GetComponent<HelperFunctions>();
  28.     }
  29.  
  30.     public override void CritterUpdate(ref PlayerData _playerData)
  31.     {
  32.         switch(base.state)
  33.         {
  34.             case CritterState.setDirection:
  35.                 if (SetDirection())
  36.                     state = CritterState.walk;
  37.                 break;
  38.  
  39.             case CritterState.walk:
  40.                 if (Walk())
  41.                     state = CritterState.idle;
  42.                 break;
  43.  
  44.             case CritterState.idle:
  45.                 idleTimer += Time.deltaTime;
  46.                 if (idleTimer > idleMax)
  47.                 {
  48.                     idleTimer = 0;
  49.                     state = CritterState.setDirection;
  50.                 }
  51.                 break;
  52.         }
  53.     }
  54.  
  55.     bool SetDirection()
  56.     {
  57.         //ta en random forward
  58.         float randomAngle = Random.Range(0, 359);
  59.         transform.rotation = Quaternion.AngleAxis(randomAngle, new Vector3(0, 1, 0));
  60.  
  61.         if (Physics.Raycast(transform.position + new Vector3(0, 1, 0), transform.forward, out collisionRay, maxRayDistance))
  62.         {
  63.             if (!collisionRay.collider.isTrigger)
  64.             {
  65.                 oldPosition = transform.position;
  66.                 return true;
  67.             }
  68.             else
  69.                 return false;
  70.         }
  71.         else
  72.         {
  73.             oldPosition = transform.position;
  74.             return true;
  75.         }
  76.         //skjuta en ray i fårvard
  77.         //if träffar, ta annan random fårward, skjut i fårvard
  78.         //om ej träffar, spring 1 - 2 units i fårvard
  79.     }
  80.  
  81.     bool Walk()
  82.     {
  83.         float tempDist = Vector3.Distance(oldPosition, transform.position);
  84.         if (tempDist < maxRayDistance)
  85.         {
  86.             rigidBody.MovePosition(transform.position + transform.forward * Time.deltaTime * moveSpeed);
  87.             return false;
  88.         }
  89.         else
  90.             return true;
  91.     }
  92.  
  93.     void FriendCheck()
  94.     {
  95.  
  96.     }
  97.  
  98.     void Idle()
  99.     {
  100.         //Play idleAnimation
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment