void DespawnGravity() { gravityField.Stop(); Destroy(gravityField.GravityGameObject); gravityField = null; }
void Update() { // Do not run when paused or dead if (paused || !IsAlive) { rbd.isKinematic = true; return; } //************************************************ // Input handling // Horizontal and vertical axis to control the ship movement horizontalAxis = 0; verticalAxis = 0; // Handheld if (SystemInfo.deviceType == DeviceType.Handheld) { // Horizontal dead zone if (Mathf.Abs(Input.acceleration.x) > .1f) { horizontalAxis = Input.acceleration.x * 6; } // Vertical dead zone if (Mathf.Abs(Input.acceleration.y) > .2f) { // Stopping movement with accelerometer is 2 times more sensitive than accelerating, which is 2 times less sensitive than raw input if (Input.acceleration.y < 0) { verticalAxis += Input.acceleration.y * 2; } else { verticalAxis += Input.acceleration.y * .5f; } } foreach (Touch touch in Input.touches) { if (touch.phase == TouchPhase.Began) { Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position); gravityField.SpawnGravity(touchPosition, gravityMagnitude); } // Move gravity field's center according to finger movement else if (touch.phase == TouchPhase.Ended && gravityField.Activated) { gravityField.Stop(); } // Touch position newMousePosition = touch.position; break; } } // Desktop else if (SystemInfo.deviceType == DeviceType.Desktop) { // Left click if (Input.GetMouseButtonDown(0)) { Vector3 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); gravityField.SpawnGravity(clickPosition, gravityMagnitude); } // Gravity despawn if (Input.GetMouseButtonUp(0) && gravityField.Activated) { gravityField.Stop(); } horizontalAxis = Input.GetAxisRaw("Horizontal"); if (Input.GetAxisRaw("Vertical") < 0) { verticalAxis += Input.GetAxisRaw("Vertical") * 2; } else { verticalAxis += Input.GetAxisRaw("Vertical") * .5f; } // Mouse position newMousePosition = Input.mousePosition; } // Clamp movement to {-1;1} horizontalAxis = Mathf.Clamp(horizontalAxis, -1, 1); verticalAxis = Mathf.Clamp(verticalAxis, -2, 1); // DEBUG verticalAcc.text = "Y:" + verticalAxis.ToString(); horizontalAcc.text = "X: " + horizontalAxis.ToString(); verticalVel.text = "Vertical velocity: " + rbd.velocity.y; horizontalVel.text = "Horizontal velocity: " + rbd.velocity.x; //************************************************ }