0% found this document useful (0 votes)
18 views3 pages

Joystick Controller

This C# script controls a joystick for moving a family of game objects in a Unity game. It gets component references at start, finds the first family member to control, and handles touch input to map joystick position to movement vectors. On touch, it calculates movement speed based on joystick position and passes the input to the controlled family member to move or teleport depending on its ability.

Uploaded by

David Parra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Joystick Controller

This C# script controls a joystick for moving a family of game objects in a Unity game. It gets component references at start, finds the first family member to control, and handles touch input to map joystick position to movement vectors. On touch, it calculates movement speed based on joystick position and passes the input to the controlled family member to move or teleport depending on its ability.

Uploaded by

David Parra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System.Collections.

Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;

public class JoystickController : MonoBehaviour, IPointerDownHandler,


IPointerUpHandler, IDragHandler
{
RectTransform m_rtBack;
RectTransform m_rtJoyStick;
public GameObject family;
FamilyMovement zombieToMove;
float m_Radius;
public float m_Speed = 6.0f;
private float _tempSpeed;

private List<FamilyMovement> familyMovement;


Vector3 m_VecMove;
Vector3 m_Angles;
bool m_bTouch = false;

private void Start()


{
family = FindAnyObjectByType<FamilyIndex>().gameObject;
m_rtBack = transform.Find("JoystickBack").GetComponent<RectTransform>();
m_rtJoyStick =
transform.Find("JoystickBack/Joystick").GetComponent<RectTransform>();

familyMovement = family.GetComponentsInChildren<FamilyMovement>().ToList();
foreach (FamilyMovement _familyMovement in familyMovement)
{
if (_familyMovement._index == 0)
zombieToMove = _familyMovement;
}
m_Radius = 55f;
m_rtJoyStick.localPosition = Vector3.zero;
_tempSpeed = m_Speed;
}

private void Update()


{
familyMovement = family.GetComponentsInChildren<FamilyMovement>().ToList();
foreach (FamilyMovement _familyMovement in familyMovement)
{
if (_familyMovement._index == 0 && _familyMovement._isAttached)
zombieToMove = _familyMovement;

}
if(m_bTouch && !zombieToMove._HadesAbility)
{
zombieToMove._input += m_VecMove;
zombieToMove._Angle = m_Angles;
}
else if (m_bTouch && zombieToMove._HadesAbility &&
zombieToMove._teleportDecalinstace != null)
{
Vector3 spawnPosition = zombieToMove.spawnDistance; // You can set this
in the Inspector or calculate it programmatically.
m_Speed = 1f;

Quaternion originalRotation = Quaternion.Euler(m_Angles);

// Create a rotation matrix for 45 degrees


Matrix4x4 rotationMatrix45 = Matrix4x4.Rotate(Quaternion.Euler(0, 45,
0));

// Rotate the Quaternion by the 45-degree rotation matrix


Quaternion rotatedQuaternion = rotationMatrix45.rotation *
originalRotation;

// Convert the rotated Quaternion back to Euler angles


Vector3 rotatedEulerAngles = rotatedQuaternion.eulerAngles;

Vector3 movement = zombieToMove.TeleportMovementSpeed * Time.deltaTime


* (zombieToMove._teleportDecalinstace.transform.forward *
m_VecMove.magnitude).normalized;

// Calculate the new position after applying the movement.


Vector3 newPosition =
zombieToMove._teleportDecalinstace.transform.position + movement;
Vector3 vectorToNewPosition = newPosition - spawnPosition;
vectorToNewPosition = Vector3.ClampMagnitude(vectorToNewPosition, 10);

zombieToMove._teleportDecalinstace.transform.position = spawnPosition +
vectorToNewPosition;

zombieToMove._teleportDecalinstace.transform.eulerAngles =
rotatedEulerAngles;
zombieToMove._input = Vector3.zero;
}
else
{
zombieToMove._input = Vector3.zero;
m_Speed = _tempSpeed;
}
}
void OnTouch(Vector2 vecTouch)
{
Vector2 vec = new Vector2(vecTouch.x - m_rtBack.position.x, vecTouch.y -
m_rtBack.position.y);

// make sure that vec value does not exceed m_radius


vec = Vector2.ClampMagnitude(vec, m_Radius);
m_rtJoyStick.localPosition = vec;

// move the joytstick background to the distance ratio of the joystick


float fSqr = (m_rtBack.position - m_rtJoyStick.position).sqrMagnitude /
(m_Radius * m_Radius);

//normalize touch position


Vector2 vecNormal = vec.normalized;

m_VecMove = new Vector3 (vecNormal.x * m_Speed * Time.deltaTime * fSqr, 0f,


vecNormal.y * m_Speed * Time.deltaTime * fSqr);
m_Angles = new Vector3(0f, Mathf.Atan2(vecNormal.x, vecNormal.y) *
Mathf.Rad2Deg, 0f);
}

public void OnDrag(PointerEventData eventData)


{
OnTouch(eventData.position);
m_bTouch = true;
}

public void OnPointerDown(PointerEventData eventData)


{
OnTouch(eventData.position);
m_bTouch = true;
}

public void OnPointerUp(PointerEventData eventData)


{
m_rtJoyStick.localPosition = Vector3.zero;
m_bTouch = false;
}
}

You might also like