Skip to content

Instantly share code, notes, and snippets.

@kankikuchi
Created January 8, 2016 04:50
Show Gist options
  • Save kankikuchi/251b0219a972b0692ce8 to your computer and use it in GitHub Desktop.
Save kankikuchi/251b0219a972b0692ce8 to your computer and use it in GitHub Desktop.
Time.timescaleを変更せずにRigidbodyの一時停止&再開処理【Unity】【拡張メソッド】
// Rigidbody2DExtension.cs
// http://kan-kikuchi.hatenablog.com/entry/Pause_Resume
//
// Created by kan kikuchi on 2015.11.26.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//一時停止時の速度を保管するクラス
public class Velocity2DTmp : MonoBehaviour{
//一時停止時の速度
private float _angularVelocity;
private Vector2 _velocity;
public float AngularVelocity{
get{return _angularVelocity;}
}
public Vector2 Velocity{
get{return _velocity;}
}
/// <summary>
/// Rigidbody2Dを入力して速度を設定する
/// </summary>
public void Set(Rigidbody2D rigidbody2D){
_angularVelocity = rigidbody2D.angularVelocity;
_velocity = rigidbody2D.velocity;
}
}
/// <summary>
/// Rigidbody2D 型の拡張メソッドを管理するクラス
/// </summary>
public static class Rigidbody2DExtension {
/// <summary>
/// 一時停止
/// </summary>
public static void Pause(this Rigidbody2D rigidbody2D, GameObject gameObject){
gameObject.AddComponent<Velocity2DTmp> ().Set(rigidbody2D);
rigidbody2D.isKinematic = true;
}
/// <summary>
/// 再開
/// </summary>
public static void Resume(this Rigidbody2D rigidbody2D, GameObject gameObject){
if(gameObject.GetComponent<Velocity2DTmp> () == null){
return;
}
rigidbody2D.velocity = gameObject.GetComponent<Velocity2DTmp>().Velocity;
rigidbody2D.angularVelocity = gameObject.GetComponent<Velocity2DTmp>().AngularVelocity;
rigidbody2D.isKinematic = false;
GameObject.Destroy (gameObject.GetComponent<Velocity2DTmp> ());
}
}
// RigidbodyExtension.cs
// http://kan-kikuchi.hatenablog.com/entry/Pause_Resume
//
// Created by kan kikuchi on 2015.11.26.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//一時停止時の速度を保管するクラス
public class VelocityTmp : MonoBehaviour{
//一時停止時の速度
private Vector3 _angularVelocity;
private Vector3 _velocity;
public Vector3 AngularVelocity{
get{return _angularVelocity;}
}
public Vector3 Velocity{
get{return _velocity;}
}
/// <summary>
/// Rigidbody2Dを入力して速度を設定する
/// </summary>
public void Set(Rigidbody rigidbody){
_angularVelocity = rigidbody.angularVelocity;
_velocity = rigidbody.velocity;
}
}
/// <summary>
/// Rigidbody 型の拡張メソッドを管理するクラス
/// </summary>
public static class RigidbodyExtension {
//一時停止時の速度
private static Vector3 _angularVelocity;
private static Vector3 _velocity;
/// <summary>
/// 一時停止
/// </summary>
public static void Pause(this Rigidbody rigidbody, GameObject gameObject){
gameObject.AddComponent<VelocityTmp> ().Set(rigidbody);
rigidbody.isKinematic = true;
}
/// <summary>
/// 再開
/// </summary>
public static void Resume(this Rigidbody rigidbody, GameObject gameObject){
if(gameObject.GetComponent<VelocityTmp> () == null){
return;
}
rigidbody.velocity = gameObject.GetComponent<VelocityTmp>().Velocity;
rigidbody.angularVelocity = gameObject.GetComponent<VelocityTmp>().AngularVelocity;
rigidbody.isKinematic = false;
GameObject.Destroy (gameObject.GetComponent<VelocityTmp> ());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment