please create a player controller script in unity for a flying object that includes an…
- User
please create a player controller script in unity for a flying object that includes an attack command that triggers a tractor beam on long press, please use wasd and space bar for pc control, and tilt control for android and tilt controls for ios
- Assistant
Sure! Here's an example of a player controller script in Unity that includes the attack command with a tractor beam using WASD and Spacebar for PC control, and tilt control for Android and iOS:
using UnityEngine; public class PlayerController : MonoBehaviour { public float speed = 5f; public float tiltSpeed = 2f; public float tiltAngle = 30f; public float tractorBeamDuration = 2f; private Rigidbody rb; private bool isTractorBeamActive = false; private float tractorBeamTimer = 0f; private void Awake() { rb = GetComponent<Rigidbody>(); } private void FixedUpdate() { if (Input.touchSupported && Application.platform != RuntimePlatform.WebGLPlayer) { // Tilt control for Android and iOS float tiltInputX = Input.acceleration.x * tiltSpeed; float tiltInputY = Input.acceleration.y * tiltSpeed; Vector3 movement = new Vector3(tiltInputX, 0f, tiltInputY); rb.velocity = movement * speed; // Tilt the object based on tilt input Vector3 rotation = new Vector3(tiltInputY * tiltAngle, 0f, -tiltInputX * tiltAngle); rb.rotation = Quaternion.Euler(rotation); } else { // WASD and Spacebar for PC control float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical); rb.velocity = movement * speed; // Rotate the object based on movement input rb.rotation = Quaternion.Euler(0f, 0f, -moveHorizontal * tiltAngle); } // Tractor beam activation if (isTractorBeamActive) { tractorBeamTimer += Time.deltaTime; // Perform tractor beam functionality here // ... if (tractorBeamTimer >= tractorBeamDuration) { isTractorBeamActive = false; tractorBeamTimer = 0f; } } // Tractor beam activation input if (Input.GetButtonDown("Fire1")) { // Start the tractor beam isTractorBeamActive = true; } if (Input.GetButtonUp("Fire1")) { // Stop the tractor beam isTractorBeamActive = false; tractorBeamTimer = 0f; } } }Please make sure to attach this script to your flying object in Unity. Additionally, you can modify the
tractorBeamDurationvariable to change the duration of the tractor beam functionality.Note: The script assumes that you have set up the input axes in the Unity Input Manager for PC controls (WASD and Spacebar). Also, please ensure that the input axes are adjusted as per your preference and assigned to corresponding inputs in the Unity Input Manager.