I am making a 2D game in Unity , for the first time. I'm writing a script, also for the first time, I watched some tutorials and I think it's not bad. However, I keep getting an error message and I have no idea what to do. Well, the error is displayed to me in a place where previously no error was displayed and everything worked. Only after typing in "void Update" :
if (Input.GetKeyDown(KeyCode.R))
{
Attack();
}
void Attack()
{
// Play an attack animation
animator.SetTrigger("Attack");
// Detect enemies in range of attack
// Damage them
}
}
(I wrote it the way the tutorial said to) Suddenly when I wanted to see if it worked I got an error "Identifier expected". The error is located a line further down, here:
@if (grouned)
doubleJump = false;
anim.SetBool ("Grounded", grouned);
if(Input.GetKeyDown(KeyCode.W)&& grouned)
"If" displayed that something was wrong, so I added an "@" there, but now it shows that something should be/something is wrong after the word "grouned". I have no idea what to do.
Here you have the whole script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public Animator animator;
public float moveSpeed;
public float jumpHeight;
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask WhatIsGround;
private bool grouned;
private bool doubleJump;
private Animator anim;
// Start is called before the first frame update
void Start(){
anim = GetComponent<Animator> ();
}
void FixedUpdate(){
grouned = Physics2D.OverlapCircle (groundCheck.position, groundCheckRadius, WhatIsGround);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
Attack();
}
void Attack()
{
// Play an attack animation
animator.SetTrigger("Attack");
// Detect enemies in range of attack
// Damage them
}
}
@if (grouned)
doubleJump = false;
anim.SetBool ("Grounded", grouned);
if(Input.GetKeyDown(KeyCode.W)&& grouned)
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, jumpHeight);
}
if(Input.GetKeyDown(KeyCode.W)&& !grouned && !doubleJump)
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (0, jumpHeight);
doubleJump = true;
}
if(Input.GetKey(KeyCode.D))
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if(Input.GetKey(KeyCode.A))
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
anim.SetFloat ("Speed", Mathf.Abs (GetComponent<Rigidbody2D> ().velocity.x));
if(GetComponent<Rigidbody2D>().velocity.x > 0)
{
transform.localScale = new Vector3 (3f, 3f, 3f);
}
else if (GetComponent<Rigidbody2D>().velocity.x < 0)
transform.localScale = new Vector3 (-3f, 3f, 3f);
}
}
Having tidied up your code, it's obvious that the code starting at
@if...is not contained within a method.The chances are that some of your closing braces
}are in the wrong place.Keeping your code well formatted would have helped you identify this error. Good formatting is not just good practice, it helps with reading the code and also helps spot errors like this.
Also, I'm not sure what you were hoping to achieve with
@if, this isn't a razor view/page!