Click here to Skip to main content
15,506,209 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moviment1 : MonoBehaviour
{
    public float Speed;
    public float JumpForce; 

    public bool isJumping;
    public bool doubleJump;

    private Rigidbody2D rig;
    
    // Start is called before the first frame update
    void Start()
    {
      rig = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        Move();
        Jump();
    }
   //  MOVE 

    void Move()
    {
        Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0.0f, 0.0f);
        transform.position += movement * Time.deltaTime * Speed;
    }
  
     // JUMP
    void Jump()
    {
        if (Input.GetButtonDown("Jump") )
        {
            if(!isJumping)
            {
           
         rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
        doubleJump = true;
        }
          else
          {
             if(doubleJump)
             {
                rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
           doubleJump = false;
    }
    }
    }
    }      
       void OnCollisionEnter2D(Collision2D collision)
       {
         if(collision.gameObject.layer == 8)
         {
             isJumping = false;
         }
       }
       void OnCollisionExit2D(Collision2D collision)
       {
          if(collision.gameObject.layer == 8)
      {  

         isJumping = true;
   }
  
       } 


What I have tried:

I wanted to try to understand what the error is and if you can help me to make it disappear, a solution for it
Thank you
Posted
Updated 20-Apr-22 6:34am
v2
Comments
Richard MacCutchan 20-Apr-22 10:33am    
As you have already been told, the error message tells you exactly what is missing, and where it needs to be placed.
Joana Gonçalves 2022 20-Apr-22 10:50am    
I already did the advice you gave, now it's giving another error

You are missing a closing curly brace '}' at the very end.
Explained here: Compiler Error CS1513 | Microsoft Docs[^]

Btw: VS supports code beautify, which would help in such cases.
 
Share this answer
 
Comments
CPallini 20-Apr-22 9:55am    
5.
0x01AA 20-Apr-22 9:57am    
Thank you sir.
In addition to what 0x01AA says, indent your code: it becomes a whole load easier to see what is wrong if you can visually match up open brackets, close brackets, and the code blocks they enclose:
    void Jump()
{
if (Input.GetButtonDown("Jump") )
{
if(!isJumping)
{

rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
doubleJump = true;
}
else
{
if(doubleJump)
{
rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
doubleJump = false;
}
}
}
}   
Is difficult to work with, but indented correctly:
void Jump()
    {
    if (Input.GetButtonDown("Jump") )
        {
        if(!isJumping)
            {
            rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
            doubleJump = true;
            }
        else
            {
            if(doubleJump)
                {
                rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
                doubleJump = false;
                }
            }
        }
    }   
It is a lot more readable.
It's also often considered better to combine the else if into one line:
void Jump()
    {
    if (Input.GetButtonDown("Jump") )
        {
        if(!isJumping)
            {
            rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
            doubleJump = true;
            }
        else if(doubleJump)
            {
            rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
            doubleJump = false;
            }
        }
    }
Again, it improves readbillity.
 
Share this answer
 
v2
Comments
Joana Gonçalves 2022 20-Apr-22 10:50am    
I already did the advice you gave, now it's giving another error
OriginalGriff 20-Apr-22 11:10am    
And were you planning on sharing the error message with us at some point?
Joana Gonçalves 2022 20-Apr-22 12:23pm    
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moviment1 : MonoBehaviour
{
public float Speed;
public float JumpForce;

public bool isJumping;
public bool doubleJump;

private Rigidbody2D rig;

// Start is called before the first frame update

void Start()
{

rig = GetComponent<rigidbody2d>();
}

// Update is called once per frame

void Update()
{
Move();
Jump();
}

// MOVE

void Move()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
transform.position += movement * Time.deltaTime * Speed;
}

// JUMP
void Jump()
{
if (Input.GetButtonDown("Jump") )
{
if(!isJumping)
{

rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
doubleJump = true;
}
else
{
if(doubleJump)
{
rig.AddForce(new Vector2(0f, JumpForce), ForceMode2D,Impulse);
doubleJump = false;
}

}

}

}
void OnCollisionEnter2D(Collision2D collision)
{
if(collision.gameObject.layer == 8)
{
isJumping = false;
}

}
void OnCollisionExit2D(Collision2D collision)
{
if(collision.gameObject.layer == 8)
{

isJumping = true;
}

}

}
Joana Gonçalves 2022 20-Apr-22 12:26pm    
(45,51):error CS0119:"ForceModeD" é um tipo que não é válido para o contexto
especificado (47,63):error CS0113:"Impulse" não existe no contexto atual
OriginalGriff 20-Apr-22 12:43pm    
Read the error messages: they are pretty explicit.
"ForceModeD" is a Type - so it's a class name, not a variable!

You need to learn how to fix syntax errors, you are going to get them every time you write code (we all do). Getting others to fix them is the slowest way imaginable to get that done!

See here: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^]
STOP POSTING THE SAME QUESTION MULTIPLE TIMES ALL OVER THE SITE!
 
Share this answer
 
Comments
Joana Gonçalves 2022 20-Apr-22 12:28pm    
I just got here, stop being rude to me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900