Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Self Made 2D Physics

Discussion in 'Scripting' started by Nigey, Jul 30, 2014.

  1. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Hi Guys,

    I have two objects, that both need to move to physics realistically. Object 'A' needs to bounce off object 'B', without object 'B' having their physics changed.

    I've tried stopping the velocity change in OnCollisionEnter(), but it looks like that's called after the physics update on the collision itself and there's no reference to the change in physics OnCollisionEnter().

    It looks like my only option is to set object 'B' as kinematic and create the physics myself. I've had a look around google for a few hours, but can't find any clean explanation on how to create physics movement.

    I'm thinking of creating something like this:

    Code (CSharp):
    1.     Vector3 velocity = new Vector3(0,0,0);
    2.     Vector3 gravity = new Vector3(0,-4.98f,0)
    3.     Vector3 externalForces = new Vector3(0,0,0);
    4.  
    5.     void FixedUpdate()
    6.     {
    7.         externalForces += gravity + anythingElse;
    8.         velocity += externalForces;
    9.  
    10.         rigidbody2D.MovePosition(transform.position + velocity * Time.deltaTime);
    but will be losing the smooth movement of the physics engine? Also asides drag and mass, is there anything that needs to be accounted for?
     
  2. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Also how would you handle 'jumping', as jumping is normally not an instantaneous 'up'. Those forces are normally handled over a (minuscule) amount of time.
     
  3. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    I've not used much physics with unity, but in reality, if you bounce a ball off the ground, the ground doesn't move because of its enormous mass. (or it moves so slightly it's inconceivable). So maybe set object B's mass to be a huge number?

    As for jump, looking at the docs, rigidbody.AddForce should do the trick
     
  4. der_r

    der_r

    Joined:
    Mar 30, 2014
    Posts:
    259
    I think the correct setup would be to set object B as kinematic and leave physics on for object A. Object A should still bounce off B, without B doing anything.
     
  5. lrlelaldl

    lrlelaldl

    Joined:
    Jul 27, 2014
    Posts:
    75
    May have misread but I think that OP was trying to make Object B still act to physics but not react to collisions with Object A.

    Have you tried setting a boolean on Object B when the collision with Object A has been registered, and then in the physics update checking for that boolean and negating any acceleration. If you wanted it to be perfect cancellation you could calculat the forces that were exerted onto Object B and remove them from the current forces that move it.

    With the above way, a normal AddForce would work