Could someone explain why i get an error on line 78 - " Use of unassigned local variable 'startPos' " & " Use of unassigned local variable 'endPos' ".
Can you not pass variables to another method this way? or am I miss understanding how to do this.
using UnityEngine;
public class PlayerInput : MonoBehaviour
{
[SerializeField] readonly float swipeDeadzone = 125f;
PlayerMovement playerMovement;
Pewpews pewpews;
UIController uIController;
GameObject player;
void Start()
{
playerMovement = player.GetComponent();
pewpews = player.GetComponentInChildren();
uIController = GameObject.Find("UIController").GetComponent();
player = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
GetPlayerInput();
}
void GetPlayerInput()
{
Vector3 startPos;
Vector3 endPos;
if (uIController.menuIsActive == true)
{
return;
}
#region Standalone Controls
// Standalone Controls are only used in the editor, as touch / swipe is considered mouse input even on mobile platform.
if (Application.isEditor)
{
if (Input.GetMouseButtonDown(0))
{
startPos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
endPos = Input.mousePosition;
}
}
#endregion
#region Touch Controls
// Track a single touch as a direction control.
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
// Handle finger movements based on touch phase.
switch (touch.phase)
{
// Record initial touch position.
case TouchPhase.Began:
startPos = touch.position;
break;
// Report that a direction has been chosen when the finger is lifted.
case TouchPhase.Ended:
endPos = touch.position;
break;
}
}
#endregion
MovePlayer(startPos, endPos);
}
void MovePlayer(Vector3 startPos, Vector3 endPos)
{
Vector3 swipeDirection;
if (startPos != Vector3.zero && endPos != Vector3.zero)
{
swipeDirection = endPos - startPos;
if (swipeDirection.magnitude > swipeDeadzone)
{
if (Mathf.Abs(swipeDirection.x) > Mathf.Abs(swipeDirection.y))
{
playerMovement.PlayerRotationDirection(swipeDirection);
ResetPos(startPos, endPos);
}
}
else
{
pewpews.Fire(endPos);
ResetPos(startPos, endPos);
}
}
}
void ResetPos(Vector3 startPos, Vector3 endPos)
{
startPos = endPos = Vector3.zero;
}
}
↧