Sure! Below is an example of a PlayerController script in Unity that includes the functionalities you mentioned:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// Movement speed of the player
public float speed = 10f;
// Tractor beam activation time (in seconds)
public float tractorBeamActivationTime = 1f;
// Reference to the tractor beam
public TractorBeam tractorBeam;
// Ground layer mask for checking if the player touches the ground
public LayerMask groundLayerMask;
// Reference to the rigidbody component
private Rigidbody rb;
// Flag to indicate if the player is grounded
private bool grounded;
// Timer for the tractor beam activation
private float tractorBeamTimer;
private void Awake()
{
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (IsGrounded())
{
grounded = true;
}
// Keyboard/PC controls
if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
{
// Movement input
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0f);
// Apply movement
rb.velocity = movement * speed;
// Tractor beam activation
if (Input.GetKey(KeyCode.Space))
{
tractorBeamTimer += Time.deltaTime;
// Tractor beam triggered on long press
if (tractorBeamTimer >= tractorBeamActivationTime)
{
tractorBeam.Activate();
}
}
else
{
tractorBeamTimer = 0f;
}
}
// Mobile controls
else if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
{
// Tilt input
float tiltHorizontal = Input.acceleration.x;
float tiltVertical = Input.acceleration.y;
Vector3 tiltMovement = new Vector3(tiltHorizontal, tiltVertical, 0f);
// Apply movement
rb.velocity = tiltMovement * speed;
// Tractor beam activation (touch input)
if (Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
{
tractorBeamTimer = 0f;
}
else if (touch.phase == TouchPhase.Stationary)
{
tractorBeamTimer += Time.deltaTime;
// Tractor beam triggered on long press
if (tractorBeamTimer >= tractorBeamActivationTime)
{
tractorBeam.Activate();
}
}
else if (touch.phase == TouchPhase.Ended)
{
tractorBeamTimer = 0f;
}
}
else
{
tractorBeamTimer = 0f;
}
}
// Kill the player if it touches the ground
if (grounded)
{
// Play death animation or game over logic
Debug.Log("Player killed!");
Destroy(gameObject);
}
}
// Check if the player is grounded (using a spherecast)
private bool IsGrounded()
{
float radius = GetComponent<CapsuleCollider>().radius * 0.9f;
Vector3 sphereCenter = transform.position - new Vector3(0f, GetComponent<CapsuleCollider>().height / 2f - radius, 0f);
bool grounded = Physics.CheckSphere(sphereCenter, radius, groundLayerMask);
return grounded;
}
}
Please note that this script assumes you have a TractorBeam script/component attached to the same GameObject as the PlayerController script. You will need to implement and customize the TractorBeam script according to your needs.
Remember to attach the PlayerController script to your player GameObject and set up the required references in the Inspector. Additionally, you need to specify the ground layer for the ground detection to work properly.
Feel free to adjust the speed, activation time, and any other variables to fit your game's requirements.