using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D body;
private SpriteRenderer spriteRenderer;
private float horizontal;
private float speed = 8f;
private float jumpingPower = 16f;
private bool isFacingRight;
[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundLayer;
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
if (Input.GetButtonDown("Jump") && isGrounded())
{
rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
}
if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f)
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
}
Flip();
}
private void FixedUpdate()
{
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
if (Input.GetKeyDown(KeyCode.A))
{
spriteRenderer.flipX == true;
}
}
private bool isGrounded()
{
return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
}
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
isFacingRight = !isFacingRight;
Vector3 localScale = transform.localScale;
localScale.x *= 1f;
transform.localScale = localScale;
}
}
}
Helps to post the error in full. No one memorises error codes.
In any case, read what the error is saying. Your Code editor should also be high-lighting the problem area.
well that == true is a compile error… but as said it will tell you a line and a place and what… it is usually right
It DOES give errors, unless you turn them off … so turn them back on
ok no… I just tried it ad that doesn’t work…
yeah i saw that starting the game it give errors when i touch “A” HEEEELP
We can’t help if you don’t tell us what the errors are.
the error says: “Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement”
Once again, the error tells you where it is in the code.
We can’t help when we can’t see your code nor the full error message (including the stack trace). Help us help you, otherwise you’re wasting both our time.
1 Like
it’s on the 28th line (modified sry)
You can fix your own typing mistakes. Here’s how:
Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
The complete error message contains everything you need to know to fix the error yourself.
The important parts of the error message are:
- the description of the error itself (google this; you are NEVER the first one!)
- the file it occurred in (critical!)
- the line number and character position (the two numbers in parentheses)
- also possibly useful is the stack trace (all the lines of text in the lower console window)
Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.
Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!
All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.
So, if it is line 28 and that matches the line numbers above, Ive already told you whats wrong with it.
1 Like
Instead of hounding the forums for every problem you have… You can fix it yourself easy with a little debugging.
Time to start debugging!!! Here is how you can begin your exciting new debugging adventures!
Debugging can allow you to get all the information you need to discover what the problem is. Once you actually know the problem, only then will you be able to create a solution…
The most coming issue that happens is one of the following:
- the code you think is running isn’t
- the code is running sooner or later than you think
- the code is running less or more often than you think
- the code is running somewhere else than you think it is
- you haven’t look at the previous errors or warnings in the console
To help gain more insight into your problem, I recommend literally sprinkling Debug.Log() statements everywhere near the source of the problem to display all the information in realtime.
“When in doubt, Debug.log it out!"