Pastebin PRO Accounts EASTER SPECIAL! For a limited time only get 40% discount on a LIFETIME PRO account! Offer Ends April 2nd!
SHARE
TWEET
Character Movement
a guest
Oct 31st, 2015
203
Never
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- public class CharacterMovement : MonoBehaviour {
- float playerHeight;
- float playerRadius;
- float distanceToGround;
- float rayDistanceToGround;
- float distanceToCeiling;
- float traceceiling;
- float xrotation;
- float xrotation2;
- float xrotation3;
- float deadzone = 0.2f;
- float lastrotation;
- float leftstickvalx;
- float leftstickvaly;
- float leftstickangle;
- float stickAngleTan;
- float stickAngleTan2;
- public bool grounded;
- float frameCount;
- bool groundhitbool;
- bool groundhitraybool;
- bool grounddelaybool;
- bool stepraybool2;
- public bool stickmoving;
- bool stickmovingright;
- bool walljumpraybool;
- bool allowjump;
- bool ledgefall;
- bool stepraybool;
- int ledgecounter;
- float ledgethreshold = 2.5f;
- float dt;
- ///
- float airtime;
- float groundtimer;
- float jumpintervaltimer;
- float fallingtimer;
- float movingtimer;
- float stoptimer;
- ///
- GameObject pwcm;
- GameObject PCstep;
- GameObject PCbase;
- GameObject PCbase2;
- GameObject PCbase3;
- GameObject Indicator;
- GameObject PChar;
- GameObject PCA;
- GameObject PCam;
- GameObject jumptext;
- GameObject playerMC;
- GameObject tgo;
- GameObject PAxis2;
- GameObject Axis2Rotation;
- GameObject Axis2baserotation;
- GameObject Slide_PS;
- GameObject OBwalljump;
- float stickangle3;
- float stickangle3vel;
- bool booluturn1;
- bool booluturn2;
- bool booluturn3;
- bool booluturn4;
- float leftStickAngle2;
- Vector2 stickInput;
- bool lockbool;
- Renderer rend;
- Vector3 runvector;
- Vector3 groundvector;
- Vector3 jumpavgv3;
- Vector4 airupright;
- Vector3 slopeupright;
- Vector3 hitrayv3;
- float maxjoystickval = 0.02f;
- float gdval = 0.02f;
- float gdval2 = 0.02f;
- float rvMag;
- float rvMag2;
- float rvForward;
- float rvRight;
- float rvUp;
- float maxrunvelocity = 6.0f;
- float runacceleration = 5.5f;
- float rightstickvalx;
- float rightstickvaly;
- float velmagvel;
- float vx;
- float vz;
- public float velMag;
- public float velMag2;
- public float deltarotationrate2;
- float fixedangle = 0.0F;
- public float rotationSpeed;
- public float chardeltarotationspeed;
- public float acceleration;
- float baserunspeed;
- float baserunspeed2;
- public float runspeed;
- public float stopspeed;
- public float jumpheight;
- public float playerfallvelocity;
- public float gravity;
- public int rotationtype;
- float axis2float;
- Rigidbody rb;
- float PlayerDrag;
- CapsuleCollider cap;
- MeshCollider meshmc;
- MeshCollider pwcmCol;
- ParticleSystem slideps;
- public Text jt;
- public PhysicMaterial playermat;
- public LayerMask playercolmask;
- void Start () {
- //
- // Slide_PS = GameObject.Find ("SlideParticleSystem");
- PAxis2 = GameObject.Find("PlayerAxis2");
- pwcm = GameObject.Find ("pwcm");
- PCstep = GameObject.Find("PCstep");
- PChar = GameObject.Find ("PlayerCharacter");
- PCA = GameObject.Find("PlayerCameraAnchor");
- PCam = GameObject.Find("MainCamera");
- PCbase = GameObject.Find("PCBase");
- PCbase2 = GameObject.Find("PCBase2");
- PCbase3 = GameObject.Find("PCBase3");
- playerMC = GameObject.Find ("PlayerMidCol");
- tgo = GameObject.Find("testGameObject");
- Axis2Rotation = GameObject.Find("Axis2Rotation");
- Axis2baserotation = GameObject.Find("Axis2baserotation");
- OBwalljump = GameObject.Find("OBwalljump");
- booluturn1 = false;
- booluturn2 = false;
- booluturn3 = false;
- booluturn4 = false;
- slopeupright = tgo.transform.up;
- baserunspeed = 1.0f;
- baserunspeed2 = 1.0f;
- jumptext = GameObject.Find ("JumpText");
- jt = jumptext.GetComponent<Text>();
- rb = PChar.GetComponent<Rigidbody>();
- Indicator = GameObject.Find ("Indicator");
- rend = (Renderer)Indicator.GetComponent<Renderer> ();
- rend.enabled = false;
- Physics.sleepThreshold = 0;
- fallingtimer = 0.0f;
- jumpintervaltimer = 0.0f;
- groundtimer = 0.0f;
- movingtimer = 0.0f;
- stoptimer = 0.0f;
- grounded = true;
- playerHeight = GameObject.Find ("PlayerCharacter").GetComponent<CapsuleCollider>().height;
- playerRadius = GameObject.Find ("PlayerCharacter").GetComponent<CapsuleCollider>().radius;
- cap = PChar.GetComponent<CapsuleCollider>() as CapsuleCollider;
- pwcmCol = pwcm.GetComponent<MeshCollider>() as MeshCollider;
- meshmc = playerMC.GetComponent<MeshCollider>() as MeshCollider;
- slideps = GameObject.Find("SlideparticleSystem").GetComponent<ParticleSystem> () as ParticleSystem;
- PlayerDrag = 1.0f;
- rb.drag = PlayerDrag;
- // cap.enabled = true;
- pwcmCol.enabled = true;
- meshmc.enabled = false;
- playermat.staticFriction = 0.0f;
- playermat.dynamicFriction = 0.0f;
- cap.material = playermat;
- ledgecounter = 0;
- allowjump = false;
- stickmoving = false;
- grounddelaybool = false;
- }
- void Update () {
- //horizontal
- leftstickvalx = Input.GetAxis ("KBHorizontal");
- leftstickvaly = Input.GetAxis ("KBVertical");
- //horizontalRJoy
- rightstickvalx = Input.GetAxis ("Mouse X");
- rightstickvaly = Input.GetAxis ("Mouse Y");
- stickInput = new Vector2 (leftstickvalx, leftstickvaly);
- Vector2 stickInputRight = new Vector2 (rightstickvalx, rightstickvaly);
- rvMag = Mathf.Sqrt ((leftstickvalx * leftstickvalx) + (leftstickvaly * leftstickvaly));
- if (!Input.GetButton ("LockBumper")) {
- lockbool = true;
- } else {
- lockbool = false;
- }
- /*
- if (stickInput.magnitude < deadzone) {
- stickmovingright = false;
- } else {
- stickmovingright = true;
- }
- //deadzoneshift
- if (stickInput.magnitude > 0.6f) {
- deadzone = 0.56f;
- } else {
- deadzone = Mathf.Lerp( deadzone, 0.12f, Time.fixedDeltaTime * 12);
- }
- */
- //DEADZONE
- ///////
- rvRight = Input.GetAxis ("Horizontal") * (runspeed * baserunspeed * baserunspeed2);
- rvForward = Input.GetAxis ("Vertical") * (runspeed * baserunspeed * baserunspeed2);
- ////////
- // tgo.transform.position = new Vector3 (transform.position.x, transform.position.y + 0.2f, transform.position.z);
- tgo.transform.position = PCbase2.transform.position;
- PAxis2.transform.position = transform.position;
- /////////rotate the player
- xrotation = -Input.GetAxis ("Mouse X") * rotationSpeed;
- xrotation2 += xrotation * Time.fixedDeltaTime;
- ///////////
- if (Input.GetButton ("Reset")) {
- transform.position = new Vector3 (0, 1, 0);
- rb.velocity = new Vector3( 0, 0, 0 );
- PCA.transform.position = new Vector3( 0, 1, 0 );
- transform.rotation = Quaternion.AngleAxis (0, Vector3.up);
- PCA.transform.rotation = Quaternion.AngleAxis (0, Vector3.up);
- xrotation2 = 0.0f;
- }
- if ( grounded == true && !Input.GetButton ("Jump") && allowjump == false) {
- allowjump = true;
- }
- vx = rb.velocity.x;
- vz = rb.velocity.z;
- dt = Time.deltaTime;
- frameCount = Time.frameCount;
- if (grounded == true) {
- airtime = 0.0f;
- groundtimer += Time.deltaTime;
- if ( stickInput.magnitude < deadzone ){
- stoptimer += Time.deltaTime;
- }
- else {
- stoptimer = 0.0f;
- }
- if ( stickInput.magnitude > deadzone ){
- movingtimer += Time.deltaTime;
- }
- else {
- movingtimer = 0.0f;
- }
- } else {
- airtime += Time.deltaTime;
- groundtimer = 0.0f;
- }
- if (groundtimer > 0.1f) {
- grounddelaybool = true;
- } else {
- grounddelaybool = false;
- }
- /////a
- jt.text = "rvmag2 " + rvMag2.ToString ()
- + "\ntan angle 2" + stickAngleTan2.ToString ()
- + "\nbase run speed2:" + baserunspeed2.ToString ()
- + "\ngrounded " + grounded.ToString ()
- //+ "\ny velocity " + playerfallvelocity.ToString ()
- + "\nvelmag " + velMag.ToString ()
- + "\nvelmag2 " + velMag2.ToString ()
- + "\n jumpintervaltimer" + jumpintervaltimer.ToString()
- + "\nstickangle2 " + leftStickAngle2.ToString ()
- + "\nmovetime " + movingtimer.ToString ()
- ;
- /*
- jt.text = "jumptimer " + jumpintervaltimer.ToString ()
- + "\ngroundtimer " + groundtimer.ToString ()
- + "\ncanjump " + allowjump.ToString ()
- + "\nledgefall " + ledgefall.ToString ()
- + "\nledgecount " + ledgecounter.ToString ()
- + "\nvelocity " + rb.velocity.ToString ()
- + "\nframecount " + frameCount.ToString ()
- ;
- */
- }
- void FixedUpdate () {
- if (rb.velocity.sqrMagnitude < 0.1f) {
- velMag = 0;
- } else {
- velMag = Mathf.SmoothDamp (velMag, rb.velocity.sqrMagnitude, ref velmagvel, Time.fixedDeltaTime);
- }
- if (stickmoving == true) {
- xrotation3 = Mathf.LerpAngle(xrotation3, xrotation2, deltarotationrate2 * Time.fixedDeltaTime);
- }
- //DEADZONE
- if (stickInput.magnitude < deadzone) {
- stickInput = Vector2.zero;
- leftStickAngle2 = Mathf.LerpAngle (leftStickAngle2, leftStickAngle2, Time.fixedDeltaTime * 2);
- stickmoving = false;
- } else {
- leftstickangle = Mathf.Atan2 (leftstickvalx, leftstickvaly) * Mathf.Rad2Deg;
- leftStickAngle2 = Mathf.LerpAngle (leftStickAngle2, leftstickangle, deltarotationrate2 * Time.fixedDeltaTime);
- //rounds out diagonals into circle
- rvMag2 = Mathf.Clamp ((rvMag - deadzone) / (1 - deadzone), 0.0f, 1.0f);
- stickmoving = true;
- }
- ////////////////////////////////////////////////////////////////
- rb.drag = PlayerDrag;
- rb.angularDrag = 0;
- playerfallvelocity = rb.velocity.y;
- //walljumpable
- RaycastHit walljumpable;
- if (Physics.Raycast (OBwalljump.transform.position, transform.forward, out walljumpable, 1.2f, playercolmask)) {
- walljumpraybool = true;
- } else {
- walljumpraybool = false;
- }
- //grounded
- RaycastHit groundhit;
- if ( Physics.Raycast(PCbase.transform.position, -PCbase.transform.up, out groundhit, 0.04f, playercolmask )) {
- distanceToGround = groundhit.distance;
- groundhitbool = true;
- } else {
- groundhitbool = false;
- }
- /*
- RaycastHit groundhitray;
- if (Physics.Raycast (PCbase2.transform.position, -transform.up, out groundhitray, playercolmask )) {
- rayDistanceToGround = groundhitray.distance;
- groundhitraybool = true;
- } else {
- groundhitraybool = false;
- }
- */
- //step
- RaycastHit stepray;
- if (Physics.Raycast (PCstep.transform.position, -PCstep.transform.up, out stepray, 3.0f, playercolmask )) {
- stepraybool2 = true;
- if ( stepray.distance <= 0.215f ) {
- stepraybool = true;
- } else {
- stepraybool = false;
- }
- } else {
- stepraybool = false;
- stepraybool2 = false;
- }
- Axis2Rotation.transform.rotation = Quaternion.AngleAxis (stickangle3, Axis2baserotation.transform.up)* Quaternion.AngleAxis( -xrotation3, Vector3.up ) * Quaternion.FromToRotation( Vector3.up, Axis2baserotation.transform.up );
- // Axis2Rotation.transform.rotation.y = leftStickAngle2;
- // stickangle3 = Mathf.LerpAngle (stickangle3, leftStickAngle2, 1000 * Time.deltaTime);
- if ( rotationtype == 0 ) {
- if (leftStickAngle2 > 316 || leftStickAngle2 < 44 || leftStickAngle2 > 46 && leftStickAngle2 < 134 || leftStickAngle2 > 136 && leftStickAngle2 < 224 || leftStickAngle2 > 226 && leftStickAngle2 < 314) {
- //character rotation code
- //FIX timer when u-turning (breaks when tapping random direction keys really fast)
- if (leftStickAngle2 > 316 || leftStickAngle2 < 44) {
- if ( lastrotation == 180 ) {
- stickangle3 = 0;
- } else {
- stickangle3 = Mathf.LerpAngle (stickangle3, 0, deltarotationrate2 * Time.fixedDeltaTime);
- }
- lastrotation = 0;
- }
- if (leftStickAngle2 > 46 && leftStickAngle2 < 134) {
- if ( lastrotation == 270 ) {
- stickangle3 = 90;
- } else {
- stickangle3 = Mathf.LerpAngle (stickangle3, 90, deltarotationrate2 * Time.fixedDeltaTime);
- }
- lastrotation = 90;
- }
- if (leftStickAngle2 > 136 && leftStickAngle2 < 224) {
- if ( lastrotation == 0 ) {
- stickangle3 = 180;
- } else {
- stickangle3 = Mathf.LerpAngle (stickangle3, 180, deltarotationrate2 * Time.fixedDeltaTime);
- }
- lastrotation = 180;
- }
- if (leftStickAngle2 > 226 && leftStickAngle2 < 314) {
- if ( lastrotation == 90 ) {
- stickangle3 = 270f;
- } else {
- stickangle3 = Mathf.LerpAngle (stickangle3, 270, deltarotationrate2 * Time.fixedDeltaTime);
- }
- lastrotation = 270f;
- //\\\\\\\\
- /*
- if ( lastrotation == 90 ) {
- booluturn4 = true;
- }
- if ( booluturn4 == true && lastrotation == 90 ) {
- movingtimer = 0.0f;
- lastrotation = 270;
- }
- if ( movingtimer > 0.5f ) {
- stickangle3 = 270;
- lastrotation = 270;
- booluturn4 = false;
- } else {
- if ( booluturn4 == true )
- stickangle3 = Mathf.LerpAngle (stickangle3, lastrotation, 50 * Time.fixedDeltaTime);
- }
- if ( lastrotation != 90 && booluturn4 == false ) {
- stickangle3 = Mathf.LerpAngle (stickangle3, 270, 50 * Time.fixedDeltaTime);
- lastrotation = 270;
- }
- */
- ///////////// //
- }
- } else {
- stickangle3 = Mathf.LerpAngle (stickangle3, lastrotation, deltarotationrate2 * Time.fixedDeltaTime);
- }
- } else {
- stickangle3 = Mathf.LerpAngle (stickangle3, leftStickAngle2, 25 * Time.fixedDeltaTime);
- }
- /* Axis2Rotation.transform.rotation = Mathf.SmoothDampAngle
- ( 1,
- Quaternion.AngleAxis( leftStickAngle2 , Vector3.up ),
- ref axis2float, 5 * Time.deltaTime)
- ;
- */
- if ( groundhitbool == true ) {
- jumpintervaltimer += Time.deltaTime;
- pwcmCol.enabled = true;
- meshmc.enabled = false;
- //stopping slide force
- if ( stickmoving == false && !Input.GetButton("Jump") && jumpintervaltimer > Time.deltaTime ) {
- PlayerDrag = Mathf.Lerp(0.0f, 15.8f, 3.2f * 100 * Time.fixedDeltaTime);
- } else {
- PlayerDrag = 15.8f;
- }
- if ( Input.GetButton ("Jump") && jumpintervaltimer > Time.deltaTime && allowjump == true && grounddelaybool == true ){
- slideps.enableEmission = false;
- transform.up = tgo.transform.up;
- if ( stickmoving == true ){
- transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up )* Quaternion.AngleAxis( -xrotation3, Vector3.up ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- } else {
- transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up )* Quaternion.AngleAxis( -xrotation2, Vector3.up ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- }
- } else {
- ////
- slideps.enableEmission = false;
- transform.up = groundhit.normal;
- if ( stickmoving == true ){
- transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhit.normal )* Quaternion.AngleAxis( -xrotation3, Vector3.up ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- } else {
- transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhit.normal )* Quaternion.AngleAxis( -xrotation2, Vector3.up ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- }
- }
- if ( movingtimer == 0f && velMag > 1f ) {
- slideps.enableEmission = true;
- }
- else {
- slideps.enableEmission = false;
- }
- //ideal values: friction 0.1f | rvUp -10f
- grounded = true;
- // rend.enabled = true;
- playermat.staticFriction = 0.0f;
- playermat.dynamicFriction = 0.0f;
- fallingtimer = 0.0f;
- //////////
- /// stop to full run speed
- if (stickmoving == true){
- baserunspeed2 = Mathf.Lerp(baserunspeed2, 1.0f, Time.fixedDeltaTime * 2.3f);
- }
- else {
- baserunspeed2 = Mathf.Lerp(baserunspeed2, 0.14f, Time.fixedDeltaTime * 3.6f);
- }
- baserunspeed = 1.0f;
- rvUp = -gravity;
- //////////
- rb.AddForce (transform.up * (rvUp * 100* Time.fixedDeltaTime ), ForceMode.Force);
- } else {
- slideps.enableEmission = false;
- transform.up = tgo.transform.up;
- pwcmCol.enabled = false;
- meshmc.enabled = true;
- jumpintervaltimer = 0f;
- // transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhitray.normal ) * Quaternion.AngleAxis( -xrotation2, Vector3.up );
- // transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhitray.normal ) * Quaternion.AngleAxis( -xrotation2 , Vector3.up ) * Quaternion.AngleAxis( leftstickangle , Vector3.up );
- //stick input
- if ( stickmoving == true ){
- transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up )* Quaternion.AngleAxis( -xrotation3, Vector3.up ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- } else {
- transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up )* Quaternion.AngleAxis( -xrotation2, Vector3.up ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- }
- PlayerDrag = 0f;
- grounded = false;
- rvUp = -gravity;
- playermat.staticFriction = 0.0f;
- playermat.dynamicFriction = 0.0f;
- // PlayerDrag = 5.568f;
- // baserunspeed = 0.15f;
- if (stickmoving == true){
- baserunspeed2 = Mathf.Lerp(baserunspeed2, 1.0f, Time.fixedDeltaTime * 4.1f);
- }
- else {
- baserunspeed2 = 0.30f;
- }
- baserunspeed = 0.030f;
- // baserunspeed2 = baserunspeed2;
- fallingtimer += Time.deltaTime;
- rb.AddForce (transform.up * (rvUp * 100 * Time.fixedDeltaTime), ForceMode.Force);
- }
- /////
- //////player movement
- if (stickmoving == true) {
- rb.AddForce (transform.forward * ((rvMag2 * (runspeed * baserunspeed * baserunspeed2)) * runacceleration * 100 )
- * Time.fixedDeltaTime , ForceMode.Force );
- }
- // rb.AddForce( transform.right * rvRight * runacceleration, ForceMode.Force );
- //jumping
- if (grounded == true && Input.GetButton ("Jump") && jumpintervaltimer > Time.deltaTime && allowjump == true) {
- slideps.enableEmission = false;
- PlayerDrag = 0.0f;
- transform.position = PCbase2.transform.position;
- transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up ) * Quaternion.AngleAxis( -xrotation3 , Vector3.up ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- transform.up = tgo.transform.up;
- rb.AddForce (PChar.transform.up * jumpheight, ForceMode.Impulse);
- if ( stickmoving == true ){
- transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhit.normal ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- } else {
- transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhit.normal ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
- }
- jumpintervaltimer = 0.0f;
- allowjump = false;
- airtime = 2.0f;
- } else {
- }
- //need to fix the logic for the jump button when held //0.38f
- // if ( allowjump == true && distanceToGround > 0.12f && distanceToGround <= 0.2f && grounddelaybool == true && groundhitbool == true && stepraybool == false ){
- // add to if....
- if ( allowjump == true && grounded == true && distanceToGround > gdval2 && groundhitbool == true ){
- transform.position = groundhit.point;
- //do magnitude of speed determines float value
- rb.AddForce( transform.up * -76.0f * 100 * Time.fixedDeltaTime, ForceMode.Impulse );
- }
- //ledge falling
- if (grounded == false && allowjump == true && airtime < dt) {
- ledgefall = true;
- }
- }
- }
RAW Paste Data
