Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by gagota763 · Aug 09, 2019 at 10:04 PM · collisionvelocity

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. ,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

231 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Stuck on using if else to prevent ball from getting stuck in brick breaker. very new to unity & C# :(,How would I reference an object to add onto its velocity in 2D? 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges