Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- using System.Collections.Generic;
- public class cl_kveik: MonoBehaviour {
- // Player view stuff
- public float playerViewYOffset;
- private Transform playerView;
- private Transform tform;
- private float sensitivity;
- private float invert;
- public float PhysGravity = 7.5f;
- public float gravity = 16f; // For player
- public float friction = 3.0f; // Friction applied to player on the ground
- private float frictionMulti; // Friction multiplier (needed?)
- // Movement stuff
- [Header("Movement")]
- public float moveSpeed = 3.2f;
- public float runAcceleration = 20.0f;
- public float runDeacceleration = 5.0f;
- //public float airAcceleration = 2.0f;
- //public float airDeacceleration = 2.0f;
- //public float airControl = 0.3f;
- public float sideStrafeAcceleration = 9000f;
- public float sideStrafeSpeed = 0.2f;
- public float jumpSpeed = 6.0f;
- public float moveScale = 1.0f;
- // Ramp stuff
- [Header("Ramps")]
- public float downLandBoost = 0.5f;
- public float downJumpBoost = 0.4f;
- public float upJumpBoost = 0.15f;
- public float upSlow = 0.3f;
- public AudioClip[] jumpSounds;
- // Camera rotationals
- private float rotX;
- private float rotY;
- private Vector3 moveDirection;
- private Vector3 playerVelocity;
- // If true then the player is fully on the ground
- private bool grounded = false;
- // Players can queue the next jump just before he hits the ground
- private bool wishJump = false;
- RaycastHit hit;
- private float playerFriction;
- private CharacterController controller;
- private Vector3 playerSpawnPos;
- private Quaternion playerSpawnRot;
- private Vector3 udp;
- public Text speed;
- public Text drag;
- public Text sensi;
- float wishvel;
- float wishspeed, wishspeedcpm;
- float accel;
- Vector3 colNormal;
- private float forwardmove;
- private float rightmove;
- private float upmove;
- // WEAPON STUFF
- public float expKnockback; // Power of knockback on player
- [HideInInspector]
- public List<Vector3> knockback; // All the knockbacks
- [HideInInspector]
- public Vector3 gaussKnockback; // Power of gauss cannon kick
- void Start() {
- Physics.gravity = new Vector3(0, -PhysGravity, 0);
- List<Vector3> knockback = new List<Vector3>();
- tform = GetComponent<Transform>();
- playerView = Camera.main.transform;
- controller = GetComponent<CharacterController>();
- //cmd = new Cmd();
- rotY = 0.0f;
- Cursor.lockState = CursorLockMode.Locked;
- Cursor.visible = false;
- // Set sensitivity
- if (PlayerPrefs.HasKey("kveiksensi")) sensitivity = PlayerPrefs.GetFloat("kveiksensi");
- else { sensitivity = 1f; PlayerPrefs.SetFloat("kveiksensi", 1f); }
- // Set invert
- if (PlayerPrefs.HasKey("kveikinvert")) invert = PlayerPrefs.GetFloat("kveikinvert");
- else { invert = -1f; PlayerPrefs.SetFloat("kveikinvert", -1f); }
- }
- void FixedUpdate() {
- if (Input.GetAxis("sensiChange") != 0) {
- sensitivity += Input.GetAxis("sensiChange");
- PlayerPrefs.SetFloat("kveiksensi", sensitivity);
- }
- sensi.text = sensitivity.ToString("f3");
- }
- void Update() {
- if (Input.GetButtonDown("Invert")) {
- invert *= -1;
- PlayerPrefs.SetFloat("kveikinvert", invert);
- }
- // Check ground below
- Ray ray = new Ray(tform.position, -tform.up);
- Physics.Raycast(ray, out hit, 1f);
- Debug.DrawLine(hit.point, hit.normal.normalized);
- forwardmove = Input.GetAxisRaw("Vertical");
- rightmove = Input.GetAxisRaw("Horizontal");
- /* Camera rotation stuff, mouse controls this shit */
- rotX += Input.GetAxisRaw("Mouse Y") * sensitivity * invert;
- rotY += Input.GetAxisRaw("Mouse X") * sensitivity;
- // Clamp the X rotation
- if (rotX < -90)
- rotX = -90;
- else if (rotX > 90)
- rotX = 90;
- tform.rotation = Quaternion.Euler(0, rotY, 0); // Rotates the collider
- playerView.rotation = Quaternion.Euler(rotX, rotY, 0);
- // Movement
- QueueJump();
- if (controller.isGrounded) GroundMove();
- else {
- AirMove();
- }
- //if (Input.GetButtonDown("Jump")) playerVelocity.y = jumpSpeed; // This is just for testing
- // Move the controller
- if (gaussKnockback.magnitude > 0) {
- playerVelocity += gaussKnockback;
- gaussKnockback = Vector3.zero;
- }
- /* This is where you check whether the rocket object that has the script obj_bullet.cs
- * has added any knockback vectors into the list. If so, you add them up to the total knockback
- * allowing the player to be affected by multiple knockbacks at once. */
- for (int i = 0; i < knockback.Count; i++) {
- Vector3 test = (knockback[i].normalized * Mathf.Clamp(3 - knockback[i].magnitude, 0, 1)) * expKnockback;
- playerVelocity += test;
- drag.text = test.magnitude.ToString();
- knockback.RemoveAt(i);
- }
- controller.Move(playerVelocity * Time.deltaTime);
- }
- void Accelerate(Vector3 wishdir, float wishspeed, float accel) {
- float addspeed;
- float accelspeed;
- float currentspeed;
- // This is from Quake source code, not mew's stuff
- currentspeed = Vector3.Dot(playerVelocity, wishdir);
- addspeed = wishspeed - currentspeed;
- if (addspeed <= 0) return;
- accelspeed = accel * wishspeed * Time.deltaTime;
- if (accelspeed > addspeed) accelspeed = addspeed;
- playerVelocity.x += accelspeed * wishdir.x;
- playerVelocity.z += accelspeed * wishdir.z;
- playerVelocity.y += accelspeed * wishdir.y;
- }
- // Happens when the player is in the air
- void AirMove() {
- Vector3 wishdir;
- float accel;
- float scale = CmdScale();
- wishdir = new Vector3(rightmove, 0, forwardmove);
- wishdir = transform.TransformDirection(wishdir);
- wishspeed = wishdir.magnitude * moveSpeed;// *scale;
- wishdir.Normalize();
- if (wishspeed > sideStrafeSpeed) wishspeed = sideStrafeSpeed;
- float turnAngle = Vector3.Dot(wishdir, new Vector3(playerVelocity.x, 0, playerVelocity.z).normalized * 1.3f);
- accel = sideStrafeAcceleration * (1f - Mathf.Clamp(Mathf.Abs(turnAngle), 0, 0.98f));
- //drag.text = (turnAngle * turnAngle).ToString();
- Accelerate(wishdir, wishspeed, accel);
- //playerVelocity += gravity * Vector3.down * Time.deltaTime;
- playerVelocity.y -= gravity * Time.deltaTime;
- if (controller.collisionFlags == CollisionFlags.Above && playerVelocity.y > 0)
- playerVelocity.y -= 100f * Time.deltaTime ;
- }
- void GroundMove() {
- Vector3 wishdir;
- // Do not apply friction if the player is queuing up the next jump
- if (!wishJump) {
- frictionMulti = 1.0f;
- // This adds the delay to friction, so you can do the ground accel stuff between jumps
- Invoke("ApplyFriction", 0.2f);
- } else {
- frictionMulti = 0.0f;
- ApplyFriction();
- }
- wishdir = new Vector3(rightmove, 0, forwardmove);
- wishdir = transform.TransformDirection(wishdir);
- wishdir.Normalize();
- float wishspeed = wishdir.magnitude * moveSpeed;
- Accelerate(wishdir, wishspeed, runAcceleration);
- // THEJUMP (this is by mew, a decent ramp jump functionality)
- if (wishJump) {
- wishJump = false;
- float oldYvel = playerVelocity.y;
- Vector2 inVelHor = new Vector2(playerVelocity.x, playerVelocity.z).normalized;
- Vector2 hitNormalHor = new Vector2(hit.normal.x, hit.normal.z);
- float rampDir = Vector3.Dot(inVelHor, hitNormalHor);
- if (playerVelocity.y < 0) playerVelocity.y = 0;
- Vector3 horVel = new Vector3(playerVelocity.x, 0, playerVelocity.z);
- if (rampDir > 0) {
- playerVelocity += horVel.normalized * Mathf.Abs(oldYvel) * rampDir * downLandBoost;
- } else if (rampDir < 0)
- playerVelocity -= horVel.normalized * Mathf.Abs(oldYvel) * Mathf.Abs(rampDir) * upSlow;
- playerVelocity.y = jumpSpeed * (1 - rampDir * playerVelocity.magnitude * upJumpBoost)
- + rampDir * Mathf.Abs(oldYvel) * downJumpBoost;
- } else playerVelocity.y = 0f;
- }
- // Applies friction to the player, called in both the air and on the ground
- void ApplyFriction() {
- Vector3 vec = playerVelocity;
- float speed;
- float newspeed;
- float control;
- float drop;
- vec.y = 0.0f;
- speed = vec.magnitude;
- drop = 0.0f;
- // Apply friction only if the player is on the ground
- if (controller.isGrounded) {
- control = speed < runDeacceleration ? runDeacceleration : speed;
- drop = control * friction * frictionMulti * Time.deltaTime;
- }
- newspeed = speed - drop;
- playerFriction = newspeed;
- if (newspeed < 0) newspeed = 0;
- if (speed != 0)
- newspeed /= speed;
- playerVelocity.x *= newspeed;
- playerVelocity.z *= newspeed;
- //playerVelocity.y *= newspeed;
- }
- void QueueJump() {
- if (Input.GetButton("Jump") && !wishJump) wishJump = true;
- if (Input.GetButtonUp("Jump")) wishJump = false;
- }
- private float CmdScale() {
- // This is from Quake source code, I have no idea what this does. - mew
- int max;
- float total;
- float scale;
- max = (int)Mathf.Abs(forwardmove);
- if ((int)Mathf.Abs(rightmove) > max) {
- max = (int)Mathf.Abs(rightmove);
- }
- if ((int)Mathf.Abs(upmove) > max) {
- max = (int)Mathf.Abs(upmove);
- }
- if (max <= 0) {
- return 0;
- }
- total = Mathf.Sqrt(forwardmove * forwardmove + rightmove * rightmove);
- scale = moveSpeed * max / (moveScale * total);
- return scale;
- }
- void OnGUI() {
- Vector3 ups;
- ups = controller.velocity;
- ups.y = 0;
- speed.text = (ups * 100).magnitude.ToString("f0");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement