Advertisement
Guest User

3D object movement script

a guest
Oct 28th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1.     #region Rotation control
  2.     float airbrakeAngle, steerAngle;
  3.     float maxTilt = 60f;
  4.     Vector3 eulerAngles;
  5.     void RotationControl()
  6.     {   //  FixedUpdate
  7.         eulerAngles = myRigidBody.rotation.eulerAngles;
  8.  
  9.         //  Calculate steering, airbrake and pitch depending on current rotation + input + handling stat
  10.         eulerAngles = FixAngles(eulerAngles);
  11.  
  12.         airbrakeAngle = eulerAngles.z + -axisValueAirbrake * speed * .002f;
  13.         steerAngle = eulerAngles.y + axisValueSteer;
  14.  
  15.         eulerAngles = new Vector3(Mathf.Clamp(eulerAngles.x, -maxTilt, maxTilt), steerAngle, airbrakeAngle);
  16.  
  17.         myRigidBody.MoveRotation(Quaternion.Slerp(myRigidBody.rotation, Quaternion.Euler(eulerAngles), Time.fixedDeltaTime * Time.timeScale));
  18.     }
  19.  
  20.     Vector3 FixAngles(Vector3 angles)
  21.     {
  22.         if (angles.x > 180) angles.x -= 360;
  23.         if (angles.x < -180) angles.x += 360;
  24.         if (angles.y > 180) angles.y -= 360;
  25.         if (angles.y < -180) angles.y += 360;
  26.         if (angles.z > 180) angles.z -= 360;
  27.         if (angles.z < -180) angles.z += 360;
  28.         return angles;
  29.     }
  30.     #endregion
  31.  
  32.    #region Hovering
  33.     float currentDistanceFromGround;
  34.     float hoverThrust = 4020f;
  35.     Ray rayFront, rayBack;
  36.     RaycastHit hitInfoFront, hitInfoBack;
  37.     Vector3 averageGroundNormal;
  38.     void HoverControl()
  39.     {   //  FixedUpdate
  40.         //  Set up Rays
  41.         var posRayFront = transform.position + transform.forward * 2f + transform.up * .1f;
  42.         var posRayBack = transform.position + transform.forward * -2f + transform.up * .1f;
  43.  
  44.         rayFront = new Ray(posRayFront, -transform.up);
  45.         rayBack = new Ray(posRayBack, -transform.up);
  46.  
  47.         //  Shoot rays
  48.         Physics.Raycast(rayFront, out hitInfoFront, 10f, 1 << LayerMask.NameToLayer("Track"));
  49.         Physics.Raycast(rayBack, out hitInfoBack, 10f, 1 << LayerMask.NameToLayer("Track"));
  50.  
  51.         //  Calculate distance from ground and ground normal
  52.         currentDistanceFromGround = (Vector3.Distance(posRayFront, hitInfoFront.point) + Vector3.Distance(posRayBack, hitInfoBack.point)) / 2f;
  53.         averageGroundNormal = (hitInfoFront.normal + hitInfoBack.normal) / 2f;
  54.  
  55.         //  Apply force depending on distance to ground
  56.         myRigidBody.AddForce(transform.up * hoverThrust * (1f - currentDistanceFromGround / distanceFromGroundWanted));
  57.  
  58.         //  Create a smooth rotational alignment to the ground
  59.         myRigidBody.MoveRotation(Quaternion.Slerp(myRigidBody.rotation, Quaternion.FromToRotation(transform.up, averageGroundNormal) * myRigidBody.rotation, Time.fixedDeltaTime * Time.timeScale * rotationDamp));
  60.         }
  61.     }
  62.     #endregion
  63.  
  64.     void AccelerationControl()
  65.     {   //  FixedUpdate
  66.         myRigidBody.AddRelativeForce(Vector3.forward * axisValueAccelerate * thrustForce, ForceMode.Force);
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement