OnCollisionStay still called after my character leave the floor,
Hi everyone,
So I'm using unity 2d (platformer type), and i want to modify the animation of my character when he is leaving the floor (by a jump, i change the vertical velocity of the rigidbody). But when i play frame by frame, with some Debug.Log, i see the method OnCollisionStay2D is called at the same frame i see my character over the floor. I thought the velocity just applies to "transform" after all the scripts just before we see the changes in the new frame, but i also noticed that the velocity was set at the frame before but didn't change the "transform", so the application of the transform must occures before the scripts.
So my questions are, if you know how to resolve it, and if you don't, do you know when does the velocity changes the transform position ?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FrogBehavior : MonoBehaviour
{
[SerializeField] private float jumpVelocity = 400f;
[Range(0, .4f)] [SerializeField] private float smoothAir = .05f;
[SerializeField] private float speedGround = 40;
[Range(0, 1f)] [SerializeField] private float speedAir = 0.6f;
private BoxCollider2D boxCollider;
private Rigidbody2D Rigidbody;
private Animator animator;
private Vector2 targetVelocity;
private bool wasGrounded = false;
private bool collided = false; // Si il touche quelquechose
public bool grounded;
public bool facingRight = false;
private List<ContactPoint2D> contacts = new List<ContactPoint2D>();
private Vector2 vectorZero = Vector2.zero;
private bool jump = false;
public float horizontal = 0f;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
Rigidbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
Debug.Log("update");
bool wasGrounded = grounded;
grounded = false;
if (collided)
{
foreach (ContactPoint2D contact in contacts) // vérifie pour chaque point si en dessous de hitbox
{
if (contact.normal.normalized == new Vector2(0, 1))
{
grounded = true;
animator.SetBool("grounded", true);
break;
}
}
}
if (!grounded)
animator.SetBool("grounded", false);
Move();
animator.SetFloat("verticalSpeed", Rigidbody.velocity.y);
}
public void Move()
{
targetVelocity = Rigidbody.velocity;
if (grounded)
{
if (jump) // jump apres horizontal pour eviter bug avec targetVelocity
{
Rigidbody.velocity = new Vector2(0, 0);
Rigidbody.velocity = new Vector2(horizontal * speedGround, jumpVelocity);
jump = false;
Debug.Log("jump");
}
else
{
Rigidbody.velocity = new Vector2(0, Rigidbody.velocity.y);
Debug.Log("not jump");
}
}
else // si !grounded
{
targetVelocity.x = horizontal * speedGround * speedAir;
Rigidbody.velocity = Vector2.SmoothDamp(Rigidbody.velocity, targetVelocity, ref vectorZero, smoothAir);
}
if (horizontal > 0 && !facingRight) // Flip
Flip();
else if (horizontal < 0 && facingRight)
Flip();
}
public void Jump()
{
jump = true;
Debug.Log("jump");
}
private void Flip()
{
facingRight = !facingRight;
transform.Rotate(0, 180f, 0);
}
private void OnCollisionEnter2D(Collision2D col)
{
contacts.Clear();
foreach (ContactPoint2D contact in col.contacts)
contacts.Add(contact);
collided = true;
Debug.Log("OnCollisionEnter2D");
}
private void OnCollisionStay2D(Collision2D col)
{
contacts.Clear();
foreach (ContactPoint2D contact in col.contacts)
contacts.Add(contact);
collided = true;
Debug.Log("OnCollisionStay2D");
}
private void OnCollisionExit2D(Collision2D col)
{
collided = false;
Debug.Log("OnCollisionExit2D");
}
private void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Vide")
transform.position = GameObject.Find("SpawnPointP1").transform.position;
}
}
To explain the code : "horizontal" is changed by another script, and "Jump()" is called when i want by the animator, this works well. The rigidbody's properties are Collision detection : continuous and Interpolate : interpolate but changing theme doesn't solve the problem. Tell me please if something is not understandable in my script. ,
Your answer
Follow this Question
Related Questions
Bounce, gravity and velocity 1 Answer
Why Does Bounciness Affects Rigidbody Velocity? (SOLVED) 0 Answers
How to reflect object's direction when collides wall ? 0 Answers
C# overriding past velocity 0 Answers