i use this code
rb.velocity = new Vector2(x, y);
but only the y axis work
any solutions?
i use this code
rb.velocity = new Vector2(x, y);
but only the y axis work
any solutions?
To give you a solution, we’d need to know what is wrong. A single line of code without any context isn’t going to show the problem unfortunately.
The only other option is guessing one of a dozen reasons why it might be happening.
Providing more information including what you’ve done to debug it so far would provide some insight.
ok sorry
so basically i want to launch the player if he collides with the hitbox of an enemy (aka get damaged)using this scrpit
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class damage_box : MonoBehaviour
{
public float x;
public float y;
public Rigidbody2D rb;
object pob;
public player_detection_system pls;
object obj;
// Start is called before the first frame update
void Start()
{
obj = pls.GetComponent<player_detection_system>();
pob = rb.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player"))
{
rb.velocity = new Vector2(x, y);
pls.health--;
}
}
}
the script launches him in the y axis but the x axis does not work
i hope you get the picture
When it collides (assuming it does) it’ll have that velocity (x/y) so it’ll always have that velocity unless it collides with something else or if you have another script modifying it or gravity is changing it. It’s impossible to debug this for you here so I presume you’ve done that already i.e. check if it’s only colliding once here etc.
The point of suggesting debugging is for you to look at your project, something nobody else can do. You say “it does not work” but you need to check what the velocity of the Rigidbody2D is, something you can see in the inspector on the Rigidbody2D component (in “Info”). Something is changing it from whatever X is to (presumably) zero. It’s not that it doesn’t work, it’s that you’re doing something to stop it, even if indirectly. No idea what that is from the above code.
hey i just discoverd that the mouvment script is the script messing this up when i remove it everything works like intended, it is the one that changes my x velocity to 0
this is the script that mouves the player
rb.velocity = new Vector2(direction*speed*Time.deltaTime,rb.velocity.y);
so how can i fixe it ?
How to fix it? Stop setting opposing values in two different scripts? For example, if you had a variable called “score” and you set it to “500” and in another script you set it to “1000” then there is no fix, it’s just bad code with a bug setting the same thing with conflicting values i.e this isn’t specific to velocity.
This is why you don’t normally set the velocity directly as if you do this in multiple places, you’re just stomping over yourself. You can use forces or just design it so that you’re not setting the velocity directly in multiple places.
If you haven’t already, I would highly suggest you follow a bunch of tutorials on how to deal with movement using Physics, there are plenty around.