So i know its quite alot of code but I’ve tried a lot and i really don’t get whats wrong. VIsual studio marks some of my { brackets red but i don’t get why. I managed to temporarily fix it by adding brackets at random places but after making a new IEnumerator it broke again.
I get two errors in the editor:
Scripts.cs(302,5): error CS0106: the modifier “private” is not valid for this item
Scripts.cs(365,2): error CS1513: } expected
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
//player variables
public float speed;
//combat player variables
//block variables
public float blockCooldown;
private bool blockCooldownComplete;
private string blockDirection;
private GameObject[] blockHitboxes;
private bool isBlocking;
//dodge variables
public float dodgeCooldown;
public float dodgeDistance;
private bool dodgeCooldownComplete;
private string dodgeDirection;
private bool isDodging;
//attack variables
public float attackCooldown;
private bool attackCooldownComplete;
private string attackDirection;
private GameObject[] attackHitboxes;
private bool isAttacking;
//Gun Variables
public GameObject BulletPrefab;
public string attack1FunctionName;
public string attack2FunctionName;
//Input variables
private float horizontalInput;
private float verticalInput;
//gravity variables
//timer variables
private float timer;
//keybinds
//Add keybinds for wasd
//action keybinds
public string attack1Keybind;
public string attack2Keybind;
public string blockKeybind;
public string dodgeKeybind;
//NOTES
//When finished with game demo check wether space or shift for block/dodge
//Make seprate gun cooldown that is longer than knife so you can shoot and then knife
// Start is called before the first frame update
void Start()
{
//Identify all block hitboxes
blockHitboxes = new GameObject[] {GameObject.Find("Block Left"), GameObject.Find("Block Right"), GameObject.Find("Block Up")};
//disable all hitbox gameobjects temporarily
for(int i = 0; i < blockHitboxes.Length; i++)
{
blockHitboxes[i].SetActive(false);
}
//Identify all attacka hitboxes
attackHitboxes = new GameObject[] {GameObject.Find("Knife Left"), GameObject.Find("Knife Right"), GameObject.Find("Knife Up")};
//disable all hitbox gameobjects temporarily
for(int i = 0; i < attackHitboxes.Length; i++)
{
attackHitboxes[i].SetActive(false);
}
//make the cooldown comnplete in start of game so that you can block one time beforer the cooldown is activated
blockCooldownComplete = true;
}
// Update is called once per frame
void Update()
{
//wasd/arrows input
horizontalInput = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
verticalInput = Input.GetAxis("Vertical") * speed * Time.deltaTime;
//Check so that the player is not blocking or attack before moving
if(!isBlocking && !isAttacking)
{
//horizontal movement
transform.Translate(horizontalInput, 0, 0);
}
//attack and block Input
//get potential block input & check so that the players block cooldown is complete
if(Input.GetKeyDown(blockKeybind) && blockCooldownComplete)
{
StopAllCoroutines();
StartCoroutine(BlockFuction());
}
//Dodge Input(Shift)
if(Input.GetKeyDown(dodgeKeybind) && dodgeCooldownComplete)
{
StopAllCoroutines();
StartCoroutine(DodgeFunction());
}
//attack 1 input(K)
if(Input.GetKeyDown(attack1Keybind))
{
StartCoroutine(attack1FunctionName);
}
//attack 2 input(L)
if(Input.GetKeyDown(attack2Keybind))
{
StartCoroutine(attack2FunctionName);
}
//add movement✓, knife✓, gun, block on shift✓, and dodge on space, changing controls✓
}
//function to handle the block and figure out what direction the player blocks in
private IEnumerator BlockFuction()
{
//reset blockdirectiion incase the player does not give any additional input & reset blockCoolDowncComplete so that the game knows that it has to wait for another cooldown
//blockDirection = "none";
blockCooldownComplete = false;
//delay to recognise input for what direction to b lock in
for(int i = 0; i < 10; i++)
{
//all input directions
if(Input.GetKey(KeyCode.A))
{
blockDirection = "Left";
} if(Input.GetKey(KeyCode.D))
{
blockDirection = "Right";
}if(Input.GetKey(KeyCode.W))
{
blockDirection = "Up";
}
//make sure each repetition takes 0.001 secconds which means the loop in a whole takes 0.3 secconds
yield return new WaitForSeconds(0.001f);
}
//check so that the player actually gave direction input for the block
if(blockDirection != "none")
{
//tell the script the player is Blocking
isBlocking = true;
//execute block
//check which hitbox to activate based of the direction input
for(int i = 0; i < blockHitboxes.Length; i++)
{
if(blockHitboxes[i].name == "Block " + blockDirection)
{
blockHitboxes[i].SetActive(true);
}
}
//wait for duration of animation
yield return new WaitForSeconds(1f);
//disable all block hitboxes just in case
for(int i = 0; i < blockHitboxes.Length; i++)
{
blockHitboxes[i].SetActive(false);
}
//tell the script the player is no longer Blocking
isBlocking = false;
//wait for cooldown then allow the player to block again
yield return new WaitForSeconds(blockCooldown);
blockCooldownComplete = true;
//add so that when successful block no cool down and that when you parry an attack it immediatly stops blocking so you can attack
}
}
//function to handle the block and figure out what direction the player blocks in
private IEnumerator DodgeFuction()
{
//reset blockdirectiion incase the player does not give any additional input & reset blockCoolDowncComplete so that the game knows that it has to wait for another cooldown
//blockDirection = "none";
dodgeCooldownComplete = false;
//delay to recognise input for what direction to b lock in
for(int i = 0; i < 10; i++)
{
//all input directions
if(Input.GetKey(KeyCode.A))
{
dodgeDirection = "Left";
} if(Input.GetKey(KeyCode.D))
{
dodgeDirection = "Right";
}
//make sure each repetition takes 0.001 secconds which means the loop in a whole takes 0.3 secconds
yield return new WaitForSeconds(0.001f);
}
//check so that the player actually gave direction input for the block
if(dodgeDirection != "none")
{
//tell the script the player is Blocking
isDodging = true;
//execute block
//check which hitbox to activate based of the direction input
if(dodgeDirection == "Left")
{
//Make the movement smooth and also act as a cooldown
for(int i = 0; i > 100; i++)
{
transform.Translate(dodgeDistance / 100, 0, 0);
yield return new WaitForSeconds(0.01f);
}
}else if(dodgeDirection == "Right")
{
transform.Translate(-dodgeDistance / 100, 0, 0);
yield return new WaitForSeconds(0.01f);
}
//tell the script the player is no longer Blocking
isDodging = false;
//wait for cooldown then allow the player to block again
yield return new WaitForSeconds(dodgeCooldown);
dodgeCooldownComplete = true;
//add so that when successful block no cool down and that when you parry an attack it immediatly stops blocking so you can attack
}
}
//Weapon functions
IEnumerator KnifeAttack()
{
//reset attackdirectiion incase the player does not give any additional input & reset attackCoolDowncComplete so that the game knows that it has to wait for another cooldown
//attackDirection = "none";
attackCooldownComplete = false;
//delay to recognise input for what direction to attack lock in
for(int i = 0; i < 10; i++)
{
//all input directions
if(Input.GetKey(KeyCode.A))
{
attackDirection = "Left";
} if(Input.GetKey(KeyCode.D))
{
attackDirection = "Right";
}if(Input.GetKey(KeyCode.W))
{
attackDirection = "Up";
}
//make sure each repetition takes 0.001 secconds which means the loop in a whole takes 0.3 secconds
yield return new WaitForSeconds(0.001f);
}
//check so that the player actually gave direction input for the attack
if(attackDirection != "none")
{
//tell the script the player is Attacking
isAttacking = true;
//execute attack
//check which hitbox to activate based of the direction input
for(int i = 0; i < attackHitboxes.Length; i++)
{
if(attackHitboxes[i].name == "Knife " + attackDirection)
{
attackHitboxes[i].SetActive(true);
}
}
//wait for duration of animation
yield return new WaitForSeconds(0.15f);
//disable all attack hitboxes just in case
for(int i = 0; i < attackHitboxes.Length; i++)
{
attackHitboxes[i].SetActive(false);
}
//tell the script the player is no longer Attackinhg
isAttacking = false;
//wait for cooldown then allow the player to attack again
yield return new WaitForSeconds(attackCooldown);
attackCooldownComplete = true;
}
private IEnumerator GunAttack()
{
//reset blockdirectiion incase the player does not give any additional input & reset blockCoolDowncComplete so that the game knows that it has to wait for another cooldown
//attackDirection = "none";
attackCooldownComplete = false;
//delay to recognise input for what direction to b lock in
for(int i = 0; i < 10; i++)
{
//all input directions
if(Input.GetKey(KeyCode.A))
{
attackDirection = "Left";
} if(Input.GetKey(KeyCode.D))
{
attackDirection = "Right";
}if(Input.GetKey(KeyCode.W))
{
attackDirection = "Up";
}
//make sure each repetition takes 0.001 secconds which means the loop in a whole takes 0.3 secconds
yield return new WaitForSeconds(0.001f);
}
//check so that the player actually gave direction input for the block
if(attackDirection != "none")
{
//tell the script the player is Blocking
isAttacking = true;
//execute block
//check which hitbox to activate based of the direction input
if(attackDirection == "Left")
{
GameObject tempObject = Instantiate(BulletPrefab, transform.position, BulletPrefab.transform.rotation);
tempObject.transform.Rotate(0, 0, 180);
}else if(attackDirection == "Right")
{
GameObject tempObject = Instantiate(BulletPrefab, transform.position, BulletPrefab.transform.rotation);
tempObject.transform.Rotate(0, 0, 0);
}else if(attackDirection == "Up")
{
GameObject tempObject = Instantiate(BulletPrefab, transform.position, BulletPrefab.transform.rotation);
tempObject.transform.Rotate(0, 0, 90);
}
//wait for duration of animation
yield return new WaitForSeconds(0.25f);
//disable all block hitboxes just in case
for(int i = 0; i < attackHitboxes.Length; i++)
{
attackHitboxes[i].SetActive(false);
}
//tell the script the player is no longer Blocking
isAttacking = false;
//wait for cooldown then allow the player to block again
yield return new WaitForSeconds(attackCooldown);
attackCooldownComplete = true;
}
}