Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

set y velocity to zero, when collision trigger

Discussion in 'Scripting' started by RWCD, Mar 18, 2014.

  1. RWCD

    RWCD

    Joined:
    Mar 10, 2014
    Posts:
    18
    Im making this game. Its a like a platformjump game except i use something like a bubble, so when the player hits one of the bubbles that are spawned, the player gets a addforce and the bubble gets destroyed. My problem is that, when i hit one of the bubbles and get the addforce on the player, the second bubble i hit gives me a way to big velocity beacuse the new force is added to the old force from the first bubble i hit. So i need some help with setting the velocity on the y axis to zero when it hits another bubble so it doesnt get a higher velocity than the addforce is giving it. Or if you guys have other suggestion than setting the velocity to zero i would be much appreciated.

    This is the code
    Code (csharp):
    1.         void OnTriggerEnter2D (Collider2D other)
    2.            
    3.         {
    4.            
    5.             if (other.gameObject.tag == "Player")
    6.                
    7.             {
    8.             Destroy(gameObject);
    9.             other.rigidbody2D.AddForce(new Vector3(0, 300, 0));
    10.  
    11.            
    12.         }
    13.        
    14.         }
    15.        
    16.     }
    17.  
     
  2. RWCD

    RWCD

    Joined:
    Mar 10, 2014
    Posts:
    18
    Anyone?
     
  3. rrh

    rrh

    Joined:
    Jul 12, 2012
    Posts:
    331
  4. RWCD

    RWCD

    Joined:
    Mar 10, 2014
    Posts:
    18
    Any chance that you could write a script exampel, i dont know how to put i all up

    /RWCD
     
  5. RWCD

    RWCD

    Joined:
    Mar 10, 2014
    Posts:
    18
    anyone who got any clue on how to do this?
     
  6. Kyle_WG

    Kyle_WG

    Joined:
    Feb 12, 2013
    Posts:
    8
    Hey RWCD, Simplest option is to set the velocity to 0 then apply force again.
    However if you have other factors (i.e. wind affecting the x axis) then this might become messy.
    Code (csharp):
    1.  
    2.         void OnTriggerEnter2D (Collider2D other)
    3.         {
    4.             if (other.gameObject.tag == "Player")
    5.             {
    6.                 Destroy(gameObject);
    7.                 other.rigidbody2D.velcocity = Vector2.zero;
    8.                 other.rigidbody2D.AddForce(new Vector2(0, 300));
    9.             }
    10.         }
    11.  
    Or what rrh is saying is to adjust the force added by the velocity the object is moving at already:
    Code (csharp):
    1.  
    2.         void OnTriggerEnter2D (Collider2D other)
    3.         {
    4.             if (other.gameObject.tag == "Player")
    5.             {
    6.                 Destroy(gameObject);
    7.                 // Might need some adjustments if you have other factors affecting but this is the idea.
    8.                 other.rigidbody2D.AddForce(new Vector2(0, 300 - other.rigidBody.velocity.y);
    9.             }
    10.         }
    11.  
     
    Last edited: Mar 19, 2014
  7. RWCD

    RWCD

    Joined:
    Mar 10, 2014
    Posts:
    18
    that works fine. But my problem is the x axis. it doesnt seem to be able to move on the x-axis until the gameObject reached the velocity.
    I want to get the force up and be able to go right and left while i get a force that pushing me in the y axis.
     
  8. Kyle_WG

    Kyle_WG

    Joined:
    Feb 12, 2013
    Posts:
    8
    What's your code for moving the x-axis?