Problem Movement with Rigidbody.velocity
As shown in the figure, the capsule continues to move in the +x direction (the direction of another collision object).
The problem is that gravity does not work properly in this case.
Rigidbody.Gravity was basically used. If there is no collision except for the floor, it works correctly.
Therefore, it is not a solution to use a high value of custom gravity.
As a result of analyzing Rigidbody.Info,
Move the Capsule in the +x and +y directions.(OK)
Collides with another object.(OK)
Rigidbody.velocity.x changes to zero. (OK)
Rigidbody.velocity.y changes close to zero. (Why?)
I ask for help like this due to lack of knowledge.
It can be awkward because I used a translator.
public class Movement : MonoBehaviour
{
Vector2 direction = Vector2.zero;
Vector3 velocity = Vector3.zero;
Rigidbody rb;
[SerializeField]
private float speed = 3f;
[SerializeField]
private bool isJumpPressed;
[SerializeField]
private float jumpHeight = 2f;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void FixedUpdate()
{
Move();
Jump();
}
private void Jump()
{
if (!isJumpPressed) return;
velocity.Set(velocity.x, jumpHeight, velocity.z);
rb.velocity = velocity;
}
private void Move()
{
velocity.Set(direction.x * speed, rb.velocity.y, 0f);
rb.velocity = velocity;
}
#region Input Events
public void OnMove(InputAction.CallbackContext value)
{
direction = value.ReadValue<Vector2>();
}
public void OnJump(InputAction.CallbackContext value)
{
if (value.performed)
isJumpPressed = true;
else
isJumpPressed = false;
}
#endregion
}
Answer by Lenny_N · Jan 15 at 06:07 PM
im new to coding and i dont rlly know much but i try putting a physics material on the wall and then turned it all to 0 and minimum and it worked fine exept when i walk into a wall i cant jump anymore until i walk into a wall again here my jumping script: public float Force;
private bool grounded = true;
void OnCollisionEnter(Collision collision)
{
grounded = true;
}
void OnCollisionExit(Collision other)
{
grounded = false;
}
void Update()
{
if(grounded == true)
{
if (Input.GetKeyDown("space"))
{
GetComponent<Rigidbody>().velocity = new Vector3(0, Force, 0);
}
}
if(grounded == false)
{
return;
}
}
Thank you for your answer. But this code is not available. Your code is simply considering jumping. This is completely different from the direction of the solution I want.
Your answer

Follow this Question
Related Questions
First Person Slide in direction of rigidbody movement and not look direction 1 Answer
Rigidbody2D.velocity Won't Move Object 3 Answers
Moving and Rotating Rigidbody in a responsive way 2 Answers
Rigidbody.velocity is rotating instead of moving my character? 0 Answers
How to move a game object to a position after selecting it 0 Answers