Beating-With-Sticks: Improved AI "patrolling" state and AI runs away

from player once in certain distance.
Made a second Movement script which allows the player to throw rocks.
Added/changed some comments for suggestions, things to do, things that
are done and bugs that need fixing.
This commit is contained in:
Santi 2024-08-12 23:52:18 +01:00
parent d00afd6a48
commit 857973aff7
4 changed files with 454 additions and 116 deletions

View File

@ -217,6 +217,103 @@ Transform:
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 0 m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
--- !u!1 &1158947384
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1158947385}
- component: {fileID: 1158947388}
- component: {fileID: 1158947387}
- component: {fileID: 1158947386}
m_Layer: 3
m_Name: Cube
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1158947385
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1158947384}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: -0.022, y: 0.54, z: 0.584}
m_LocalScale: {x: 0.40011483, y: 0.2110746, z: 0.3931717}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 1423233324}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!65 &1158947386
BoxCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1158947384}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
serializedVersion: 2
m_Size: {x: 1, y: 1, z: 1}
m_Center: {x: 0, y: 0, z: 0}
--- !u!23 &1158947387
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1158947384}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 2100000, guid: c905fc84b924f604a824c53fb10bf855, type: 2}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &1158947388
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1158947384}
m_Mesh: {fileID: 10202, guid: 0000000000000000e000000000000000, type: 0}
--- !u!1 &1423233316 --- !u!1 &1423233316
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -229,8 +326,8 @@ GameObject:
- component: {fileID: 1423233323} - component: {fileID: 1423233323}
- component: {fileID: 1423233322} - component: {fileID: 1423233322}
- component: {fileID: 1423233321} - component: {fileID: 1423233321}
- component: {fileID: 1423233320}
- component: {fileID: 1423233319} - component: {fileID: 1423233319}
- component: {fileID: 1423233325}
- component: {fileID: 1423233318} - component: {fileID: 1423233318}
- component: {fileID: 1423233317} - component: {fileID: 1423233317}
m_Layer: 3 m_Layer: 3
@ -296,31 +393,6 @@ MonoBehaviour:
cooldownSeconds: 1 cooldownSeconds: 1
cooldownActive: 0 cooldownActive: 0
interactKey: 102 interactKey: 102
--- !u!114 &1423233320
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1423233316}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 93424cdca660b854faec02bc3325bc31, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 5
turnSpeed: 720
sprintSpeed: 10
jumpForce: 2.5
gravity: 9.8
throwForce: {fileID: 1423233319}
forwardsKey: 119
backwardsKey: 115
leftKey: 97
rightKey: 101
sprintKey: 304
jumpKey: 32
hammer: 0
--- !u!136 &1423233321 --- !u!136 &1423233321
CapsuleCollider: CapsuleCollider:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -396,10 +468,71 @@ Transform:
m_LocalPosition: {x: -8.31, y: 2, z: 6.61} m_LocalPosition: {x: -8.31, y: 2, z: 6.61}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: [] m_Children:
- {fileID: 1158947385}
m_Father: {fileID: 0} m_Father: {fileID: 0}
m_RootOrder: 4 m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1423233325
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1423233316}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: f1361d39a2e401545a42006ca4e951ed, type: 3}
m_Name:
m_EditorClassIdentifier:
speed: 5
turnSpeed: 720
sprintSpeed: 10
jumpForce: 2.5
gravity: 9.8
throwForce: {fileID: 1423233319}
forwardsKey: 119
backwardsKey: 115
leftKey: 97
rightKey: 101
sprintKey: 304
jumpKey: 32
rockKey: 324
hammer: 0
rock: 1
rockObject: {fileID: 1874423841}
spawnPoint: {fileID: 1158947384}
--- !u!1 &1477235389
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1477235390}
m_Layer: 0
m_Name: CentrePoint
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &1477235390
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1477235389}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 5
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1664811009 --- !u!1 &1664811009
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -534,12 +667,10 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b478d0ab1ff19cb4cb9e6161ea2a14d5, type: 3} m_Script: {fileID: 11500000, guid: b478d0ab1ff19cb4cb9e6161ea2a14d5, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
player: {fileID: 0}
agent: {fileID: 0} agent: {fileID: 0}
walkPoint: {x: 0, y: 0, z: 0} range: 10
walkPointSet: 0 centrePoint: {fileID: 1477235390}
walkPointRange: 7 isHit: 0
playerInSightRange: 0
--- !u!135 &1664811016 --- !u!135 &1664811016
SphereCollider: SphereCollider:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -553,6 +684,121 @@ SphereCollider:
serializedVersion: 2 serializedVersion: 2
m_Radius: 9 m_Radius: 9
m_Center: {x: 0, y: 0, z: 0} m_Center: {x: 0, y: 0, z: 0}
--- !u!1 &1874423841
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1874423845}
- component: {fileID: 1874423844}
- component: {fileID: 1874423843}
- component: {fileID: 1874423842}
- component: {fileID: 1874423846}
m_Layer: 0
m_Name: Cylinder
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!136 &1874423842
CapsuleCollider:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1874423841}
m_Material: {fileID: 0}
m_IsTrigger: 0
m_Enabled: 1
m_Radius: 0.5000001
m_Height: 2
m_Direction: 1
m_Center: {x: 0.000000059604645, y: 0, z: -0.00000008940697}
--- !u!23 &1874423843
MeshRenderer:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1874423841}
m_Enabled: 1
m_CastShadows: 1
m_ReceiveShadows: 1
m_DynamicOccludee: 1
m_StaticShadowCaster: 0
m_MotionVectors: 1
m_LightProbeUsage: 1
m_ReflectionProbeUsage: 1
m_RayTracingMode: 2
m_RayTraceProcedural: 0
m_RenderingLayerMask: 1
m_RendererPriority: 0
m_Materials:
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
m_StaticBatchInfo:
firstSubMesh: 0
subMeshCount: 0
m_StaticBatchRoot: {fileID: 0}
m_ProbeAnchor: {fileID: 0}
m_LightProbeVolumeOverride: {fileID: 0}
m_ScaleInLightmap: 1
m_ReceiveGI: 1
m_PreserveUVs: 0
m_IgnoreNormalsForChartDetection: 0
m_ImportantGI: 0
m_StitchLightmapSeams: 1
m_SelectedEditorRenderState: 3
m_MinimumChartSize: 4
m_AutoUVMaxDistance: 0.5
m_AutoUVMaxAngle: 89
m_LightmapParameters: {fileID: 0}
m_SortingLayerID: 0
m_SortingLayer: 0
m_SortingOrder: 0
m_AdditionalVertexStreams: {fileID: 0}
--- !u!33 &1874423844
MeshFilter:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1874423841}
m_Mesh: {fileID: 10206, guid: 0000000000000000e000000000000000, type: 0}
--- !u!4 &1874423845
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1874423841}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 18.566313, y: -32.571102, z: -2.637422}
m_LocalScale: {x: 0.15, y: 0.15, z: 0.15}
m_ConstrainProportionsScale: 1
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!54 &1874423846
Rigidbody:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1874423841}
serializedVersion: 2
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_UseGravity: 1
m_IsKinematic: 0
m_Interpolate: 0
m_Constraints: 0
m_CollisionDetection: 0
--- !u!1 &1947815994 --- !u!1 &1947815994
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -781,7 +1027,7 @@ Camera:
height: 1 height: 1
near clip plane: 0.3 near clip plane: 0.3
far clip plane: 1000 far clip plane: 1000
field of view: 36 field of view: 56
orthographic: 0 orthographic: 0
orthographic size: 5 orthographic size: 5
m_Depth: -1 m_Depth: -1

View File

@ -1,5 +1,6 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
@ -11,103 +12,85 @@ using UnityEngine.AI;
//TODO AI Script for the Stick Beating Game //TODO AI Script for the Stick Beating Game
//* AI for enemies to run away from the character once in range //* AI for enemies to run away from the character once in range
//* AI should change direction every few seconds to make it harder for the player to hit them //* AI should change direction every few seconds to make it harder for the player to hit them
//TODO AI should not go into a direction within the player's range //* AI should not go into a direction within the player's range
//* If player is not in range, AI should move randomly //* If player is not in range, AI should move randomly
//TODO Slow down AI when hit by player rock
//TODO AI dies if hit by player stick
//? Enemy AI gets stuck in corners - maybe have AI move in a random direction if it gets stuck
public class AIScript : MonoBehaviour public class AIScript : MonoBehaviour
{ {
private bool runningCoroutine = false;
public GameObject player;
public NavMeshAgent agent; public NavMeshAgent agent;
public float range;
//* Patroling public Transform centrePoint;
public Vector3 walkPoint; private bool playerInRange = false;
public bool walkPointSet; public bool isHit = false;
public float walkPointRange; private void Awake()
//* States
public bool playerInSightRange = false;
void Start()
{ {
player = GameObject.Find("Capsule");
agent = GetComponent<NavMeshAgent>(); agent = GetComponent<NavMeshAgent>();
} }
// Update is called once per frame private void Update()
void Update()
{ {
if(!playerInSightRange) Patroling(); if(agent.remainingDistance <= agent.stoppingDistance && !playerInRange)
if(playerInSightRange) Running(); {
Vector3 point;
if(RandomPoint(centrePoint.position, range, out point))
{
Debug.DrawRay(point, Vector3.up, Color.blue, 1.0f);
agent.SetDestination(point);
}
}
else if(playerInRange && !isHit)
{
agent.speed = 7.0f;
RunAway();
} }
private void Patroling() if(isHit)
{
agent.speed = 1.75f;
}
else
{ {
agent.speed = 3.5f; agent.speed = 3.5f;
if(!walkPointSet) SetWalkPointPatroling(); }
if(walkPointSet)
agent.SetDestination(walkPoint);
Vector3 distanceToWalkPoint = transform.position - walkPoint;
if(distanceToWalkPoint.magnitude < 1f)
walkPointSet = false;
} }
private void SetWalkPointPatroling() void RunAway()
{ {
float randomZ = Random.Range(-walkPointRange, walkPointRange); Vector3 direction = transform.position - centrePoint.position;
float randomX = Random.Range(-walkPointRange, walkPointRange); Vector3 runTo = transform.position + direction.normalized;
NavMeshHit hit;
walkPoint = new Vector3(randomX, transform.position.y, randomZ); NavMesh.SamplePosition(runTo, out hit, 5.0f, NavMesh.AllAreas);
walkPointSet = true; agent.SetDestination(hit.position);
} }
private void Running()
bool RandomPoint(Vector3 center, float range, out Vector3 result)
{ {
agent.speed = 6f; Vector3 randompoint = center + Random.insideUnitSphere * range;
if(!walkPointSet) SetWalkPointRunning(); NavMeshHit hit;
if (NavMesh.SamplePosition(randompoint, out hit, 1.0f, NavMesh.AllAreas))
if(walkPointSet)
agent.SetDestination(walkPoint);
if(!runningCoroutine) StartCoroutine(WaitTime());
Vector3 distanceToWalkPoint = transform.position - walkPoint;
if(distanceToWalkPoint.magnitude < 1f)
walkPointSet = false;
}
private void SetWalkPointRunning()
{ {
float randomZ = Random.Range(-walkPointRange, walkPointRange); result = hit.position;
float randomX = Random.Range(-walkPointRange, walkPointRange); return true;
}
walkPoint = new Vector3(randomX, transform.position.y, randomZ); result = Vector3.zero;
return false;
walkPointSet = true;
} }
IEnumerator WaitTime() private void OnTriggerEnter(Collider other)
{ {
runningCoroutine = true; if(other.tag == "Player")
yield return new WaitForSeconds(Random.Range(2, 5));
walkPointSet = false;
runningCoroutine = false;
}
private void OnTriggerEnter(Collider other) {
if(other.gameObject == player)
{ {
playerInSightRange = true; playerInRange = true;
} }
} }
private void OnTriggerExit(Collider other) { private void OnTriggerExit(Collider other)
if(other.gameObject == player)
{ {
playerInSightRange = false; if(other.tag == "Player")
{
playerInRange = false;
} }
} }
} }

View File

@ -0,0 +1,98 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//* General Comments or Finished Tasks
//TODO Tasks left to be done for game
//! Bugs or Issues
//? Questions or Suggestions
//! Aftering hitting down with hammer, turning stops working in what direction the player tries to move first in
//! As with throwing objects, the rock sometimes goes in random directions and random speeds
//*TODO Playing StickBeating Game, player should be able to throw rocks every few seconds
//? Soft auto lock on enemies when throwing rocks
public class MovementStickBeating : MonoBehaviour
{
public float speed = 7.0f;
public float turnSpeed;
public float sprintSpeed = 10.0f;
public float jumpForce;
public float gravity = 9.8f;
// public PickUpObject throwForce;
private CharacterController controller;
private Vector3 moveDirection = Vector3.zero;
[Header("Keybinds")]
public KeyCode forwardsKey = KeyCode.W;
public KeyCode backwardsKey = KeyCode.S;
public KeyCode leftKey = KeyCode.A;
public KeyCode rightKey = KeyCode.D;
public KeyCode sprintKey = KeyCode.LeftShift;
public KeyCode jumpKey = KeyCode.Space;
public KeyCode rockKey = KeyCode.Mouse1;
public bool hammer = false;
public bool rock = false;
public GameObject rockObject;
public GameObject spawnPoint;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
float forwards = Convert.ToSingle(Input.GetKey(forwardsKey));
float backwards = Convert.ToSingle(Input.GetKey(backwardsKey));
float left = Convert.ToSingle(Input.GetKey(leftKey));
float right = Convert.ToSingle(Input.GetKey(rightKey));
if (controller.isGrounded && !hammer)
{
moveDirection = new Vector3(right - left, 0, forwards - backwards);
//* Change the direction the player is facing based on the direction they are moving
if (right - left != 0 || forwards - backwards != 0)
{
Quaternion ToRotation = Quaternion.LookRotation(moveDirection);
transform.rotation = Quaternion.RotateTowards(transform.rotation, ToRotation, turnSpeed * Time.deltaTime);
}
moveDirection *= speed;
jumpForce = 2.5f;
if (Input.GetKeyDown(jumpKey))
{
moveDirection.y = jumpForce;
}
//If player is moving diagonally, reduce speed
if (forwards != 0 && right != 0 || forwards != 0 && left != 0 || backwards != 0 && right != 0 || backwards != 0 && left != 0)
{
moveDirection *= 0.7f;
}
else
{
moveDirection *= 1.0f;
}
}
if(hammer)
{
moveDirection = Vector3.zero;
}
if(rock)
{
if(Input.GetKeyDown(rockKey))
{
GameObject throwing = Instantiate(rockObject, spawnPoint.transform.position, spawnPoint.transform.rotation);
throwing.GetComponent<Rigidbody>().AddForce(transform.forward * 8.5f, ForceMode.Impulse);
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f1361d39a2e401545a42006ca4e951ed
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: