Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PlayerControler : MonoBehaviour {
- public GameObject _AnimationBody;
- float ForwardVelocity = 0;
- float HorizontalVelocity = 0;
- Vector3 AirVelocity = new Vector3();
- Vector3 Velocity
- {
- get
- {
- return transform.TransformDirection(HorizontalVelocity, 0, HorizontalVelocity) + AirVelocity;
- }
- set
- {
- Vector3 V = value;
- V = Vector3.ProjectOnPlane(V, transform.up);
- ForwardVelocity = transform.InverseTransformDirection(V).z;
- HorizontalVelocity = transform.InverseTransformDirection(V).x;
- AirVelocity = value - GroundVelocity;
- }
- }
- Vector3 LocalVelocity
- {
- get
- {
- return transform.InverseTransformDirection(AirVelocity) + new Vector3(HorizontalVelocity, 0, ForwardVelocity);
- }
- set
- {
- Vector3 V = transform.TransformDirection(value);
- V = Vector3.ProjectOnPlane(V, transform.up);
- ForwardVelocity = transform.InverseTransformDirection(V).z;
- HorizontalVelocity = transform.InverseTransformDirection(V).x;
- AirVelocity = value - GroundVelocity;
- }
- }
- Vector3 GroundVelocity
- {
- get
- {
- return transform.TransformDirection(HorizontalVelocity, 0, HorizontalVelocity);
- }
- set
- {
- Vector3 V = Vector3.ProjectOnPlane(value, transform.up);
- ForwardVelocity = transform.InverseTransformDirection(V).z;
- HorizontalVelocity = transform.InverseTransformDirection(V).x;
- }
- }
- Vector3 LocalGroundVelocity
- {
- get
- {
- return new Vector3(HorizontalVelocity, 0, HorizontalVelocity);
- }
- set
- {
- Vector3 V = transform.TransformDirection(value);
- ForwardVelocity = transform.InverseTransformDirection(Vector3.Project(V, transform.forward)).z;
- HorizontalVelocity = transform.InverseTransformDirection(Vector3.Project(V, transform.right)).x;
- }
- }
- Trans _CameraTransformDuplicate;
- Vector3 _MoveInput = new Vector3();
- Vector3 _RawInput = new Vector3();
- public Vector3 _Gravity = new Vector3(0, -9.81f, 0);
- float InitialInputMag;
- float InitialLerpedInput;
- public float GroundRaycastLengthMin = 0.5f;
- public float GroundRaycastLengthMax = 2f;
- public float Height = 0.25f;
- public float MaximumSpeed = 40;
- public float Acell = 0.6f;
- public float AirAcell = 1.2f;
- public float Decel = 0.4f;
- public bool UtopiaTurning = false;
- public bool _Grounded;
- Rigidbody RB;
- public enum PlayerState
- {
- Grounded,
- OnWall,
- InAir,
- WallRun
- }
- public PlayerState _PlayerState = PlayerState.Grounded;
- private void Start()
- {
- RB = GetComponent<Rigidbody>();
- _CameraTransformDuplicate = new Trans(Camera.main.transform);
- }
- private void Update()
- {
- _CameraTransformDuplicate = new Trans(Camera.main.transform);
- //CameraControl(Time.deltaTime);
- // Get curve position
- Debug.DrawRay(transform.position, Velocity, Color.red, Velocity.magnitude);
- // Get the axis and jump input.
- float h = InputManager.LeftStickX; //Input.GetAxis("Horizontal");
- float v = InputManager.LeftStickY; //Input.GetAxis("Vertical");
- if (InputManager.IsPressed("Up"))
- {
- v = 1;
- }
- if (InputManager.IsPressed("Down"))
- {
- v = -1;
- }
- if (InputManager.IsPressed("Right"))
- {
- h = 1;
- }
- if (InputManager.IsPressed("Left"))
- {
- h = -1;
- }
- // calculate move direction
- Vector3 moveInp = new Vector3(h, 0, v);
- InitialInputMag = moveInp.sqrMagnitude;
- InitialLerpedInput = Mathf.Lerp(InitialLerpedInput, InitialInputMag, Time.deltaTime);
- Vector3 transformedInput = Quaternion.FromToRotation(_CameraTransformDuplicate.Up, transform.up) * (_CameraTransformDuplicate.rotation * moveInp);
- transformedInput = transform.InverseTransformDirection(transformedInput);
- transformedInput.y = 0.0f;
- _RawInput = transformedInput;
- moveInp = transformedInput;
- _MoveInput = moveInp;
- if (InputManager.IsJustPressed("Jump") && _Grounded)
- {
- }else if (InputManager.PressedElapsedTime("Jump") < 1)
- {
- }
- if (InputManager.IsJustReleased("Jump"))
- {
- }
- }
- private void FixedUpdate()
- {
- Velocity = RB.velocity;
- GeneralPhysics();
- RB.velocity = Velocity;
- DebugText._DebugText._Velocity = Velocity;
- }
- void GeneralPhysics()
- {
- Velocity = Velocity + _Gravity * Time.fixedDeltaTime;
- RaycastHit _Hit;
- if (_Grounded)
- {
- if (Physics.Raycast(transform.position, -transform.up, out _Hit, Mathf.Lerp(GroundRaycastLengthMin, GroundRaycastLengthMax, GroundVelocity.magnitude/MaximumSpeed)))
- {
- _Grounded = true;
- transform.position = _Hit.point + _Hit.normal * Height;
- Vector3 F1 = Vector3.Cross(Vector3.up, -Vector3.Cross(Vector3.up, _CameraTransformDuplicate.Forward));
- Vector3 F2 = Vector3.Cross(_Hit.normal, -Vector3.Cross(_Hit.normal, _CameraTransformDuplicate.Forward));
- Quaternion Q1 = Quaternion.LookRotation(F1, Vector3.up);
- Quaternion Q2 = Quaternion.LookRotation(F2, _Hit.normal);
- Rotate(Quaternion.Lerp(Q1, Q2, GroundVelocity.magnitude / MaximumSpeed), 0);
- }
- else
- {
- _Grounded = false;
- Vector3 F1 = Vector3.Cross(Vector3.up, -Vector3.Cross(Vector3.up, _CameraTransformDuplicate.Forward));
- Quaternion Q1 = Quaternion.LookRotation(F1, Vector3.up);
- Rotate(Q1, 0);
- }
- }
- else
- {
- if (Physics.Raycast(transform.position, -transform.up, out _Hit, GroundRaycastLengthMin))
- {
- _Grounded = true;
- transform.position = _Hit.point + _Hit.normal * Height;
- Vector3 F1 = Vector3.Cross(Vector3.up, -Vector3.Cross(Vector3.up, _CameraTransformDuplicate.Forward));
- Vector3 F2 = Vector3.Cross(_Hit.normal, -Vector3.Cross(_Hit.normal, _CameraTransformDuplicate.Forward));
- Quaternion Q1 = Quaternion.LookRotation(F1, Vector3.up);
- Quaternion Q2 = Quaternion.LookRotation(F2, _Hit.normal);
- Rotate(Quaternion.Lerp(Q1, Q2, GroundVelocity.magnitude / MaximumSpeed), 0);
- }
- else
- {
- _Grounded = false;
- Vector3 F1 = Vector3.Cross(Vector3.up, -Vector3.Cross(Vector3.up, _CameraTransformDuplicate.Forward));
- Quaternion Q1 = Quaternion.LookRotation(F1, Vector3.up);
- Rotate(Q1, 0);
- }
- }
- Vector3 GV = GroundVelocity;
- if (_MoveInput == Vector3.zero)
- {
- if (_Grounded)
- {
- float Mag = GV.magnitude;
- Mag = Mathf.Clamp(Mag - Decel * Time.fixedDeltaTime, 0, MaximumSpeed);
- }
- }
- else
- {
- if (_Grounded)
- {
- float Mag = GV.magnitude;
- Mag = Mathf.Clamp(Mag + Acell * Time.fixedDeltaTime, 0, MaximumSpeed);
- GV = _MoveInput * Mag;
- }
- else
- {
- float Mag = GV.magnitude;
- Mag = Mathf.Clamp(Mag + AirAcell * Time.fixedDeltaTime, 0, MaximumSpeed);
- GV = _MoveInput * Mag;
- }
- }
- LocalGroundVelocity = GV;
- Vector3 NextPoint = transform.position + Velocity * Time.fixedDeltaTime;
- if (_Grounded)
- {
- if (Physics.Raycast(NextPoint, -transform.up, out _Hit, Mathf.Lerp(GroundRaycastLengthMin, GroundRaycastLengthMax, GroundVelocity.magnitude / MaximumSpeed)))
- {
- Velocity += DownForce(_Hit);
- }
- }
- }
- void Rotate(Quaternion Rotation, float PreserveVAmount)
- {
- Vector3 V = Velocity;
- transform.rotation = Rotation;
- Vector3 V2 = Velocity;
- Velocity = Vector3.Lerp(V, V2, PreserveVAmount);
- }
- Vector3 DownForce(RaycastHit _Hit)
- {
- Vector3 DF = -_Gravity;
- DF = _Hit.distance * -_Hit.normal;
- return DF;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement