Golf2D/Assets/Scripts/GameManager.cs
2023-02-10 16:04:36 +05:30

72 lines
1.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;
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){
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;
stopCooldown=0;
ball.simulated=true;
ball.AddForce(-v);
}
dragging = false;
}
public void OnMouseDrag(BaseEventData e){
}
}