Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerController : MonoBehaviour {
- [SerializeField]
- private float _force = 1.5f;
- private Rigidbody2D _rb;
- private bool _isMouseOver, _lmbClickedOnPlayer;
- private Vector3 _mouseClickPosition;
- // Use this for initialization
- void Start () {
- _rb = GetComponent<Rigidbody2D> ();
- _lmbClickedOnPlayer = false;
- _isMouseOver = false;
- }
- // Update is called once per frame
- void Update () {
- }
- void OnMouseOver()
- {
- _isMouseOver = true;
- }
- void OnMouseExit()
- {
- _isMouseOver = false;
- }
- void OnMouseDown(){
- if (_isMouseOver)
- _lmbClickedOnPlayer = true;
- _mouseClickPosition = Input.mousePosition;
- }
- void OnMouseUp(){
- if (_lmbClickedOnPlayer)
- {
- Dash();
- }
- }
- private void Dash()
- {
- Debug.Log("Dash");
- _rb.bodyType = RigidbodyType2D.Dynamic;
- Vector3 mouseUpPosition = Input.mousePosition;
- Vector2 forceVector = CalculateCounterVector2D(_mouseClickPosition, mouseUpPosition) / 50;
- _rb.AddForce(forceVector, ForceMode2D.Impulse);
- }
- public void Cling(Vector2 position)
- {
- StartCoroutine(AdjustPosition(position));
- }
- private IEnumerator AdjustPosition(Vector2 position)
- {
- while (Vector2.Distance(_rb.position, position) > 0.01)
- {
- _rb.MovePosition(position);
- yield return new WaitForFixedUpdate();
- }
- _rb.bodyType = RigidbodyType2D.Static;
- yield return null;
- }
- Vector3 CalculateCounterVector2D(Vector3 origin, Vector3 end){
- Vector3 result = (origin - end)*_force;
- return new Vector2 (result.x, 2.5f * result.y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement