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
- This topic has 11 replies, 3 voices, and was last updated 5 days, 10 hours ago by
Terence.
-
AuthorPosts
-
April 8, 2025 at 6:49 pm #17858::
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’
April 8, 2025 at 6:56 pm #17859::It looks like you’re encountering two separate issues in your Unity project. Let’s address them one by one.
-
Rigidbody2D does not contain a definition for ‘velocity’: This error usually indicates that you’re trying to access the
velocity
property of aRigidbody2D
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 aRigidbody
. If you’re using the correct component and still getting the error, check for any possible naming conflicts in your project. -
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 ownVector2
class. To resolve this ambiguity, you can explicitly specify whichVector2
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!
April 8, 2025 at 7:05 pm #17860::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); } }
April 8, 2025 at 7:05 pm #17861::It looks like you’re using
Velocity
with an uppercase ‘V’, which is incorrect. In Unity, the correct property isvelocity
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
withrb.velocity
.If you have any further questions or need more clarification, feel free to ask!
April 8, 2025 at 7:30 pm #17862April 8, 2025 at 7:30 pm #17863::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?
April 8, 2025 at 7:34 pm #17864::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); } }
April 8, 2025 at 7:35 pm #17865::The issue with infinite jumping likely stems from how the
Grounded()
function is being used in conjunction with your jump logic.-
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 returntrue
, allowing for infinite jumps. -
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.
-
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?April 8, 2025 at 7:42 pm #17866::it says ‘airjumpcounter’ does not exist in you current context ‘maxairjumps’ does not exist in you current context
April 8, 2025 at 7:45 pm #17867::It seems that the variables
airJumpCounter
andmaxAirJumps
are not defined in the scope of yourJump()
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 accessairJumpCounter
andmaxAirJumps
without any issues.Do you have these variables declared in your class, and are they accessible in the
Jump()
method?April 8, 2025 at 11:20 pm #17868::@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/
April 15, 2025 at 1:51 am #17910April 15, 2025 at 1:56 am #17911::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?
April 15, 2025 at 2:01 am #17912::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
April 15, 2025 at 2:05 am #17913::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 theCameraFollow
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?
-
-
AuthorPosts
- You must be logged in to reply to this topic.