Home || Previous || Next

Codes for Workshop SharkMan Part 1, Part 2, Part 3,

 Test

using UnityEngine; using System.Collections;

public class Test : MonoBehaviour {

public Animation anim;

// Use this for initialization void Start () {

anim = GameObject.Find(“SharkmanFinal”).GetComponent <Animation>();

}

void TestAnim(){

//any forward input from player we move aniamtion to Swim

if((Input.GetAxis(“Vertical”) *1 * Time.deltaTime) > 0) { anim.Play(“Swim”); transform.Translate(0, 0, Input.GetAxis(“Vertical”) * 1 );

} else { anim.Play(“Idle”); } }

// Update is called once per frame void Update () {

TestAnim();

} }

Movement Script

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Movement : MonoBehaviour {

public NavMeshAgent nAgent; public Vector3 desiredPosition; public List<Vector3> waypoints; public int counter; public string currentState;

// Use this for initialization void Start () {

nAgent = GetComponent<NavMeshAgent>(); waypoints = new List<Vector3>();

waypoints.Add(GameObject.Find(“WayPointOne”).transform.position); waypoints.Add(GameObject.Find(“WayPointTwo”).transform.position); waypoints.Add(GameObject.Find(“WayPointThree”).transform.position); waypoints.Add(GameObject.Find(“WayPointFour”).transform.position); counter = 0; desiredPosition = waypoints[counter];

}

void Scan() { Vector3 sharkmanPos = GameObject.Find(“SharkmanFinal”).transform.position; Debug.DrawRay(new Vector3 (sharkmanPos.x, sharkmanPos.y +1, sharkmanPos.z), transform.forward * 6 , Color.red); RaycastHit forwardRay;

if(Physics.Raycast(new Vector3(sharkmanPos.x, sharkmanPos.y +1, sharkmanPos.z), transform.forward *6, out forwardRay, 6)){ if(forwardRay.collider.tag == “Fish”){ if(GetComponent<SharkStats>().hunger >= 70){ GetComponent<StateMachine>().state = “HUNT”; } } }

} void Patrol() {

if((Mathf.Abs(transform.position.x – waypoints[counter].x) < 1) && (Mathf.Abs(transform.position.z – waypoints[counter].z) < 1)) { if(GetComponent<SharkStats>().energy >0){ GetComponent<SharkStats>().energy -= 1; }

counter += 1; if(counter == waypoints.Count) { counter = 0; } desiredPosition = waypoints[counter]; } }

// Update is called once per frame void Update () {

currentState = GetComponent<StateMachine>().state;

Scan();

if(currentState == “PATROL”) {

Patrol(); }

nAgent.SetDestination(desiredPosition);

} }

 

StateMachine

using UnityEngine; using System.Collections;

public class StateMachine : MonoBehaviour {

public string state;

// Use this for initialization void Start () {

state = “PATROL”;

}

// Update is called once per frame void Update () {

} }

Shark Stats

using UnityEngine; using System.Collections;

public class SharkStats : MonoBehaviour {

public float health; public float rage; public float hunger; public float energy;

// Use this for initialization void Start () {

health = 100f; rage = 0f; hunger = 50f; energy = 100f;

}

void increaseHunger(){

if(hunger < 100){

hunger = 100-energy; } }

void increaseRage(){

rage = hunger * 0.75f; }

// Update is called once per frame void Update () {

increaseHunger(); increaseRage();

} }

SharkmanAnimation

using UnityEngine; using System.Collections;

public class SharkmanAnimation : MonoBehaviour {

public string currentState; public Animation anim;

// Use this for initialization void Start () {

anim = GameObject.Find(“SharkmanFinal”).GetComponent<Animation>();

}

void ChangeAnimation (){

if (currentState ==”PATROL”) { anim.Play(“Swim”); } else if(currentState == “IDLE”) { anim.Play(“Idle”); }

}

// Update is called once per frame void Update () {

currentState = GetComponent<StateMachine>().state; ChangeAnimation(); } }

 

Home || Previous || Next

Advertisements