void Update() { float rotationX; if (useVCR) { rotationX = vcr.GetAxis("Mouse X") * sensitivityX; rotationY += vcr.GetAxis("Mouse Y") * sensitivityY; } else { rotationX = Input.GetAxis("Mouse X") * sensitivityX; rotationY += Input.GetAxis("Mouse Y") * sensitivityY; } if (axes == RotationAxes.MouseXAndY) { rotationX += transform.localEulerAngles.y; rotationY = Mathf.Clamp(rotationY, minimumY, maximumY); transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0); } else if (axes == RotationAxes.MouseX) { transform.Rotate(0, rotationX, 0); } else { rotationY = Mathf.Clamp(rotationY, minimumY, maximumY); transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0); } }
// Update is called once per frame void Update() { xAxis = vcr.GetAxis("Right", "Left", 5); yAxis = vcr.GetAxis("Forward", "Backward", 5); camxAxis = vcr.GetAxis("Mouse X", 1); camyAxis = vcr.GetAxis("Mouse Y", 1); // if (useVCR) // { // xAxis = vcr.GetAxis("Right", "Left", 4); // yAxis = vcr.GetAxis("Forward", "Backward", 4); // camxAxis = vcr.GetAxis("Mouse X", 1); // camyAxis = vcr.GetAxis("Mouse Y", 1); // } // else // { // xAxis = Input.GetAxisRaw("Horizontal"); // yAxis = Input.GetAxisRaw("Vertical"); // camxAxis = Input.GetAxisRaw("Mouse X"); // camyAxis = Input.GetAxisRaw("Mouse Y"); // } }
// Update is called once per frame void Update() { // Get the input vector from kayboard or analog stick Vector3 directionVector; if (useVCR) { directionVector = new Vector3(vcr.GetAxis("Horizontal"), 0, vcr.GetAxis("Vertical")); } else { directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); } if (directionVector != Vector3.zero) { // Get the length of the directon vector and then normalize it // Dividing by the length is cheaper than normalizing when we already have the length anyway float directionLength = directionVector.magnitude; directionVector = directionVector / directionLength; // Make sure the length is no bigger than 1 directionLength = Mathf.Min(1, directionLength); // Make the input vector more sensitive towards the extremes and less sensitive in the middle // This makes it easier to control slow speeds when using analog sticks directionLength = directionLength * directionLength; // Multiply the normalized direction vector by the modified length directionVector = directionVector * directionLength; } // Apply the direction to the CharacterMotor motor.inputMoveDirection = transform.rotation * directionVector; motor.inputJump = useVCR && vcr.GetButton("Jump") || !useVCR && Input.GetButton("Jump"); }
/// <summary> /// Updates on a fixed time interval. /// </summary> void FixedUpdate() { //Check for lap reset button if (vcr.GetButtonDown("Reset")) { ResetLap(); } else if (Input.GetButtonDown("Reset")) { ResetLap(); } /* * //Check for pause button, doesn't actually work yet * if( vcr.GetButtonDown( "Reset" ) ) * { * Time.timeScale = 0; * isPaused = true; * } * else if( vcr.GetButtonDown( "Reset" ) && isPaused ) * { * Time.timeScale = 1; * isPaused = false; * }*/ //Boost //if( useVCR ) // isBoosting = vcr.GetAxis( "Boost" ) > 0.0f; //else // isBoosting = Input.GetAxis( "Boost" ) > 0.0f; // Rotate to match plane // 0: Front // 1: Back // 2: Left // 3: Right // Do raycasts for (int ii = 0; ii < raycasters.Length; ++ii) { Physics.Raycast(raycasters[ii].transform.position, -raycasters[ii].transform.up, out raycastHits[ii]); } //print out the raycast distances //Debug.Log( raycastHits[ 0 ].distance + ", " + raycastHits[ 1 ].distance + " / " + raycastHits[ 2 ].distance + ", " + raycastHits[ 3 ].distance ); //push directly downwards when rotating onto a berm //if( transform.eulerAngles.z < 180.0f && transform.eulerAngles.z > 5.0f ) // rigidbody.AddForce( new Vector3( 0f, -100.0f * transform.eulerAngles.z, 0.0f ) ); //else if( transform.eulerAngles.z > 180.0f && transform.eulerAngles.z < 355.0f ) // rigidbody.AddForce( new Vector3( 0f, -100.0f * ( 360.0f - transform.eulerAngles.z ), 0.0f ) ); //push forwards when going up a hill //if( transform.eulerAngles.x > 180.0f ) //rigidbody.AddForce( new Vector3( 0.0f, 0.0f, 100.0f * ( 360f - transform.eulerAngles.x ) ) ); // If on the track if (raycastHits[0].distance < 1.5f || raycastHits[1].distance < 1.5f) { //hover off the ground rigidbody.AddRelativeForce(0.0f, 150.0f, 0.0f); // If the front and back of the ship are not balanced, rotate to balance them if (raycastHits[0].distance != raycastHits[1].distance) { transform.Rotate( Mathf.Atan((raycastHits[0].distance - raycastHits[1].distance) / (raycasters[1].transform.localPosition.z - raycasters[0].transform.localPosition.z)), 0.0f, 0.0f); } // Rotate up onto berms if (Mathf.Abs(raycastHits[2].distance - raycastHits[3].distance) > 0.05f) { transform.Rotate( 0.0f, 0.0f, Mathf.Atan((raycastHits[2].distance - raycastHits[3].distance) / (raycasters[2].transform.localPosition.z - raycasters[3].transform.localPosition.z))); } } //if going over a jump or off the track, turn the noise downwards else if (raycastHits[0].distance > 2f && raycastHits[1].distance > 2f) { transform.Rotate(rigidbody.mass / 5.0f, 0.0f, 0.0f); //magnetize down onto to the track if you get too far off rigidbody.AddRelativeForce(0.0f, Mathf.Min(-150.0f * raycastHits[0].distance, 5000f), 0.0f); Physics.gravity = new Vector3(0, Mathf.Min(-150.0f * raycastHits[0].distance, 5000f), 0); } // Lean if (useVCR) { if (vcr.GetAxis("Horizontal") < 0.0f) { currentTurn = Mathf.Max(new float[] { vcr.GetAxis("Horizontal"), currentTurn - (turnAcceleration * Time.deltaTime) }); } else if (vcr.GetAxis("Horizontal") > 0.0f) { currentTurn = Mathf.Min(new float[] { vcr.GetAxis("Horizontal"), currentTurn + (turnAcceleration * Time.deltaTime) }); } else if (Mathf.Abs(currentTurn) > 0.2f) { currentTurn = currentTurn + ((currentTurn > 0.0f ? -turnAcceleration : turnAcceleration) * Time.deltaTime); } else { currentTurn = 0.0f; } } else { if (Input.GetAxis("Horizontal") < 0.0f) { currentTurn = Mathf.Max(new float[] { Input.GetAxis("Horizontal"), currentTurn - (turnAcceleration * Time.deltaTime) }); } else if (Input.GetAxis("Horizontal") > 0.0f) { currentTurn = Mathf.Min(new float[] { Input.GetAxis("Horizontal"), currentTurn + (turnAcceleration * Time.deltaTime) }); } else if (Mathf.Abs(currentTurn) > 0.2f) { currentTurn = currentTurn + ((currentTurn > 0.0f ? -turnAcceleration : turnAcceleration) * Time.deltaTime); } else { currentTurn = 0.0f; } } model.transform.localRotation = Quaternion.Euler(0.0f, 0.0f, maxRotation * currentTurn); float forwardVelocity = transform.InverseTransformDirection(rigidbody.velocity).z; // Rotate rigidbody.AddTorque(rigidbody.transform.up * -currentTurn * rotateSpeed); rigidbody.velocity = rigidbody.transform.forward * forwardVelocity; if (rigidbody.angularVelocity.magnitude > maxAngularVelocity) { rigidbody.angularVelocity = rigidbody.angularVelocity.normalized * maxAngularVelocity; } // Accelerate if (useVCR) { rigidbody.AddForce(rigidbody.transform.forward * (vcr.GetAxis("Vertical") * acceleration * Time.deltaTime * (isBoosting ? boostAccelerationMultiplier : 1.0f))); } else { rigidbody.AddForce(rigidbody.transform.forward * (Input.GetAxis("Vertical") * acceleration * Time.deltaTime * (isBoosting ? boostAccelerationMultiplier : 1.0f))); } if (!isBoosting && forwardVelocity > maxSpeed + 10.0f) { rigidbody.velocity = rigidbody.velocity.normalized * (forwardVelocity - 10.0f); } else if (!isBoosting && forwardVelocity > maxSpeed) { rigidbody.velocity = rigidbody.velocity.normalized * maxSpeed; speedLines.Play(); } else if (isBoosting && forwardVelocity > boostMaxSpeed) { rigidbody.velocity = rigidbody.velocity.normalized * boostMaxSpeed; speedLines.Play(); } else { speedLines.Stop(); } // Change particles based on input if (useVCR) { engineParticles.emissionRate = (vcr.GetAxis("Vertical") + (isBoosting ? 1.0f : 0.0f)) * 400.0f; } else { engineParticles.emissionRate = (Input.GetAxis("Vertical") + (isBoosting ? 1.0f : 0.0f)) * 400.0f; } }