Forum begins after the advertisement:

 


[Part 1] errors on velocity and victor

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 1] errors on velocity and victor

Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #17858
    insects studios
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    these are my errors pleas help me ‘rigidbody2d does not contain a defenition for ‘velocity’ ‘victor2 and 3’ is an ambiguous reference between ‘UnityEngine.Vector2 and 3’ and ‘system.numerics.victor2 and 3’

    #17859
    Ser Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It looks like you’re encountering two separate issues in your Unity project. Let’s address them one by one.

    1. Rigidbody2D does not contain a definition for ‘velocity’: This error usually indicates that you’re trying to access the velocity property of a Rigidbody2D object incorrectly. Make sure you’re using the correct syntax. Here is how you should access and set the velocity:

      Rigidbody2D rb; // Make sure you have a reference to your Rigidbody2D
      
      void Start() {
          rb = GetComponent(); // Get the Rigidbody2D component
      }
      
      void Update() {
          rb.velocity = new Vector2(5f, 0f); // Set the velocity to (5, 0)
      }

      Ensure that you are indeed working with a Rigidbody2D type and not a Rigidbody. If you’re using the correct component and still getting the error, check for any possible naming conflicts in your project.

    2. Ambiguous reference between ‘UnityEngine.Vector2’ and ‘System.Numerics.Vector2’: This error occurs when the compiler cannot determine which Vector2 you are referring to, as both Unity and System.Numerics have their own Vector2 class. To resolve this ambiguity, you can explicitly specify which Vector2 you want to use in your code.

      For example, if you want to use Unity’s Vector2, you can do it like this:

      using UnityEngine;
      
      public class Example : MonoBehaviour {
          void Start() {
              UnityEngine.Vector2 unityVector = new UnityEngine.Vector2(1f, 2f);
          }
      }

      Alternatively, if you want to use System.Numerics.Vector2, you can specify it accordingly:

      using System.Numerics;
      
      public class Example {
          void SomeMethod() {
              System.Numerics.Vector2 numVector = new System.Numerics.Vector2(1f, 2f);
          }
      }

      Choose one of the above based on your requirements and ensure that you consistently use the correct namespace in your code.

    If you still face issues after these adjustments, please provide the relevant code snippets where the errors occur for further assistance!

    #17860
    insects studios
    Level 1
    Participant
    Helpful?
    Up
    0
    ::
    void Move()
    {
        rb.Velocity = new Vector2(walkSpeed * xAxis, rb.Velocity.y);
    }
    
    public bool Grounded()
    {
        if (Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround)
            || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)
            || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void Jump()
    {
        if (Input.GetButtonUp("Jump") && rb.Velocity.y > 0)
        {
            rb.Velocity = new Vector2(rb.Velocity.x, 0);
        }
    
        if (Input.GetButtonDown("Jump") && Grounded())
        {
            rb.Velocity = new Vector3(rb.Velocity.x, jumpForce);
        }
    }
    #17861
    Ser Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It looks like you’re using Velocity with an uppercase ‘V’, which is incorrect. In Unity, the correct property is velocity with a lowercase ‘v’.

    Here’s how you should modify your code:

    void Move()
    {
        rb.velocity = new Vector2(walkSpeed * xAxis, rb.velocity.y);
    }
    
    void Jump()
    {
        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, 0);
        }
    
        if (Input.GetButtonDown("Jump") && Grounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }

    Make sure to replace all instances of rb.Velocity with rb.velocity.

    If you have any further questions or need more clarification, feel free to ask!

    #17862
    insects studios
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    now im infinitly jumping how do i fix that?

    #17863
    Ser Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    To fix the issue of infinite jumping, you need to ensure that your jump logic correctly checks if the player is grounded before allowing another jump. Based on the reference information, you should manage the jump state and air jump counter properly.

    Make sure your Jump method includes a check for whether the player is grounded before allowing a jump. Here’s an example of how to implement this:

    void Jump()
    {
        if (Grounded())
        {
            airJumpCounter = 0; // Reset air jump counter when on ground
        }
    
        if (Input.GetButtonDown("Jump") && Grounded() && airJumpCounter < maxAirJumps)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            airJumpCounter++; // Increment air jump counter
        }
    }

    This ensures that the player can only jump if they are grounded or if they have remaining air jumps available.

    Do you have a specific maximum number of air jumps set, and is it being properly initialized?

    #17864
    insects studios
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    what wrong here for infinite jumping?

    public bool Grounded()
    {
        if (Physics2D.Raycast(groundCheckPoint.position, Vector2.down, groundCheckY, whatIsGround)
            || Physics2D.Raycast(groundCheckPoint.position + new Vector3(groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround)
            || Physics2D.Raycast(groundCheckPoint.position + new Vector3(-groundCheckX, 0, 0), Vector2.down, groundCheckY, whatIsGround))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    void Jump()
    {
        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, 0);
        }
    
        if (Input.GetButtonDown("Jump") && Grounded())
        {
            rb.velocity = new Vector3(rb.velocity.x, jumpForce);
        }
    }
    #17865
    Ser Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    The issue with infinite jumping likely stems from how the Grounded() function is being used in conjunction with your jump logic.

    1. Ensure Ground Detection is Accurate: Make sure that the Grounded() function is correctly detecting when the player is on the ground. If the raycasts are not hitting the ground layer correctly, it will always return true, allowing for infinite jumps.

    2. Reset Air Jump Counter: If you are implementing air jumps, ensure that you reset the air jump counter when the player lands. This is not shown in your provided code.

    3. Check Jump Logic: In your Jump() method, ensure that you are only allowing jumps when the player is grounded or has remaining air jumps.

    Here’s an updated example of how you might structure your Jump() method:

    void Jump()
    {
      if (Grounded())
      {
          airJumpCounter = 0; // Reset air jump counter when grounded
      }
    
      if (Input.GetButtonDown("Jump") && (Grounded() || airJumpCounter < maxAirJumps))
      {
          rb.velocity = new Vector2(rb.velocity.x, jumpForce);
          if (!Grounded()) // Increment air jump counter only if not grounded
          {
              airJumpCounter++;
          }
      }
    }

    Make sure you have initialized maxAirJumps to a value greater than 0 if you want to allow air jumps.

    Is your maxAirJumps variable set correctly, and are you resetting the air jump counter when the player lands?

    #17866
    insects studios
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    it says ‘airjumpcounter’ does not exist in you current context ‘maxairjumps’ does not exist in you current context

    #17867
    Ser Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It seems that the variables airJumpCounter and maxAirJumps are not defined in the scope of your Jump() method. You need to ensure that these variables are declared as class-level fields in your script.

    Here’s how you can declare them:

    private int airJumpCounter = 0; // Keeps track of how many air jumps have been made
    [SerializeField] private int maxAirJumps = 1; // Set this to the maximum number of air jumps allowed

    Make sure these declarations are placed outside of any methods, typically at the top of your class.

    After adding these declarations, your Jump() method should be able to access airJumpCounter and maxAirJumps without any issues.

    Do you have these variables declared in your class, and are they accessible in the Jump() method?

    #17868
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    @yentlblanckaert don’t follow Ser’s suggestion for the jump issue first.

    Check out this topic to see whether it fixes your problem: https://blog.terresquall.com/community/topic/part-1-jump-continues-to-trigger-in-midair/

    #17910
    insects studios
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    why does my camera move away from the player?

    #17911
    Ser Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    The camera may be moving away from the player due to the offset value you set in your CameraFollow script. If the offset is too large or incorrectly configured, it can cause the camera to appear distant from the player.

    Make sure that the offset variable is set correctly in the Inspector. For example, if you want the camera to follow the player closely, you might set the z value of the offset to -10 and adjust the x and y values as needed.

    Here’s a reminder of how you can set the offset:

    [SerializeField] private Vector3 offset; // Set this in the Inspector

    If the offset is set correctly and the camera still moves away, check if there are any other scripts or settings affecting the camera’s position.

    Could you provide the current values of your offset in the Inspector?

    #17912
    insects studios
    Level 1
    Participant
    Helpful?
    Up
    0
    ::

    my offset is good ist just my camera followscript is good but when i press play it moves a little but up and i cant see my player

    #17913
    Ser Apustaja
    Level 5
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It sounds like there may be an issue with how the camera’s position is being updated in your CameraFollow script. The camera should smoothly follow the player’s position based on the offset you set.

    Make sure your Update() method in the CameraFollow script looks like this:

    void Update() 
    {
        transform.position = Vector3.Lerp(transform.position, PlayerController.Instance.transform.position + offset, followSpeed);
    }

    If the camera is moving up unexpectedly, double-check the values of your offset variable, especially the y component. If the y value is too high, it could cause the camera to move up and out of view of the player.

    Is the y value of your offset set correctly in the Inspector? What values are you using for the offset?

Viewing 15 posts - 1 through 15 (of 22 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: