Player Controls
This commit is contained in:
21
Assets/Game/Scripts/Minigame/CameraFollower.cs
Normal file
21
Assets/Game/Scripts/Minigame/CameraFollower.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class CameraFollower : MonoBehaviour
|
||||
{
|
||||
public bool autoOffset = true;
|
||||
public Vector3 offset;
|
||||
public Transform target;
|
||||
public float smoothness = 0.1f;
|
||||
void Start()
|
||||
{
|
||||
offset = transform.position - target.position;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
transform.position = Vector3.Lerp(transform.position, target.position + offset, smoothness * Time.deltaTime);
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/Minigame/CameraFollower.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/CameraFollower.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b20032ec7edc76b0ca5b2759bc19363f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
79
Assets/Game/Scripts/Minigame/Joystick.cs
Normal file
79
Assets/Game/Scripts/Minigame/Joystick.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class Joystick : MonoBehaviour
|
||||
{
|
||||
public bool DissappearOnEnd = true;
|
||||
private Vector3 defaultPosition;
|
||||
public bool touchDown;
|
||||
public bool autoConfigMaxDist;
|
||||
public float maxDist = 100;
|
||||
public Vector2 input;
|
||||
private Vector2 deltaPos;
|
||||
private Vector2 startPos;
|
||||
private Vector2 endPos;
|
||||
public RectTransform joyBG;
|
||||
public RectTransform joyStick;
|
||||
|
||||
void Start(){
|
||||
defaultPosition = joyBG.position;
|
||||
}
|
||||
|
||||
public void onDrag(BaseEventData pointer)
|
||||
{
|
||||
PointerEventData ped = pointer as PointerEventData;
|
||||
endPos = ped.position;
|
||||
deltaPos = endPos - startPos;
|
||||
|
||||
deltaPos = new Vector2(Mathf.Clamp(deltaPos.x, -maxDist, maxDist), Mathf.Clamp(deltaPos.y, -maxDist, +maxDist));
|
||||
|
||||
Vector2 _input = new Vector2(deltaPos.x / maxDist, deltaPos.y / maxDist);
|
||||
|
||||
if (_input.magnitude < 0.1f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
input = _input;
|
||||
|
||||
input = (input.magnitude > 1.0f) ? input.normalized : input;
|
||||
|
||||
joyStick.localPosition = new Vector2(input.x * (joyBG.sizeDelta.x / 2f), input.y * (joyBG.sizeDelta.y / 2f));
|
||||
}
|
||||
|
||||
public void pointerDown(BaseEventData pointer)
|
||||
{
|
||||
PointerEventData ped = pointer as PointerEventData;
|
||||
startJoy(ped);
|
||||
}
|
||||
|
||||
public void pointerUp()
|
||||
{
|
||||
endJoy();
|
||||
}
|
||||
|
||||
|
||||
void startJoy(PointerEventData pointer)
|
||||
{
|
||||
startPos = pointer.position;
|
||||
joyBG.gameObject.SetActive(true);
|
||||
joyStick.gameObject.SetActive(true);
|
||||
joyStick.localPosition = Vector2.zero;
|
||||
joyBG.position = startPos;
|
||||
|
||||
touchDown = true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
void endJoy()
|
||||
{
|
||||
input=Vector2.zero;
|
||||
joyBG.position = defaultPosition;
|
||||
joyStick.localPosition = Vector2.zero;
|
||||
joyBG.gameObject.SetActive(!DissappearOnEnd);
|
||||
joyStick.gameObject.SetActive(!DissappearOnEnd);
|
||||
touchDown = false;
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/Minigame/Joystick.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/Joystick.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d8b30d04f7d1f44cd9ed38986120d33c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
42
Assets/Game/Scripts/Minigame/PlayerController.cs
Normal file
42
Assets/Game/Scripts/Minigame/PlayerController.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
public Transform body;
|
||||
public float movingSpeed = 0.1f;
|
||||
public float turningSmoothFactor = 0.1f;
|
||||
public Joystick joystick;
|
||||
void Start()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void FixedUpdate()
|
||||
{
|
||||
body.Translate(new Vector3(0, movingSpeed), body);
|
||||
if (joystick.input != Vector2.zero)
|
||||
{
|
||||
//Turn
|
||||
var angle1 = Mathf.Atan2(-joystick.input.y, -joystick.input.x) * Mathf.Rad2Deg;
|
||||
body.rotation = Quaternion.Lerp(body.rotation, Quaternion.AngleAxis(angle1 + 90, Vector3.forward), turningSmoothFactor * joystick.input.magnitude);
|
||||
}
|
||||
}
|
||||
|
||||
public float Angle(Vector2 vector2)
|
||||
{
|
||||
return 360 - (Mathf.Atan2(vector2.x, vector2.y) * Mathf.Rad2Deg * Mathf.Sign(vector2.x));
|
||||
}
|
||||
|
||||
|
||||
//Auto assign default variables [Editor only]
|
||||
void OnValidate()
|
||||
{
|
||||
if (body == null)
|
||||
{
|
||||
body = transform;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Game/Scripts/Minigame/PlayerController.cs.meta
Normal file
11
Assets/Game/Scripts/Minigame/PlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f9292398ab503e2abb1f0dd8acf58f9b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user