summaryrefslogtreecommitdiff
path: root/Mage/Assets/Test/PhysicsExample/Scripts/SceneSpecific/Pendulum_Force.cs
blob: af2e04e188c61e2f3443c4951312376c9f97b067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using UnityEngine;

public class Pendulum_Force : MonoBehaviour
{
    public float TangentForce = 1.0f;
    public Rigidbody2D PivotBody;
    private Rigidbody2D BallBody;

    void Start()
    {
        BallBody = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        var clockwise = Input.GetKeyDown(KeyCode.LeftArrow);
        var anticlockwise = Input.GetKeyDown(KeyCode.RightArrow);

        var constraint = (PivotBody.position - BallBody.position).normalized;

        if (clockwise)
        {
            var force = new Vector2(-constraint.y, constraint.x) * TangentForce;
            BallBody.AddForce(force, ForceMode2D.Impulse);
        }

        if (anticlockwise)
        {
            var force = new Vector2(constraint.y, -constraint.x) * TangentForce;
            BallBody.AddForce(force, ForceMode2D.Impulse);
        }
    }
}