please create a unity script to control a player that is a flying character, please…
- User
please create a unity script to control a player that is a flying character, please control its direction with WASD in pc, and with tilting in IOS and android. please make it able to pick up and object from a platform if landing gear is out and it lands slowly. please make it able to deploy and retract landing gear with a button press on pc and a touch on ios and android
- Assistant
Sure! Here's a Unity script that allows you to control a flying character using both keyboard input (WASD) and device tilting (iOS and Android). It also includes the ability to pick up objects from a platform when the landing gear is out and the character lands slowly. Additionally, it allows for deploying and retracting the landing gear with a button press on PC and touch input on iOS and Android.
using UnityEngine; public class FlyingCharacterController : MonoBehaviour { public float speed = 5f; public float tiltFactor = 2f; public float landingGearThreshold = 0.2f; private Rigidbody rb; private bool landingGearDeployed = false; void Start() { rb = GetComponent<Rigidbody>(); } void Update() { // PC Controls - Keyboard (WASD) for movement float moveHorizontal = Input.GetAxis("Horizontal"); float moveVertical = Input.GetAxis("Vertical"); // Mobile Controls - Tilting for movement (iOS and Android) if (Input.acceleration.magnitude > landingGearThreshold) { float tiltHorizontal = Input.acceleration.x; float tiltVertical = Input.acceleration.y; moveHorizontal = tiltHorizontal * tiltFactor; moveVertical = tiltVertical * tiltFactor; } // Apply movement Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical); rb.AddForce(movement * speed); // Deploy/Retract landing gear if (Input.GetKeyDown(KeyCode.Space) // PC Controls - Space key for landing gear || (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)) // Mobile Controls - Touch for landing gear (iOS and Android) { if (landingGearDeployed) { RetractLandingGear(); } else { DeployLandingGear(); } } } void OnCollisionEnter(Collision collision) { // Check if landing gear is deployed and character lands slowly on a platform if (landingGearDeployed && rb.velocity.y < landingGearThreshold) { // Check for platform tag or layer if (collision.gameObject.CompareTag("Platform") || collision.gameObject.layer == LayerMask.NameToLayer("Platform")) { // Pick up object code goes here Debug.Log("Picked up an object!"); } } } void DeployLandingGear() { // Code to deploy landing gear Debug.Log("Landing gear deployed!"); landingGearDeployed = true; } void RetractLandingGear() { // Code to retract landing gear Debug.Log("Landing gear retracted!"); landingGearDeployed = false; } }Make sure to attach this script to your flying character in Unity. You will also need to set up the mobile controls using Unity's Input System and accelerometer.