Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
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.
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);
}
}
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!
It looks like you've put your Attack function in the middle of the update function which is making your curly braces not line up properly and leaving a lot of code outside of a function.
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();
}
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);
}
void Attack()
{
// Play an attack animation
animator.SetTrigger("Attack");
// Detect enemies in range of attac
// Damage them
}
}
Are you using some kind of editor with syntax highlighting? It would help you catch errors like this along with indenting your code properly which will help you spot missing/extra { braces.
Related
so first time learning C# for my team studio class. I found this video which I was able to get my player moving and jumping perfectly, https://www.youtube.com/watch?v=3GtQ2yQX2kU&t=569s however after I added a health/can die system to the script, now the player refuses to jump no matter what I do. Since I am a newbie at this, I am just going to post the whole thing here if anyone can point me in the right direction. I've played with the box colliders, added the layers to the platforms like the tutorial said... please, if anyone can help. I know I'm a newbie to this :(
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public HealthBar healthBar; //Communicates with the healthbar script
public float moveSpeed;
public float jumpForce;
public int jumpsAmount;
int jumpsLeft;
public Transform GroundCheck;
public LayerMask GroundLayer;
public int maxHealth = 3;
public int currentHealth;
bool isGrounded;
float moveInput;
Rigidbody2D rb2d;
float scaleX;
// Start is called before the first frame update
void Start()
{
healthBar.SetMaxHealth(maxHealth); //Sets healthbar to max health
currentHealth = maxHealth;
rb2d = GetComponent<Rigidbody2D>();
scaleX = transform.localScale.x;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
if (currentHealth <= 0) //When Health is zero the player is destroyed
{
Destroy(gameObject);
}
healthBar.SetHealth(currentHealth); //Updates Healthbar when damgage is taken
}
// Update is called once per frame
void Update()
{
moveInput = Input.GetAxisRaw("Horizontal");
Jump();
}
private void FixedUpdate()
{
Move();
}
public void Move()
{
Flip();
rb2d.velocity = new Vector2(moveInput * moveSpeed, rb2d.velocity.y);
}
public void Flip()
{
if (moveInput > 0)
{
transform.localScale = new Vector3(scaleX, transform.localScale.y, transform.localScale.z);
}
if (moveInput < 0)
{
transform.localScale = new Vector3((-1) * scaleX, transform.localScale.y, transform.localScale.z);
}
}
public void Jump()
{
if (Input.GetKeyDown(KeyCode.Space))
{
CheckIfGrounded();
if (jumpsLeft > 0)
{
rb2d.velocity = new Vector2(rb2d.velocity.x, jumpForce);
jumpsLeft--;
}
}
}
public void CheckIfGrounded()
{
isGrounded = Physics2D.OverlapCircle(GroundCheck.position, GroundCheck.GetComponent<CircleCollider2D>().radius, GroundLayer);
ResetJumps();
}
public void ResetJumps()
{
if (isGrounded)
{
jumpsLeft = jumpsAmount;// jumpsAmount =2;
}
}
}
I put the groundcheck collider where the video wanted me to, even played around having different locations too. Nothing is working... tried changing the jump force too to see if it needs to be higher. I am at a complete loss.
So, I want to make it so the player loses control when it collides with an enemy. I already got a thing set up to make the player fly off towards the direction of the collision, but I can't seem to be able to make them lose control. I want input to not register.
Here's my player controller script:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D _rigidBody2d;
Animator animatorController;
public float moveSpeed;
public float jumpForce = 1f;
float maxJumpHeight;
bool isOnGround = false;
bool collidedWithEnemy = false;
private void Start()
{
_rigidBody2d = GetComponent<Rigidbody2D>();
animatorController = GetComponent<Animator>();
}
float horizontalInput;
private void Update()
{
if (collidedWithEnemy == false)
{
horizontalInput = Input.GetAxis("Horizontal");
}
// Player movement
MovementMechanics();
// Sprite Flipping
SpriteFlipping();
// Jumping
JumpingMechanics();
// Set Falling Animation
FallingMechanics();
}
void MovementMechanics()
{
if (horizontalInput != 0)
{
transform.Translate(new Vector2(1, 0) * horizontalInput * Time.deltaTime * moveSpeed);
animatorController.SetBool("isMoving", true);
}
else
{
animatorController.SetBool("isMoving", false);
}
}
void SpriteFlipping()
{
if (horizontalInput > 0)
{
transform.localScale = new Vector3(-1, 1, 1);
}
else if (horizontalInput < 0)
{
transform.localScale = new Vector3(1, 1, 1);
}
}
void JumpingMechanics()
{
if (Input.GetKeyDown(KeyCode.Space) && isOnGround)
{
_rigidBody2d.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
isOnGround = false;
animatorController.SetBool("isMoving", false);
animatorController.SetBool("isJumping", true);
animatorController.SetBool("isFalling", false);
}
}
void FallingMechanics()
{
if (_rigidBody2d.velocity.y < 0f)
{
animatorController.SetBool("isFalling", true);
}
else if (_rigidBody2d.velocity.y > 0f)
{
animatorController.SetBool("isFalling", false);
animatorController.SetBool("isJumping", true);
}
}
void OnCollisionEnter2D(Collision2D other)
{
// Avoids Jump Spawn
if (other.gameObject.CompareTag("Ground"))
{
isOnGround = true;
collidedWithEnemy = false;
animatorController.SetBool("isJumping", false);
animatorController.SetBool("isMoving", false);
animatorController.SetBool("isFalling", false);
}
// On Collision With Enemy
if (other.gameObject.CompareTag("Enemy"))
{
collidedWithEnemy = true;
}
}
}
And here's the enemy script:
using UnityEngine;
public class EnemyController : MonoBehaviour
{
Vector2 otherTransform;
Rigidbody2D playerRigidBody2d;
public float impulseForce = 100f;
private void Start()
{
playerRigidBody2d = GameObject.Find("Player").GetComponent<Rigidbody2D>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
otherTransform = other.gameObject.transform.position;
playerRigidBody2d.AddForce((otherTransform * 1) * impulseForce, ForceMode2D.Impulse);
other.transform.Translate(Vector3.zero);
}
}
}
And here's a little video of whats happening:
https://vimeo.com/709461296
As you can see in the console, the game still registers horizontal input after collision with enemy
(Don't mind the player launching off at lightspeed, thats not my concern here, I can fix that easily)
I'd say the problem is that you don't set horizontalInput to zero if the collision happens. The way you wrote your Update method, it takes the previously assigned value of horizontalInput and processes it.
Add the condition of not colliding with the enemy in MovementMechanics():
if (horizontalInput != 0 && !collidedWithEnemy)
{
// do something..
}
so I started unity literally yesterday meaning this is my second day. I followed a tutorial to make a movement script, but when I continuously press space, I can jump like infinitely. This is my code below, is there any way to fix the infinite jump to just two jumps and then it resets when it hits the ground?
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float speed;
private Rigidbody2D body;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
}
private void Update()
{
body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, body.velocity.y);
if (Input.GetKey(KeyCode.Space))
body.velocity = new Vector2(body.velocity.x, speed);
}
}
You can prevent this by adding time delay
using UnityEngine;
public class PlayerMovement : MonoBehaviour {
[SerializeField] private float speed;
[SerializeField] private float jumpTimeDelay = 0.5f; // feel free to increase or decrease
private const float timeDelayConst = jumpTimeDelay;
private bool isGrounded = false;
private Rigidbody2D body;
private void Awake() {
body = GetComponent<Rigidbody2D>();
}
private void Update(){
body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed,
body.velocity.y);
isGrounded = jumpTimeDelay <= 0;
jumpTimeDelay -= Time.time;
if (Input.GetKey(KeyCode.Space) && isGrounded){
body.velocity = new Vector2(body.velocity.x, speed);
jumpTimeDelay = timeDelayConst;
}
}
}
This is easily done by adding a boolean value to check if the player is grounded or not and change the value when player jumps. You also need to add a tag to your ground object so we can check if the player is touching the ground. To add tags to objects please refer to Unity - Manual: https://docs.unity3d.com/530/Documentation/Manual/Tags.html
Just for future these kind of questions are asked a lot so you can pretty much google your question and find similar answers.
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float speed;
private Rigidbody2D body;
private bool isGrounded = true;
private void Awake()
{
body = GetComponent<Rigidbody2D>();
}
private void Update()
{
body.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, body.velocity.y);
if(Input.GetKey(KeyCode.Space))
{
if (isGrounded)
{
isGrounded = false;
body.velocity = new Vector2(body.velocity.x, speed);
}
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground") {
isGrounded = true;
}
}
}
I have both player and ground with colliders 2D and player is supposed to stop on top of ground but instead it stops at the bottom of the ground.
Code
PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private float moveSpeedStore;
public float speedMultiplier;
public float speedIncreateMilestone;
private float speedIncreateMilestoneStore;
private float speedMilestoneCount;
private float speedMilestoneCountStore;
public float jumpForce;
public float jumpTime;
private float jumpTimeCounter;
private bool stoppedJumping;
private bool canDoubleJump;
private Rigidbody2D myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
public Transform groundCheck;
public float groundCheckRadius;
// private Collider2D myCollider;
private Animator myAnimator;
public GameManager theGameManager;
public AudioSource jumpSound;
public AudioSource deathSound;
// Start is called before the first frame update
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
myAnimator = GetComponent<Animator>();
jumpTimeCounter = jumpTime;
speedMilestoneCount = speedIncreateMilestone;
moveSpeedStore = moveSpeed;
speedMilestoneCountStore = speedMilestoneCount;
speedIncreateMilestoneStore = speedIncreateMilestone;
stoppedJumping = true;
}
// Update is called once per frame
void Update()
{
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if(transform.position.x > speedMilestoneCount)
{
speedMilestoneCount += speedIncreateMilestone;
speedIncreateMilestone = speedIncreateMilestone * speedMultiplier;
moveSpeed = moveSpeed * speedMultiplier;
}
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0) )
{
if(grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
stoppedJumping = false;
jumpSound.Play();
}
if(!grounded && canDoubleJump)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter = jumpTime;
stoppedJumping = false;
canDoubleJump = false;
jumpSound.Play();
}
}
if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
{
if(jumpTimeCounter > 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
}
if(Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
if(grounded)
{
jumpTimeCounter = jumpTime;
canDoubleJump = true;
}
myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
myAnimator.SetBool("Grounded", grounded);
}
void OnCollisionEnter2D(Collision2D other) {
if(other.gameObject.tag == "killbox")
{
theGameManager.RestartGame();
moveSpeed = moveSpeedStore;
speedMilestoneCount = speedMilestoneCountStore;
speedIncreateMilestone = speedIncreateMilestoneStore;
deathSound.Play();
}
}
}
Player settings
Question
What should I do to hold my player on top of ground?
NOTE: it's my first question on unity ever so if you need any sort of code or data please just ask, I try my best to provide you what you need.
Thanks.
It looks like your collider is simply too high. You'll notice that the green box indicating your collider size and position is colliding with the correct spot on the ground. The bottom of it is just touching the top of the ground. You simply need to move the collider down on the player so that the bottom of the green box is at the bottom of the player's sprite.
I coded my player in my platform game so that he would attack with a sword when you press space. It attacks right when I run right. It attacks left when I run left. But when I stand still by default it attacks left. How do I make it attack right instead?
Below is all the code in my player controller script, and also an image of my blend tree.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public int moveSpeed;
private Animator anim;
public int playerJumpPower = 1250;
private float moveX;
public bool isGrounded;
public float fJumpWaitTime = 0.2f;
private float fJumpWait;
private object col;
private bool attacking;
public float attackTime;
private float attackTimeCounter;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (!attacking)
{
if (Input.GetButtonDown("Jump"))
{
Jump();
}
if (Input.GetAxisRaw("Horizontal") > 0.5f || Input.GetAxisRaw("Horizontal") < -0.5f)
{
transform.Translate(new Vector3(Input.GetAxisRaw("Horizontal") * moveSpeed * Time.deltaTime, 0f, 0f));
}
void Jump()
{
//Jumping Code
GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);
isGrounded = false;
}
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("Player has collided with " + col.collider.name);
if (col.gameObject.tag == "ground")
{
isGrounded = true;
}
}
}
anim.SetFloat("MoveX", Input.GetAxisRaw("Horizontal"));
if (Input.GetKeyDown(KeyCode.Space))
{
attackTimeCounter = attackTime;
attacking = true;
anim.SetBool("Attack", true);
}
if(attackTimeCounter > 0)
{
attackTimeCounter -= Time.deltaTime;
}
if(attackTimeCounter <= 0)
{
attacking = false;
anim.SetBool("Attack", false);
}
}
}
After having another look at your Blend Tree, I would check if your Threst is the problem.
What you have right now:
PlayerAttackLeft 0 -1
PlayerAttackRight 1 1
What you should probably have:
PlayerAttackLeft -0.01 -1
PlayerAttackRight 0 1