• 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 Reality-Gaming-Studio · Feb 05, 2016 at 10:51 AM · unity 52dmobileoptimizationphysics2d

Physics2D Optimize

Hi I'm working on 2D shooting game, I'm having problem of performance. I see the profiler, performance problem cause because of Physics.UpdateBodies. Physics2D.ColliderTransformChanged. Physics2D.ColliderTransformPositionRotationChanged. Physics2D.PolygonColliderCreate. Physics2D.ColliderCleanup.

How to optimize?

Comment
Add comment · Show 2
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
avatar image Taxen0 · Feb 05, 2016 at 03:01 PM 0
Share

It would help to see the relevant code, but off the top of my head: $$anonymous$$ake sure you don't move any static colliders. If a object should be moved and have a collider make sure you also give it a RidgidBody2D (make it kinematic if you don't want physics)

avatar image Reality-Gaming-Studio Taxen0 · Feb 06, 2016 at 02:37 AM 0
Share

Already did kinematic, but still having framedrop

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Reality-Gaming-Studio · Feb 06, 2016 at 02:38 AM

Here's the code: Bullet

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(Rigidbody2D))]
 public class UbhNonKinematic2D : UbhMonoBehaviour
 {
 #if UNITY_4_3
     Vector3 _FixedPosition = Vector3.zero;
     float _OrgGravityScale;
 #endif
 
     void Awake ()
     {
 #if !UNITY_4_3
         rigidbody2D.isKinematic = true;
         enabled = false;
         return;
 #else
         _FixedPosition = transform.localPosition;
 #endif
     }
 
     void Update ()
     {
 #if !UNITY_4_3
         enabled = false;
         return;
 #else
         if (rigidbody2D == null) {
             return;
         }
         rigidbody2D.velocity = Vector3.zero;
         rigidbody2D.angularVelocity = 0;
         transform.localPosition = _FixedPosition;
 #endif
     }
 
     void OnEnable ()
     {
 #if !UNITY_4_3
         enabled = false;
         return;
 #else
         if (rigidbody2D == null) {
             return;
         }
         _OrgGravityScale = rigidbody2D.gravityScale;
         rigidbody2D.gravityScale = 0f;
 #endif
     }
 
     void OnDisable ()
     {
 #if !UNITY_4_3
         return;
 #else
         if (rigidbody2D == null) {
             return;
         }
         rigidbody2D.gravityScale = _OrgGravityScale;
 #endif
     }
 }

Enemy

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(UbhSpaceship))]
 public class UbhEnemy : UbhMonoBehaviour
 {
     public const string NAME_PLAYER = "Player";
     public const string NAME_PLAYER_BULLET = "PlayerBullet";
     const string ANIM_DAMAGE_TRIGGER = "Damage";
     [SerializeField]
     int _Hp = 1;
     [SerializeField]
     int _Point = 100;
     [SerializeField]
     bool _UseStop = false;
     [SerializeField]
     float _StopPoint = 2f;
     UbhSpaceship _Spaceship;
 
     void Start ()
     {
         _Spaceship = GetComponent<UbhSpaceship>();
 
         Move(transform.up.normalized * -1);
     }
 
     void FixedUpdate ()
     {
         if (_UseStop) {
             if (transform.position.y < _StopPoint) {
                 rigidbody2D.velocity = Vector2.zero;
                 _UseStop = false;
             }
         }
     }
 
     public void Move (Vector2 direction)
     {
         rigidbody2D.velocity = direction * _Spaceship._Speed;
     }
 
     void OnTriggerEnter2D (Collider2D c)
     {
         // *It is compared with name in order to separate as Asset from project settings.
         //  However, it is recommended to use Layer or Tag.
         if (c.name.Contains(NAME_PLAYER_BULLET)) {
             UbhSimpleBullet bullet = c.transform.parent.GetComponent<UbhSimpleBullet>();
 
             UbhObjectPool.Instance.ReleaseGameObject(c.transform.parent.gameObject);
 
             _Hp = _Hp - bullet._Power;
 
             if (_Hp <= 0) {
                 FindObjectOfType<UbhScore>().AddPoint(_Point);
 
                 _Spaceship.Explosion();
 
                 Destroy(gameObject);
             } else {
                 _Spaceship.GetAnimator().SetTrigger(ANIM_DAMAGE_TRIGGER);
             }
         }
     }
 }
Comment
Add comment · Share
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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

76 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

Related Questions

Mobile Optimization question 1 Answer

Problem when build terrain on android device 0 Answers

Forcibly halt movement of slow rigidbodies, but not falling ones. (2D) 1 Answer

Physics2D Optimize Physics.UpdateBodies. 0 Answers

How can i change 3d model to isometric 2d image? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges