Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using FMOD.Studio;
- public class PlayerMovement : MonoBehaviour
- {
- public float maxSpeed; //The speed of the player and segway.
- public float minSpeed;
- private float SPEED; //The local variable of SPEED so we only need to multiply it by fixedDeltaTime once.
- public float maxAcceleration; //The acceleration.
- private float acceleration;
- public float maxDeceleration;
- private float deceleration;
- public float tiltSpeed; //The speed at which the player and segway tilts.
- public float fallTilt; //The tilt at which the player falls off the segway regardless of their attempts to stay on.
- public bool gravityOn; //Whether or not gravity effects the object.
- public float gravityOnset; //The angle at which gravity begins to affect the player.
- public float gravityIntensity; //How intense gravity is on the players rotation.
- public float gravityBias;
- //private Quaternion rotGoal; //The rotation goal for slerp and lerp.
- [HideInInspector]
- public float xLock; //The x coordinate the player is locked to on straightaways.
- public GameObject ragdollPrefab; //The players' ragdoll prefab to be used on death.
- public string joystickYInput; //The joystick y coordinate input.
- public KeyCode forwardKey; //Debugging forward key.
- public KeyCode backwardKey; //Debugging backwards key.
- private Rigidbody rb; //The rigidbody component.
- private float joystickAlpha = 0.25f; //The joysticks' alpha.
- private float leftJoyYPerc; //The joysticks' x movement.
- //Pathing Variables.
- public GameObject pathnodeList; //The list of nodes which the player travels along.
- [HideInInspector]
- public int currNode; //The node the player is currently traveling towards.
- [HideInInspector]
- public int nextNode; //The node the player will next travel towards.
- [HideInInspector]
- public int prevNode; //The node the player has last reached.
- //[HideInInspector]
- //public RigidbodyConstraints origRestraints; //The constraints which the player must abide by.
- [HideInInspector]
- public bool backwardsLock; //True == the player cannot go backwards.
- [HideInInspector]
- public Vector3 oldVelocity; //The old velocity.
- private Vector3 startRot;
- public GameObject warningSpot;
- //Destruction Variables
- public GameObject segway;
- public GameObject lance;
- public GameObject armourChest;
- //Ragdoll Exaggeration
- [HideInInspector]
- public bool hitByLance;
- [HideInInspector]
- public bool resetHitByLance;
- public float hitByLanceTimeLeniency;
- private float exagTimer;
- //FMOD
- FMOD.Studio.EventInstance Warning;
- FMOD.Studio.ParameterInstance Player;
- FMOD.Studio.ParameterInstance Urgency;
- FMOD.Studio.ParameterInstance TurnOn;
- FMOD.Studio.ParameterInstance TurnOff;
- //Animations
- private Animator animator;
- //private float origAniSpeed;
- public float percLeanAniAngle; //Percentage the player needs to be in order to play the falling off animations
- public AllCrowdController mySide;
- public AllCrowdController oppSide;
- void Awake()
- {
- rb = GetComponent<Rigidbody>();
- xLock = rb.position[0];
- startRot = rb.rotation.eulerAngles;
- backwardsLock = false;
- exagTimer = hitByLanceTimeLeniency;
- rb.centerOfMass += new Vector3(0.0f, 1.0f, 0.0f);
- //Set the player's nodes.
- prevNode = 0;
- currNode = 1;
- nextNode = 2;
- //origRestraints = rb.constraints;
- animator = this.GetComponent<Animator>();
- //origAniSpeed = animator.speed;
- animator.SetFloat("LeanAngle", 0.0f);
- }
- void Start()
- {
- //FMOD
- Warning = FMOD_StudioSystem.instance.GetEvent("event:/Segway/Warning");
- if(Warning.getParameter("Player", out Player) != FMOD.RESULT.OK)
- {
- Debug.LogError("player param not found");
- return;
- }
- if(Warning.getParameter("Urgency", out Urgency) != FMOD.RESULT.OK)
- {
- Debug.LogError("urgency param not found");
- return;
- }
- if(Warning.getParameter("TurnOn", out TurnOn) != FMOD.RESULT.OK)
- {
- Debug.LogError("turnon param not found");
- return;
- }
- if(Warning.getParameter("TurnOff", out TurnOff) != FMOD.RESULT.OK)
- {
- Debug.LogError("turnoff param not found");
- return;
- }
- if(tag == "P1")
- {
- Player.setValue(1);
- } else{
- Player.setValue(2);
- }
- if(warningSpot == null && GameObject.Find ("FMODListener") != null)
- {
- warningSpot = GameObject.Find("FMODListener");
- }
- TurnOn.setValue(1);
- TurnOff.setValue(0);
- Warning.start ();
- TurnOn.setValue(0);
- PLAYBACK_STATE playback;
- Warning.getPlaybackState(out playback);
- }
- void FixedUpdate()
- {
- Update3DAttributes ();
- if (!LoopManager.Instance.getLocked(this.gameObject))
- {
- Vector3 dirToCurrNode = (pathnodeList.transform.GetChild(currNode).position - rb.position).normalized;
- leftJoyYPerc = Input.GetAxisRaw(joystickYInput); //Get the left-stick's Y
- SPEED = maxSpeed * Time.fixedDeltaTime;
- acceleration = maxAcceleration * Time.fixedDeltaTime;
- //Debug Movement w/o controller
- if (Input.GetKey(forwardKey))
- {
- leftJoyYPerc = 1.0f;
- }
- else if (Input.GetKey(backwardKey))
- {
- leftJoyYPerc = -1.0f;
- }
- //Stop backwards movement if Pathnode has locked it.
- if (leftJoyYPerc < 0.0f && backwardsLock)
- {
- leftJoyYPerc = 0.0f;
- }
- //Avoid joystick input error with an alpha. (My joystick reads ~0.03 when at default)
- if (leftJoyYPerc >= joystickAlpha)
- {
- Vector3 possibleVel = rb.velocity + dirToCurrNode * (acceleration * leftJoyYPerc);
- if (possibleVel.magnitude > SPEED && Vector3.Angle( rb.velocity, dirToCurrNode ) <= 180.0f ) //If we're going faster than the designated max speed.
- {
- rb.velocity = dirToCurrNode * SPEED; //Set us at the max speed.
- }
- else
- {
- rb.velocity = possibleVel;
- }
- //Lean forward depending on the amount the left controllers' stick is pushed.
- if (leftJoyYPerc * fallTilt >= rb.rotation.eulerAngles[0] || rb.rotation.eulerAngles[0] > fallTilt)
- {
- SlerpRot(Quaternion.Euler(leftJoyYPerc * fallTilt, rb.rotation.eulerAngles[1], 0.0f), Time.fixedDeltaTime * tiltSpeed);
- }
- }
- else if (leftJoyYPerc <= -joystickAlpha)
- {
- float sp = minSpeed * Time.fixedDeltaTime;
- deceleration = maxDeceleration * Time.fixedDeltaTime;
- //Lean backward depending on the amount the left controllers' stick is pulled.
- Vector3 possibleVel = rb.velocity + dirToCurrNode * (deceleration * leftJoyYPerc);
- if( possibleVel.magnitude < sp && Vector3.Angle( rb.velocity, dirToCurrNode ) <= 180.0f )
- {
- rb.velocity = dirToCurrNode * sp;
- }
- else
- {
- rb.velocity = possibleVel;
- }
- if (leftJoyYPerc * fallTilt <= rb.rotation.eulerAngles[0] - 360.0f || rb.rotation.eulerAngles[0] < fallTilt)
- {
- SlerpRot(Quaternion.Euler(leftJoyYPerc * fallTilt, rb.rotation.eulerAngles[1], 0.0f), Time.fixedDeltaTime * tiltSpeed);
- }
- }
- if (Mathf.Abs(rb.rotation.eulerAngles[0] - 180.0f) <= 180.0f - fallTilt || Mathf.Abs(rb.rotation.eulerAngles[2] - 180.0f) <= 180.0f - fallTilt) //If the Player is tilting too far off the base rotation.
- {
- FallOff(); //Reset the level.
- }
- float rotIntensity = (rb.rotation.eulerAngles[0] >= 180.0f) ? (rb.rotation.eulerAngles[0] - 360.0f) / fallTilt : rb.rotation.eulerAngles[0] / fallTilt;
- Urgency.setValue(Mathf.Abs(rotIntensity));
- if (Mathf.Abs(rotIntensity) >= percLeanAniAngle) //Falling
- {
- //animator.SetFloat("LeanAngle", rotIntensity);
- animator.SetBool("isFalling", true);
- }
- else
- {
- animator.SetBool("isFalling", false);
- //animator.SetFloat("LeanAngle", 0.0f);
- }
- //Falls forward/backward gradually because of gravity.
- if (gravityOn) //Allows for turning off of gravity for easy debugging.
- {
- if ((rb.rotation.eulerAngles[0] >= gravityOnset) || (rb.rotation.eulerAngles[0] <= 360.0f - gravityOnset)) //If the player is past the point where gravity is applied.
- {
- float gBias = (rb.rotation.eulerAngles[0] >= 180.0f) ? -gravityBias : gravityBias;
- rb.AddRelativeTorque(new Vector3((rotIntensity + gBias) * gravityIntensity * Time.fixedDeltaTime, 0.0f, 0.0f));
- }
- else if (Mathf.Abs(leftJoyYPerc) < joystickAlpha)
- {
- SlerpRot(Quaternion.Euler(0.0f, rb.rotation.eulerAngles[1], 0.0f), Time.fixedDeltaTime);
- }
- }
- if (Vector3.Angle(transform.forward, rb.velocity) <= 90.0f)
- {
- rb.drag = 0.0f;
- }
- else
- {
- rb.drag = 2.0f;
- }
- //Orient towards goal node.
- Quaternion lookRot = Quaternion.LookRotation(pathnodeList.transform.GetChild(currNode).position - rb.position, transform.up);
- SlerpRot(Quaternion.Euler(new Vector3(rb.rotation.eulerAngles[0], lookRot.eulerAngles[1], rb.rotation.eulerAngles[2])), Time.fixedDeltaTime * 7.0f);
- }
- else
- {
- SlerpRot(Quaternion.Euler(0.0f, startRot[1], startRot[2]), tiltSpeed * Time.fixedDeltaTime);
- animator.SetBool("isFalling", false);
- //animator.SetFloat("LeanAngle", 0.0f);
- }
- if (Vector3.Angle(transform.forward, rb.velocity) <= 90.0f) //Adjust velocity to go towards next node if knocked backwards.
- {
- rb.velocity = (pathnodeList.transform.GetChild(currNode).position - rb.position).normalized * rb.velocity.magnitude;
- }
- else //Adjust velocity to go towards previous node if knocked backwards.
- {
- rb.velocity = (pathnodeList.transform.GetChild(prevNode).position - rb.position).normalized * rb.velocity.magnitude;
- }
- oldVelocity = rb.velocity;
- rb.rotation = Quaternion.Euler(rb.rotation.eulerAngles[0], startRot[1], startRot[2]);
- }
- void Update()
- {
- if( resetHitByLance )
- {
- if( exagTimer <= 0 )
- {
- resetHitByLance = false;
- hitByLance = false;
- exagTimer = hitByLanceTimeLeniency;
- }
- else
- {
- exagTimer -= Time.deltaTime;
- }
- }
- }
- void SlerpRot( Quaternion rotGoal, float delta )
- {
- rb.rotation = Quaternion.Slerp(rb.rotation, rotGoal, delta);
- }
- void FallOff()
- {
- mySide.RoundOverFell(oppSide);
- TurnOn.setValue(0);
- TurnOff.setValue(1);
- //Detach Segway from player and bring it back to default position.
- IndependentSegway seg = segway.GetComponent<IndependentSegway>();
- if( seg )
- {
- seg.DetachFromPlayer();
- }
- //Detach lance from player and let it travel it's own trajectory.
- if (lance)
- {
- lance.transform.parent = null;
- Rigidbody lanceRB = lance.AddComponent<Rigidbody>();
- lance.AddComponent<FallCollision>();
- if (lanceRB)
- {
- lanceRB.velocity = rb.velocity;
- }
- lance.layer = 0;
- }
- if(armourChest)
- {
- chestArmour aDet = armourChest.GetComponent<chestArmour>();
- if( aDet)
- {
- aDet.breakApart(rb.velocity);
- }
- }
- //Delete the player and create a ragdoll to simulate the player falling.
- GameObject ragdoll = (GameObject)GameObject.Instantiate( ragdollPrefab, rb.position, rb.rotation );
- ragdoll.tag = gameObject.tag;
- if( hitByLance == true )
- {
- PlayerRagdoll pRag = ragdoll.GetComponent<PlayerRagdoll>();
- if( pRag )
- {
- pRag.knockedOffByLance = true;
- }
- }
- ScoreManager.AddScore(this.gameObject, 1);
- LoopManager.Instance.roundEnd();
- Destroy( this.gameObject );
- }
- public void TurnWarningOff()
- {
- TurnOn.setValue(0);
- TurnOff.setValue(1);
- }
- public void TurnWarningOn()
- {
- TurnOn.setValue(0);
- TurnOff.setValue(0);
- }
- void Update3DAttributes()
- {
- if(warningSpot == null && GameObject.Find ("FMODListener") != null)
- {
- warningSpot = GameObject.Find("FMODListener");
- }
- if (Warning != null && Warning.isValid())
- {
- var attributes = FMOD.Studio.UnityUtil.to3DAttributes(warningSpot);
- if(Warning.set3DAttributes(attributes) != FMOD.RESULT.OK)
- {
- Debug.LogError("could not set 3D sound attributes");
- return;
- }
- }
- }
- void OnDestroy()
- {
- if(Warning != null)
- {
- Warning.stop (STOP_MODE.IMMEDIATE);
- Warning.release();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement