create-a-game-project's People
Forkers
wouldyougo praveen1091 viure npk1091 waynemunro notyours180 the-lost-echo evanmalmud stickylabdev bradparks gwydiondesert mengtest mattdiamondd oldamnis ijemin xxxadcmainxxx halfdrizzle jasonthename u3dc imranzaidiedu mrjonyk paulbenge akhomach eshwaranad knut0815 dragonbig devlinh90 panduikap bilter1001 hacksoffical xgamecast unicreators changgyu-oh zhongliche hsf26 mulwasteve enze000 hsw42544869 umar-hyatt ashrafulsbmcbd bryanchance pjlovepb tianqianbao pokkfw xiduoshan ohhokscreate-a-game-project's Issues
Episode 7 of Unity Create a Game Series
I was pretty sure I didn't leave anything out when following the tutorial, but for some reason when I get to the part where you input the damage and health system the enemy seems to kill me in one hit. No matter what my health is, when I start the game it does the normal attacking animation, but then one shots me. The damage and health system works fine when I hit the enemy, but for some reason not the other way around.
What I have so far...
`[RequireComponent (typeof (UnityEngine.AI.NavMeshAgent))]
public class Enemy : LivingEntity {
public enum State {Idle, Chasing, Attacking};
State currentState;
UnityEngine.AI.NavMeshAgent pathfinder;
Transform target;
LivingEntity targetEntity;
Material skinMaterial;
Color originalColor;
float attackDistanceThreshold = .5f;
float timeBetweenAttacks = 1;
float damage = 1;
float nextAttackTime;
float myCollisionRadius;
float targetCollisionRadius;
bool hasTarget;
protected override void Start()
{
base.Start();
pathfinder = GetComponent<UnityEngine.AI.NavMeshAgent>();
skinMaterial = GetComponent<Renderer>().material;
originalColor = skinMaterial.color;
if (GameObject.FindGameObjectWithTag("Player") != null)
{
currentState = State.Chasing;
hasTarget = true;
target = GameObject.FindGameObjectWithTag("Player").transform;
targetEntity = target.GetComponent<LivingEntity>();
targetEntity.OnDeath += OnTargetDeath;
myCollisionRadius = GetComponent<CapsuleCollider>().radius;
targetCollisionRadius = target.GetComponent<CapsuleCollider>().radius;
StartCoroutine(UpdatePath());
}
}
void OnTargetDeath()
{
hasTarget = false;
currentState = State.Idle;
}
void Update () {
if (hasTarget)
{
if (Time.time > nextAttackTime)
{
float sqrDstToTarget = (target.position - transform.position).sqrMagnitude;
if (sqrDstToTarget < Mathf.Pow(attackDistanceThreshold + myCollisionRadius + targetCollisionRadius, 2))
{
nextAttackTime = Time.time + timeBetweenAttacks;
StartCoroutine(Attack());
}
}
}
}
IEnumerator Attack()
{
currentState = State.Attacking;
pathfinder.enabled = false;
Vector3 originalPosition = transform.position;
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 attackPosition = target.position - dirToTarget * (myCollisionRadius);
float attackSpeed = 3;
float percent = 0;
skinMaterial.color = Color.red;
bool hasAppliedDamage = false;
while (percent <= 1)
{
if (percent >= .5f && !hasAppliedDamage) {
hasAppliedDamage = true;
targetEntity.TakeDamage(damage);
}
percent += Time.deltaTime * attackSpeed;
float interpolation = (-Mathf.Pow(percent, 2) + percent) * 4;
transform.position = Vector3.Lerp(originalPosition, attackPosition, interpolation);
yield return null;
}
skinMaterial.color = originalColor;
currentState = State.Chasing;
pathfinder.enabled = true;
}
IEnumerator UpdatePath() {
float refreshRate = .25f;
while (hasTarget) {
if (currentState == State.Chasing)
{
Vector3 dirToTarget = (target.position - transform.position).normalized;
Vector3 targetPosition = target.position - dirToTarget * (myCollisionRadius + targetCollisionRadius + attackDistanceThreshold/2);
if (!dead)
{
pathfinder.SetDestination(targetPosition);
}
}
yield return new WaitForSeconds(refreshRate);
}
}
}
`
Episode 12 the obstiles are just remaining flat
I did this but none of the obsticals have any height even after i put the min to 0.27 and the max to 4.2
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
public class MapGenerator : MonoBehaviour {
public Map[] maps;
public int mapIndex;
public Transform tilePrefab;
public Transform obstaclePrefab;
public Transform navmeshFloor;
public Transform navmeshMaskPrefab;
public Vector2 maxMapSize;
[Range(0,1)]
public float outlinePercent;
public float tileSize;
List<Coord> allTileCoords;
Queue<Coord> shuffledTileCoords;
Map currentMap;
private void Start(){
GenerateMap();
}
public void GenerateMap() {
System.Random prng = new System.Random(currentMap.seed);
currentMap = maps[mapIndex];
//
allTileCoords = new List<Coord>();
for (int x = 0; x < currentMap.mapSize.x; x++){
for (int y = 0; y < currentMap.mapSize.y; y++){
allTileCoords.Add(new Coord(x, y));
}
}
shuffledTileCoords = new Queue<Coord>(Utility.ShuffleArray(allTileCoords.ToArray(), currentMap.seed));
//
string holderName = "Generated Map";
if (transform.FindChild(holderName)) {
DestroyImmediate(transform.FindChild(holderName).gameObject);
}
Transform mapHolder = new GameObject(holderName).transform;
mapHolder.parent = transform;
//
for (int x = 0; x < currentMap.mapSize.x; x++) {
for (int y = 0; y < currentMap.mapSize.y; y++) {
Vector3 tilePosition = CoordToPosition(x, y);
Transform newTile = Instantiate(tilePrefab, tilePosition, Quaternion.Euler(Vector3.right * 90))as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent) * tileSize;
newTile.parent = mapHolder;
}
}
//
bool[,] obstacleMap = new bool[(int)currentMap.mapSize.x, (int)currentMap.mapSize.y];
int obstacleCount = (int)(currentMap.mapSize.x * currentMap.mapSize.y * currentMap.obstaclePercent);
int currentObstacleCount = 0;
for (int i = 0; i < obstacleCount; i++) {
Coord randomCoord = GetRandomCoord();
obstacleMap[randomCoord.x, randomCoord.y] = true;
currentObstacleCount++;
if (randomCoord != currentMap.MapCentre && MapIsFullyAccessible(obstacleMap, currentObstacleCount)){
float obstacleHeight = Mathf.Lerp(currentMap.minObstacleHeight, currentMap.maxObstacleHeight, (float)prng.NextDouble());
Vector3 obstaclePosition = CoordToPosition(randomCoord.x, randomCoord.y);
Transform newObstacle = Instantiate(obstaclePrefab, obstaclePosition + Vector3.up * obstacleHeight / 2, Quaternion.identity) as Transform;
newObstacle.parent = mapHolder;
newObstacle.localScale = new Vector3((1 - outlinePercent) * tileSize, obstacleHeight, (1 - outlinePercent) * tileSize);
}
else {
obstacleMap[randomCoord.x, randomCoord.y] = false;
currentObstacleCount--;
}
}
//
Transform maskLeft = Instantiate(navmeshMaskPrefab, Vector3.left * (currentMap.mapSize.x + maxMapSize.x) / 4 * tileSize, Quaternion.identity) as Transform;
maskLeft.parent = mapHolder;
maskLeft.localScale = new Vector3((maxMapSize.x - currentMap.mapSize.x) / 2, 1, currentMap.mapSize.y) * tileSize;
Transform maskRight = Instantiate(navmeshMaskPrefab, Vector3.right * (currentMap.mapSize.x + maxMapSize.x) / 4 * tileSize, Quaternion.identity) as Transform;
maskRight.parent = mapHolder;
maskRight.localScale = new Vector3((maxMapSize.x - currentMap.mapSize.x) / 2, 1, currentMap.mapSize.y) * tileSize;
Transform maskTop = Instantiate(navmeshMaskPrefab, Vector3.forward * (currentMap.mapSize.y + maxMapSize.y) / 4 * tileSize, Quaternion.identity) as Transform;
maskTop.parent = mapHolder;
maskTop.localScale = new Vector3(maxMapSize.x, 1, (maxMapSize.y - currentMap.mapSize.y)/2) * tileSize;
Transform maskBottom = Instantiate(navmeshMaskPrefab, Vector3.back * (currentMap.mapSize.y + maxMapSize.y) / 4 * tileSize, Quaternion.identity) as Transform;
maskBottom.parent = mapHolder;
maskBottom.localScale = new Vector3(maxMapSize.x, 1, (maxMapSize.y - currentMap.mapSize.y) / 2) * tileSize;
navmeshFloor.localScale = new Vector3(maxMapSize.x, maxMapSize.y) * tileSize;
}
bool MapIsFullyAccessible(bool[,] obsticleMap, int currentObstacleCount){
bool[,] mapFlags = new bool[obsticleMap.GetLength(0), obsticleMap.GetLength(1)];
Queue<Coord> queue = new Queue<Coord>();
queue.Enqueue(currentMap.MapCentre);
mapFlags[currentMap.MapCentre.x, currentMap.MapCentre.y] = true;
int accessibleTileCount = 1;
while (queue.Count > 0)
{
Coord tile = queue.Dequeue();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
int neighbourX = tile.x + x;
int neighbourY = tile.y + y;
if (x == 0 || y == 0) {
if (neighbourX >= 0 && neighbourX < obsticleMap.GetLength(0) && neighbourY >= 0 && neighbourY < obsticleMap.GetLength(1)) {
if (!mapFlags[neighbourX,neighbourY] && !obsticleMap[neighbourX, neighbourY]) {
mapFlags[neighbourX, neighbourY] = true;
queue.Enqueue(new Coord(neighbourX, neighbourY));
accessibleTileCount++;
}
}
}
}
}
}
int targetaccessibleTileCount = (int)(currentMap.mapSize.x * currentMap.mapSize.y - currentObstacleCount);
return targetaccessibleTileCount == accessibleTileCount;
}
Vector3 CoordToPosition(int x, int y) {
return new Vector3(-currentMap.mapSize.x / 2 + 0.5f + x, 0, -currentMap.mapSize.y / 2 + 0.5f + y) * tileSize;
}
public Coord GetRandomCoord() {
Coord randomCoord = shuffledTileCoords.Dequeue();
shuffledTileCoords.Enqueue(randomCoord);
return randomCoord;
}
[System.Serializable]
public struct Coord {
public int x;
public int y;
public Coord(int _x, int _y) {
x = _x;
y = _y;
}
public static bool operator == (Coord c1, Coord c2) {
return c1.x == c2.x && c1.y == c2.y;
}
public static bool operator !=(Coord c1, Coord c2){
return !(c1 == c2);
}
}
[System.Serializable]
public class Map {
public Coord mapSize;
[Range(0,1)]
public float obstaclePercent;
public int seed;
public float minObstacleHeight;
public float maxObstacleHeight;
public Color foregroundColour;
public Color backgroundColour;
public Coord MapCentre{
get {
return new Coord(mapSize.x / 2, mapSize.y / 2);
}
}
}
}
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.