Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #region Rotation control
- float airbrakeAngle, steerAngle;
- float maxTilt = 60f;
- Vector3 eulerAngles;
- void RotationControl()
- { // FixedUpdate
- eulerAngles = myRigidBody.rotation.eulerAngles;
- // Calculate steering, airbrake and pitch depending on current rotation + input + handling stat
- eulerAngles = FixAngles(eulerAngles);
- airbrakeAngle = eulerAngles.z + -axisValueAirbrake * speed * .002f;
- steerAngle = eulerAngles.y + axisValueSteer;
- eulerAngles = new Vector3(Mathf.Clamp(eulerAngles.x, -maxTilt, maxTilt), steerAngle, airbrakeAngle);
- myRigidBody.MoveRotation(Quaternion.Slerp(myRigidBody.rotation, Quaternion.Euler(eulerAngles), Time.fixedDeltaTime * Time.timeScale));
- }
- Vector3 FixAngles(Vector3 angles)
- {
- if (angles.x > 180) angles.x -= 360;
- if (angles.x < -180) angles.x += 360;
- if (angles.y > 180) angles.y -= 360;
- if (angles.y < -180) angles.y += 360;
- if (angles.z > 180) angles.z -= 360;
- if (angles.z < -180) angles.z += 360;
- return angles;
- }
- #endregion
- #region Hovering
- float currentDistanceFromGround;
- float hoverThrust = 4020f;
- Ray rayFront, rayBack;
- RaycastHit hitInfoFront, hitInfoBack;
- Vector3 averageGroundNormal;
- void HoverControl()
- { // FixedUpdate
- // Set up Rays
- var posRayFront = transform.position + transform.forward * 2f + transform.up * .1f;
- var posRayBack = transform.position + transform.forward * -2f + transform.up * .1f;
- rayFront = new Ray(posRayFront, -transform.up);
- rayBack = new Ray(posRayBack, -transform.up);
- // Shoot rays
- Physics.Raycast(rayFront, out hitInfoFront, 10f, 1 << LayerMask.NameToLayer("Track"));
- Physics.Raycast(rayBack, out hitInfoBack, 10f, 1 << LayerMask.NameToLayer("Track"));
- // Calculate distance from ground and ground normal
- currentDistanceFromGround = (Vector3.Distance(posRayFront, hitInfoFront.point) + Vector3.Distance(posRayBack, hitInfoBack.point)) / 2f;
- averageGroundNormal = (hitInfoFront.normal + hitInfoBack.normal) / 2f;
- // Apply force depending on distance to ground
- myRigidBody.AddForce(transform.up * hoverThrust * (1f - currentDistanceFromGround / distanceFromGroundWanted));
- // Create a smooth rotational alignment to the ground
- myRigidBody.MoveRotation(Quaternion.Slerp(myRigidBody.rotation, Quaternion.FromToRotation(transform.up, averageGroundNormal) * myRigidBody.rotation, Time.fixedDeltaTime * Time.timeScale * rotationDamp));
- }
- }
- #endregion
- void AccelerationControl()
- { // FixedUpdate
- myRigidBody.AddRelativeForce(Vector3.forward * axisValueAccelerate * thrustForce, ForceMode.Force);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement