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

Physics not working the same in Unity PSM, so confused as to why. Simple rigidbody2d stuff...

Discussion in 'PSM' started by codejoy, Dec 18, 2014.

  1. codejoy

    codejoy

    Joined:
    Aug 17, 2012
    Posts:
    204
    So I am not trying to spam the forums, I just wrote this post last night:

    http://forum.unity3d.com/threads/bu...no-where-on-vita-works-fine-on-pc-etc.286222/

    and I thought that was the only issue, though after playing around with stuff in unity more it is REALLY broken, I am confused as to what I am doing wrong. Basic explination:

    For instance I have a simple sprite, with a Rigidbody2d physics on it. It has the following properties:

    Mass 1
    LinearDrag 0
    AngularDrag 0.05
    Gravity Scale 1
    Fixed Angle yes
    Interpolate NONE
    Sleeping Mode Start Awake
    Collision Detection Discrete


    Then in my code, I want to move this thing forward. I have a WalkForwardScript which is this:

    Code (CSharp):
    1. public class WalkForward : MonoBehaviour {
    2.  
    3.         public float speed = 300f;
    4.         public int dir = 1;
    5.         float forceX, forceY;
    6.         Vector2 maxVelocity=new Vector2(5,0);
    7.         // Use this for initialization
    8.         void Start () {
    9.             forceY=0;    
    10.         }
    11.      
    12.         // Update is called once per frame
    13.         void Update () {
    14.             if(rigidbody2D.velocity.x < maxVelocity.x)
    15.             {
    16.                 rigidbody2D.AddForce(new Vector2(speed * dir * Time.deltaTime, 0));
    17.             }
    18.          
    19.         }
    20. }
    21.  
    Pretty simple script. The speed is set at a high rate. ...i noticed the Update and Time.deltaTime behaving differently. in either case, if I run the regular unity (non PSM) this works...if I run the PSM version on the Vita or in the Editor, the sprites BARELY move forward, and in fact start floating up?

    I am beyond confused as to what is going on or different to cause this....would love any ideas in the right direction.

    In addition what is SUPER weird, when an enemy dies the spawn two prefabs: GIBS (body parts) and Coins.

    They are the same essentially... both are just sprites that when spawned have these values:

    Code (CSharp):
    1.     public override void Explode(){
    2.  
    3.         Destroy (gameObject);
    4.        
    5.         var t = transform;
    6.        
    7.         for (int i = 0; i < totalParts; i++) {
    8.             BodyPart clone = Instantiate(bodyPart, t.position, Quaternion.identity) as BodyPart;
    9.             clone.rigidbody2D.AddForce(Vector3.right * (Random.Range (-50, 50)));
    10.             clone.rigidbody2D.AddForce(Vector3.up * Random.Range(100, 400));
    11.             clone.rigidbody2D.AddTorque( Random.Range (-10,10));
    12.         }
    13.  
    14.         int totalCoins = Random.Range (MAXHEALTH * 2, MAXHEALTH * 5);
    15.         Debug.Log ("Total: " + totalCoins + " " + health);
    16.         for (int i = 0; i < totalCoins; i++) {
    17.             GameObject clone = Instantiate(collectible, t.position, Quaternion.identity) as GameObject;
    18.             clone.rigidbody2D.AddForce(Vector3.right * (Random.Range (-50, 50)));
    19.             clone.rigidbody2D.AddForce(Vector3.up * Random.Range(100, 400));
    20.             clone.rigidbody2D.AddTorque( Random.Range (-10,10));
    21.         }
    22.        
    23.  
    24.     }
    The body part does what is supposed to explodes out, spins and FALLS to the ground. The coins, float up like the other sprites... the only script they have really is a FadeScript (poorly named) :

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BodyPart : MonoBehaviour {
    5.  
    6.     private SpriteRenderer spriteRenderer;
    7.     private Color start;
    8.     private Color end;
    9.     private float t = 0.0f;
    10.     public float fadeSpeed=2f;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         spriteRenderer = GetComponent<SpriteRenderer> ();
    15.         start = spriteRenderer.color;
    16.         end = new Color (start.r, start.g, start.b, 0.0f);
    17.     }  
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         t += Time.deltaTime;
    22.         renderer.material.color = Color.Lerp (start, end, (t-fadeSpeed) / 2);
    23.         if (renderer.material.color.a <= 0.0)
    24.             Destroy (gameObject);
    25.     }
    26. }
    27.  
    No idea why one behaves with the physics and gravity properly and the others just slowly float up and don't move with their initial addforce and torques on cloning...
     
    Last edited: Dec 18, 2014
  2. jesusluvsyooh

    jesusluvsyooh

    Joined:
    Jan 10, 2012
    Posts:
    377
    Have you tried using FixedUpdate to see if the physics and rigid body sections speeds stay the same better than update, even if there is a time.deltatime ? ;)
    Add a frames per second gui script onto your scenes, it maybe that some shader is lagging the vita so bad it cannot run the update properly.

    As for your 'explode' spawning two, add an additional boolean in there

    private bool : hasExploded = false;
    publicoverridevoid Explode(){
    if(hasExploded == false)
    {
    hasExploded = true;
    rest of code
    }
     
  3. codejoy

    codejoy

    Joined:
    Aug 17, 2012
    Posts:
    204
    Well the odd thing is, in the unity psm version even in the Editor it is failling like this, and a lot of the time it is failing on a simple rigid body that has no update method and is just letting the physic engine handle it.

    Also what I meant is super wierd is when an enemy dies, two prefabs are spawned. One is GIBS , the other is Coins. This is correct the odd thing is the Gibs work correctly, they explode out fall to the ground tumble around. The coins (same as the gibs just different named prefab) spawn and don't fly out anywhere...and don't fall back to earth even though their gravity scale is 1 too.. just like the gibs.
     
  4. codejoy

    codejoy

    Joined:
    Aug 17, 2012
    Posts:
    204
    OMG I think I just figured it out, it has to do with the Apply Root Motion being checked, I unchecked it and things seem okay now.
     
    jesusluvsyooh likes this.