AnyPortrait > Scripting > Physic Effect

Physic Effect

Physics Modifier's physics effects can be controlled by script.

You can turn physical effects on or off, and you can further control external force and pull.

public void SetPhysicEnabled(bool isPhysicEnabled)

​Description

Turn physical effects on or off.

Parameters

bool isPhysicEnabled : Whether the physics modifier is processed

public apForceUnit AddForce_Point(Vector2 pointPosW, float radius)

​Description

Add a radial force based on the point.

It returns apForceUnit, then you have to add properties.

Parameters

Vector2 pointPosW : The position where the force is applied (World coordinate system)

float radius : Radius of radial force

Return

apForceUnit : Generated force

public apForceUnit AddForce_Direction(Vector2 directionW)

​Description

Adds a directional force.

It returns apForceUnit, then you have to add properties.

Parameters

Vector2 directionW : The direction in which the force is applied (World coordinate system)

Return

apForceUnit : Generated force

public apForceUnit AddForce_Direction(Vector2 directionW,

                                                     float waveSizeX,

                                                     float waveSizeY,

                                                     float waveTimeX,

                                                     float waveTimeY)

​Description

Adds a directional force.

It has a repetitive directional change that draws a sin curve with respect to the X and Y axes, making it suitable for expressions like wind.

It returns apForceUnit, then you have to add properties.

Parameters

Vector2 directionW : The direction in which the force is applied (World coordinate system)

float waveSizeX : The maximum X value in the periodically changing direction

float waveSizeY : The maximum Y value in the periodically changing direction

float waveTimeX : The cycle in which the direction changes about the X axis

float waveTime : The cycle in which the direction changes about the Y axis

Return

apForceUnit : Generated force

public bool IsAnyForceEvent

​Description

Make sure that there is currently added force.

Return

bool : Whether there is added forces

public Vector2 GetForce(Vector2 targetPosW)

​Description

The calculated force is measured at the input position.

Parameters

Vector2 targetPosW : Position to be measured (World coordinate system)

Return

Vector2 : Measured force vector

public void ClearForce()

​Description

Eliminate all the forces you have added with the AddForce function.

public apPullTouch AddTouch(Vector2 posW, float radius)

​Description

Start "pull" by touch input.

Parameters

Vector2 posW : Touch position (World coordinate system)

float radius : Radius to be pulled

Return

apPullTouch : Objects distinguished by unique IDs with information about "pull by touch"

public void SetTouchPosition(int touchID, Vector2 posW)

​Description

Updates the current position of the touch corresponding to the apPullTouch returned after the call to AddTouch.

Select using apPullTouch.TouchID.

Parameters

int touchID : ID corresponding to the TouchID of apPullTouch returned from AddTouch

Vector2 posW : The position of the current touch

public void SetTouchPosition(apPullTouch touch, Vector2 posW)

​Description

Updates the current position of the touch corresponding to the apPullTouch returned after the call to AddTouch.

Select apPullTouch by entering parameters.

Parameters

apPullTouch touch, : The apPullTouch returned from AddTouch

Vector2 posW : The position of the current touch

public bool IsAnyTouchEvent

​Description

Returns if there is a touch currently added.

Return

bool : Whether there is an added touch

public apPullTouch GetTouch(int touchID)

​Description

Returns an apPullTouch object with an ID corresponding to touchID.

Parameters

int touchID : ID of touch event to find

Return

apPullTouch : "Pull touch event" with the requested TouchID (null return if not exist)

public void RemoveTouch(int touchID)

​Description

Removes the touch event object that is calculating the pulling.

Parameters

int touchID : ID corresponding to the TouchID of apPullTouch returned from AddTouch

public void ClearTouch()

​Description

Remove all touch events.

public void ClearForceAndTouch()

​Description

Remove all forces and touch events.

< apForceUnit >

If you use the AddForce function, the force is not applied.

You need to include additional information in the return value of the AddForce function, apForceUnit.

You must call one of the SetPower functions and call either EmitLoop or EmitOnce.

public apForceUnit SetPower(float power)

: Add power.

public apForceUnit SetPower(float power, float waveSize, float waveTime)

 : Adds a force that changes periodically by the amount of waveTime times the size of waveSize based on power.

public apForceUnit EmitLoop()

: Set as a sustained force.

public apForceUnit EmitOnce(float liveTime)

: Set as a power to disappear after the time of liveTime.

using UnityEngine;
using AnyPortrait;

public class PhysicsTest : MonoBehaviour
{
           // Target AnyPortrait Object
           public apPortrait portrait;

           // Touch ID
           private int _touchID;


           void Start () { }
    
           void Update ()
           {
                      //----------------------------------------
                      // Force Events
                      //----------------------------------------
                      // Clear Force

                      if(Input.GetKeyDown(KeyCode.C))
                      {
                                 portrait.ClearForce();

                                 Debug.Log("Clear Force");
                      }

        

                      // Add Point Force
                      if(Input.GetKeyDown(KeyCode.P))
                      {
                                 // Position : (0, 0)
                                 // Radius : 10
                                 // Power : 2000
                                 // Live Time : 10 Sec

                                 portrait.AddForce_Point(Vector2.zero, 10.0f)
                                            .SetPower(2000)
                                            .EmitOnce(10);

                                 Debug.Log("Add Point Force");
                      }

 

                      //Add Waved Directional Force
                      if(Input.GetKeyDown(KeyCode.D))
                      {
                                 // Direction : (1 +- 0.5, 0.5 +- 0.2)
                                 // Power : 4000 +- 1000
                                 // Loop

                                 portrait.AddForce_Direction(new Vector2(1.0f, 0.5f), 0.5f, 0.2f, 5, 10)
                                            .SetPower(4000, 1000, 25)
                                            .EmitLoop();

                                 Debug.Log("Add Waved Directional Force");
                      }

 

                      //----------------------------------------
                      // Touch Events
                      //----------------------------------------

                      Vector3 touchPosW = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
                      Vector2 touchPosW2 = new Vector2(touchPosW.x, touchPosW.y);

 

                      //Left Click -> Touch
                      if(Input.GetMouseButtonDown(0))
                      {
                                 //Start Touch Event
                                 _touchID = portrait.AddTouch(touchPosW2, 2.0f).TouchID;

                                 Debug.Log("Add Touch (" + _touchID + ")");
                      }
                      else if(Input.GetMouseButton(0))
                      {
                                 //Refresh Touch Position
                                 portrait.SetTouchPosition(_touchID, touchPosW2);
                      }
                      else if(Input.GetMouseButtonUp(0))
                      {
                                 //Release Touch Event
                                 portrait.ClearTouch();
                                 _touchID = -1;
                      }
        
           }
}