95 lines
2.9 KiB
C#
95 lines
2.9 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
|
|
public class GameManager : MonoBehaviour
|
|
{
|
|
public Rigidbody2D ball;
|
|
public Transform cam;
|
|
public Vector3 camTargetPos;
|
|
public float cameraSmoothness = 0.1f;
|
|
public float inputSensitivity = 100f;
|
|
public float BallFriction = 0.1f;
|
|
public float StopVelocity = 0.01f;
|
|
public float StopTime = 0.1f;
|
|
|
|
public float curVelocity;
|
|
public float forceMultiplier;
|
|
public Transform ballProjection;
|
|
public float ballProjectionScaleMin, ballProjectionScaleMax;
|
|
|
|
void Start()
|
|
{
|
|
camTargetPos = ball.transform.position;
|
|
}
|
|
float stopCooldown = 0;
|
|
|
|
void FixedUpdate(){
|
|
curVelocity = ball.velocity.magnitude;
|
|
ball.velocity = Vector2.Lerp(ball.velocity, new Vector2(0, ball.velocity.y), BallFriction);
|
|
if(Mathf.Abs(ball.velocity.magnitude) < StopVelocity){
|
|
if(stopCooldown > StopTime){
|
|
ball.simulated=false;
|
|
}else{
|
|
stopCooldown+=Time.deltaTime;
|
|
}
|
|
}else{
|
|
stopCooldown=0;
|
|
}
|
|
}
|
|
|
|
void Update(){
|
|
camTargetPos = ball.transform.position;
|
|
}
|
|
|
|
void LateUpdate()
|
|
{
|
|
cam.position = Vector3.Lerp(cam.position, new Vector3(camTargetPos.x, cam.position.y,cam.position.z), cameraSmoothness);
|
|
}
|
|
|
|
|
|
bool dragging = false;
|
|
Vector2 startPos;
|
|
public void OnMouseDown(BaseEventData e){
|
|
if(ball.simulated){return;}
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
|
|
startPos = ped.position;
|
|
dragging = true;
|
|
}
|
|
|
|
public void OnMouseUp(BaseEventData e){
|
|
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
if(dragging){
|
|
Vector2 v = ((ped.position-startPos)/inputSensitivity);
|
|
if(v.magnitude > 1){v = v.normalized;}
|
|
stopCooldown=0;
|
|
|
|
ball.simulated=true;
|
|
ball.AddForce(-v * forceMultiplier);
|
|
}
|
|
dragging = false;
|
|
ballProjection.position = Vector3.zero;
|
|
}
|
|
|
|
public void OnMouseDrag(BaseEventData e){
|
|
if(ball.simulated){return;}
|
|
|
|
ballProjection.position = ball.position;
|
|
PointerEventData ped = (PointerEventData) e as PointerEventData;
|
|
Vector2 v = ((ped.position-startPos)/inputSensitivity);
|
|
Debug.Log(v.magnitude);
|
|
|
|
if(v.magnitude > 1){v = v.normalized;}
|
|
|
|
Vector3 direction = (ped.position - startPos).normalized;
|
|
float rot_z = Mathf.Atan2(direction.y,direction.x) * Mathf.Rad2Deg;
|
|
ballProjection.rotation = Quaternion.Euler(0,0,rot_z+180);
|
|
|
|
float scaleDiff = ballProjectionScaleMax - ballProjectionScaleMin;
|
|
ballProjection.GetChild(0).localScale = new Vector3(ballProjection.GetChild(0).localScale.x,ballProjectionScaleMin + (scaleDiff*v.magnitude));
|
|
}
|
|
}
|