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 21 replies, 3 voices, and was last updated 4 months, 2 weeks 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?
April 15, 2025 at 2:09 am #17914April 15, 2025 at 2:11 am #17916::If the camera is moving on its own when you press play, it could be due to several reasons. Here are a few things you can check:
-
Camera Follow Logic: Ensure that your camera follow logic is correctly implemented and not causing the camera to jump unexpectedly. If you’re using
Lerp
orSmoothDamp
, make sure the target position is being calculated correctly. -
Multiple Camera Scripts: Check if there are multiple scripts attached to the camera that might be affecting its position. Disable or remove any other scripts that could be influencing the camera’s movement.
-
Parenting Issues: Ensure that the camera is not a child of another object that is moving. If it is, it will inherit the movement of that parent object.
-
Player Position: Check if the player’s position is being altered in a way that causes the camera to react. For example, if the player starts at an unexpected position or is moving rapidly, the camera might follow it in a way that seems erratic.
-
Update Method: Check if you are using
FixedUpdate()
instead ofUpdate()
for your camera follow logic. The camera should ideally follow inUpdate()
to ensure smooth movement.
Here’s an example of a simple
CameraFollow
script:public class CameraFollow : MonoBehaviour { public Transform player; // Assign the player transform in the Inspector public Vector3 offset; // Assign offset in the Inspector public float followSpeed = 0.1f; void LateUpdate() { Vector3 targetPosition = player.position + offset; transform.position = Vector3.Lerp(transform.position, targetPosition, followSpeed); } }
Make sure to assign the
player
variable in the Inspector.Does any of this help clarify the issue? If the problem persists, please share your
CameraFollow
script for further assistance.April 15, 2025 at 2:17 am #17917::i used the last one but now the camera is not following the player. what do i need to type in player controller script?
April 15, 2025 at 2:21 am #17918::In your
PlayerController
script, you need to ensure that thePlayerController
instance is properly set up as a singleton so that theCameraFollow
script can access it. Here’s a reminder of how to implement the singleton pattern in yourPlayerController
:public class PlayerController : MonoBehaviour { public static PlayerController Instance; private void Awake() { if (Instance != null && Instance != this) { Destroy(gameObject); } else { Instance = this; } } // Other player movement code... }
Make sure this code is included in your
PlayerController
script. This will allow theCameraFollow
script to reference the player correctly.Additionally, ensure that you have assigned the
player
variable in theCameraFollow
script to reference thePlayerController
instance. You can do this in the Unity Inspector by dragging the player GameObject into theplayer
field of theCameraFollow
component.Is your
PlayerController
script set up correctly with the singleton pattern, and have you assigned the player reference in theCameraFollow
script?April 15, 2025 at 2:28 am #17919 -
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: