private void OnJoyStick(ref float tempX, ref float tempY) { Vector3 dir = Vector3.zero; Vector3 dir2 = Vector3.zero; dir.x = joystick.Horizontal(); dir.z = joystick.Vertical(); dir2.x = joystick2.Horizontal(); dir2.z = joystick2.Vertical(); //Joystick moving EngineForce += dir2.z / 2; // SpeedUp if (EngineForce < 0) { EngineForce = 0; } if (!IsOnGround) { if (dir.x > 0 && Mathf.Abs(dir.x) > Mathf.Abs(dir.z)) // right { tempX = Time.fixedDeltaTime; } if (dir.x < 0 && Mathf.Abs(dir.x) > Mathf.Abs(dir.z)) // left { tempX = -Time.fixedDeltaTime; } if (dir.z > 0 && Mathf.Abs(dir.z) > Mathf.Abs(dir.x)) // forward { tempY = Time.fixedDeltaTime; } if (dir.z < 0 && Mathf.Abs(dir.z) > Mathf.Abs(dir.x)) // back { tempY = -Time.fixedDeltaTime; } if (dir2.x > 0 && Mathf.Abs(dir2.x) > Mathf.Abs(dir2.z)) // turn right { var force = (turnForcePercent - Mathf.Abs(hMove.y)) * HelicopterModel.mass; HelicopterModel.AddRelativeTorque(0f, force, 0); } if (dir2.x < 0 && Mathf.Abs(dir2.x) > Mathf.Abs(dir2.z)) // turn left { var force = -(turnForcePercent - Mathf.Abs(hMove.y)) * HelicopterModel.mass; HelicopterModel.AddRelativeTorque(0f, force, 0); } } }
IEnumerator PlayerControl() { while (isAlive) { moveHorizontal = _joystick.Horizontal(); moveVertical = _joystick.Vertical();; Vector2 movement = new Vector2(moveHorizontal, moveVertical); rigidbody.velocity = movement * speed; rigidbody.position = new Vector3( Mathf.Clamp(rigidbody.position.x, -constrainX, constrainX), Mathf.Clamp(rigidbody.position.y, -constrainY, constrainY), 0); yield return(null); } }
void FixedUpdate() { /*** VECTORS USED ***/ // Player movement direction (left joystick) Vector2 moveVec = new Vector2(leftJoystick.Horizontal() , leftJoystick.Vertical()) * playerSpeed; // Player rotation direction (left joystick) Vector3 lookVec = new Vector3(leftJoystick.Horizontal() , leftJoystick.Vertical(), 4000); // Player rotation direction (right joystick) Vector3 lookVec2 = new Vector3(rightJoystick.inputVec.x , rightJoystick.inputVec.y, 4000); /*** PLAYER MOVEMENT & ROTATION ***/ // Right joystick dictates player rotation if (lookVec2.x != 0 && lookVec2.y != 0) { /*GetComponentInParent <Transform> ().rotation = * Quaternion.LookRotation (lookVec2, Vector3.back);*/ // Flips player graphics when going right if (lookVec2.x > 0) { transform.localScale = new Vector3(1.0f, transform.localScale.y, transform.localScale.z); } // Flips player graphics when going left if (lookVec2.x < 0) { transform.localScale = new Vector3(-1.0f, transform.localScale.y, transform.localScale.z); } } else { // Left joystick dictates player rotation if (lookVec.x != 0 && lookVec.y != 0) { /*GetComponentInParent <Transform> ().rotation = * Quaternion.LookRotation (lookVec, Vector3.back);*/ } else if (transform.localScale == new Vector3(1.0f, transform.localScale.y, transform.localScale.z)) { /*GetComponentInParent <Transform> ().rotation = * Quaternion.LookRotation (lookVec, Vector3.right);*/ } else { /*GetComponentInParent <Transform> ().rotation = * Quaternion.LookRotation (lookVec, Vector3.left);*/ } // Flips player graphics when going right if (leftJoystick.Horizontal() > 0) { transform.localScale = new Vector3(1.0f, transform.localScale.y, transform.localScale.z); } // Flips player graphics when going left if (leftJoystick.Horizontal() < 0) { transform.localScale = new Vector3(-1.0f, transform.localScale.y, transform.localScale.z); } } // Boost feature (not in use) bool isBoosting = CrossPlatformInputManager.GetButton("BoostButton"); playerBody.AddForce(moveVec * (isBoosting ? boostMultiplier : 1)); }
void FixedUpdate() { if ((GameObject.Find("LeftJoystick") == null) || (GameObject.Find("RightJoystick") == null)) { Destroy(gameObject); } else { // If Player turns left, so does Torpedo if (GameObject.Find("Player").GetComponent <PlayerMovement> ().turnedLeft) { transform.localScale = new Vector3(-1.0f, transform.localScale.y, transform.localScale.z); } else { // If Player turns right, so does Torpedo transform.localScale = new Vector3(1.0f, transform.localScale.y, transform.localScale.z); } // Follow target if (!stopFollow) { transform.position = target.position + offset; } // Player rotation direction (left joystick) Vector3 lookVec = new Vector3(leftJoystick.Horizontal() , leftJoystick.Vertical(), 4000); // Player rotation direction (right joystick) Vector3 lookVec2 = new Vector3(rightJoystick.inputVec.x , rightJoystick.inputVec.y, 4000); if (!stopFollow) { // Right Joystick dictates Torpedo rotation if (lookVec2.x != 0 && lookVec2.y != 0) { GetComponentInParent <Transform> ().rotation = Quaternion.LookRotation(lookVec2, Vector3.back); } else { // Left Joystick dictates Torpedo rotation if (lookVec.x != 0 && lookVec.y != 0) { GetComponentInParent <Transform> ().rotation = Quaternion.LookRotation(lookVec, Vector3.back); } else if (transform.localScale == new Vector3(1.0f, transform.localScale.y, transform.localScale.z)) { GetComponentInParent <Transform> ().rotation = Quaternion.LookRotation(lookVec, Vector3.right); } else { GetComponentInParent <Transform> ().rotation = Quaternion.LookRotation(lookVec, Vector3.left); } } } } }