using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movecode : MonoBehaviour
{
private bool jumpKeyWasPressed;
private Rigidbody rigidBody;
private float horizontalInput;
private bool isGrounded;
// Start is called before the first frame update
void Start()
{
rigidBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
if(!isGrounded) {
return;
}
if(Input.GetKeyDown(KeyCode.Space)) {
jumpKeyWasPressed = true;
}
if(jumpKeyWasPressed) {
rigidBody.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
jumpKeyWasPressed = false;
}
horizontalInput = Input.GetAxis("Horizontal");
rigidBody.velocity = new Vector3(horizontalInput, rigidBody.velocity.y, 0);
}
void FixedUpdate() {
if(jumpKeyWasPressed) {
rigidBody.AddForce(Vector3.up * 5, ForceMode.VelocityChange);
jumpKeyWasPressed = false;
}
void OnCollisionEnter() {
isGrounded = true;
}
void OnCollisionExit() {
isGrounded = false;
}
}
}
I am trying to make a thing where I can’t double jump because my game is not a flappy bird. I am following a tutorial and I succeeded but I am learning not copying so I decided to restart with no help, but, the code is not working.
You’ve declared two methods inside the FixedUpdate() Unity message. They should be moved outside the FixedUpdate’s braces to property work as intended.
please, do yourself a favour, format that code. that is a jungle of confusion.
an error message tells you whats wrong, most of the time
in your case that should be a warning or info message not an error, not used just means you have not called the function/variable.
in the follwing code you are missing a " } " – its placed incorrectly
One of the reasons I like to have the opening bracket on it’s own line (C# standard). That means opening anc closing braces are always at the same indention level. If also avoids confusion when using a single statement body without braces like
if (condition)
DoSomething();
// vs
if (condition)
{
DoSomething();
}
Turns out C# >= 7 has support for nested functions, so that can be valid C# syntax. That of course has completely different semantics from having corresponding class member methods, so it will not work as corresponding unity message handlers like OnCollisionEnter. Not sure if all the Unity scripting backends support this C# feature. Nice feature to have, but one more potential pitfall for beginners.
Fun fact: This was my first through when I got those news back then. It’s also not “that” useful as it’s usage would be quite rare. There were some occations in the past where I needed a local function / closure and when I needed it, I just created this the old fashion way, creating a local delegate. Of course slightly bulky syntax but does the job. Local methods of course provide a much cleaner syntax, though just cause more problems like this one. In the past this would be just a compiler error, now it turned into a warning (only because the two methods weren’t called at all).
There’s a long list of new errors related to this. For example using access modifiers on local methods would also cause an error, since they are local methods. So I guess the majority of cases when local methods pop into existence is by mistakes ^^.
I was just thinking about an old case where I actually used a local delegate and I found this crazy example This was at a time we didn’t have local methods, so I created a normal lambda closure. It looks terrible but works. Of course instead of a closure I could have created another static method and pass the relevant variables as arguments. However in some cases you may need some recursive sub step that needs access to various variables which may not be reference types. So you would have to use ref parameters and it would get messy quite fast. local methods or closures can solve such cases quite nicely.
Though as I said, those are really rare usecases and there are always alternatives. Local methods keep the class clean if a certain method is really only used inside that method.