Hello,
I am developing a midcore game inspired by Horizon, with a grinding flow as Monster Hunter or Diablo.
I have just implemented:
- click to move
- double click to jump
It doesn’t work perfectly, specially on camera switch:
and I really don’t know perfectly why, I leave you the code:
using System;
using System.Collections;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
[RequireComponent(typeof (ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
{
[SerializeField] float distanceToStop = .2f;
[SerializeField] float timeForDoubleClick = .2f;
[SerializeField] float clickDelay = .1f;
int clickCount = 0;
WaitForSeconds doubleClickTreashold;
bool hasToJump = false;
float lastClickTime;
ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
CameraRaycaster cameraRaycaster;
Vector3 currentClickTarget;
private void Start()
{
cameraRaycaster = GameObject.FindGameObjectWithTag("CameraDirector").GetComponent<CameraRaycaster>();
m_Character = GetComponent<ThirdPersonCharacter>();
currentClickTarget = transform.position;
doubleClickTreashold = new WaitForSeconds(timeForDoubleClick);
lastClickTime = Time.time;
}
// Fixed update is called in sync with physics
private void FixedUpdate()
{
if (Input.GetMouseButton(0))
{
CheckRaycastHit();
}
if (Input.GetMouseButtonDown(0) && (Time.time - lastClickTime >= clickDelay)) {
lastClickTime = Time.time;
OnPointerClick();
}
}
void LateUpdate() {
Vector3 playerClickPoint = Vector3.zero;
if ((currentClickTarget - transform.position).magnitude >= distanceToStop) {
playerClickPoint = currentClickTarget - transform.position;
}
m_Character.Move(playerClickPoint, false, hasToJump);
hasToJump = false;
}
void CheckRaycastHit() {
switch (cameraRaycaster.layerHit) {
case Layer.Walkable:
currentClickTarget = cameraRaycaster.hit.point;
break;
case Layer.Enemy:
Debug.Log("[PlayerMovement.cs] this enemy should be attacked");
break;
case Layer.Obstacle:
Debug.Log("[PlayerMovement.cs] not moving toward Obstacle");
break;
default:
Debug.Log("[PlayerMovement.cs] this case for layerHit wasn't considered");
break;
}
}
void OnPointerClick() {
clickCount++;
if(clickCount == 2) {
hasToJump = true;
clickCount = 0;
} else {
StartCoroutine(TickDown());
}
}
IEnumerator TickDown() {
yield return doubleClickTreashold;
if (clickCount > 0) {
clickCount--;
}
}
}