SHARE
TWEET

Character Movement

a guest Oct 31st, 2015 203 Never
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4.  
  5. public class CharacterMovement : MonoBehaviour {
  6.  
  7.         float playerHeight;
  8.         float playerRadius;
  9.         float distanceToGround;
  10.         float rayDistanceToGround;
  11.         float distanceToCeiling;
  12.         float traceceiling;
  13.         float xrotation;
  14.         float xrotation2;
  15.         float xrotation3;
  16.         float deadzone = 0.2f;
  17.  
  18.         float lastrotation;
  19.  
  20.         float leftstickvalx;
  21.         float leftstickvaly;
  22.         float leftstickangle;
  23.         float stickAngleTan;
  24.         float stickAngleTan2;
  25.  
  26.         public bool grounded;
  27.         float frameCount;
  28.  
  29.         bool groundhitbool;
  30.         bool groundhitraybool;
  31.         bool grounddelaybool;
  32.         bool stepraybool2;
  33.         public bool stickmoving;
  34.         bool stickmovingright;
  35.  
  36.         bool walljumpraybool;
  37.  
  38.         bool allowjump;
  39.         bool ledgefall;
  40.         bool stepraybool;
  41.         int ledgecounter;
  42.         float ledgethreshold = 2.5f;
  43.         float dt;
  44.  
  45.         ///
  46.         float airtime;
  47.         float groundtimer;
  48.         float jumpintervaltimer;
  49.         float fallingtimer;
  50.         float movingtimer;
  51.         float stoptimer;
  52.         ///
  53.  
  54.         GameObject pwcm;
  55.  
  56.         GameObject PCstep;
  57.         GameObject PCbase;
  58.         GameObject PCbase2;
  59.         GameObject PCbase3;
  60.         GameObject Indicator;
  61.         GameObject PChar;
  62.         GameObject PCA;
  63.         GameObject PCam;
  64.         GameObject jumptext;
  65.         GameObject playerMC;
  66.         GameObject tgo;
  67.         GameObject PAxis2;
  68.         GameObject Axis2Rotation;
  69.         GameObject Axis2baserotation;
  70.         GameObject Slide_PS;
  71.         GameObject OBwalljump;
  72.  
  73.         float stickangle3;
  74.         float stickangle3vel;
  75.  
  76.         bool booluturn1;
  77.         bool booluturn2;
  78.         bool booluturn3;
  79.         bool booluturn4;
  80.  
  81.         float leftStickAngle2;
  82.         Vector2 stickInput;
  83.         bool lockbool;
  84.  
  85.         Renderer rend;
  86.        
  87.         Vector3 runvector;
  88.         Vector3 groundvector;
  89.         Vector3 jumpavgv3;
  90.  
  91.         Vector4 airupright;
  92.  
  93.         Vector3 slopeupright;
  94.         Vector3 hitrayv3;
  95.  
  96.         float maxjoystickval = 0.02f;
  97.  
  98.         float gdval = 0.02f;
  99.         float gdval2 = 0.02f;
  100.  
  101.         float rvMag;
  102.         float rvMag2;
  103.         float rvForward;
  104.         float rvRight;
  105.         float rvUp;
  106.         float maxrunvelocity = 6.0f;
  107.         float runacceleration = 5.5f;
  108.  
  109.         float rightstickvalx;
  110.         float rightstickvaly;
  111.  
  112.         float velmagvel;
  113.         float vx;
  114.         float vz;
  115.         public float velMag;
  116.         public float velMag2;
  117.  
  118.         public float deltarotationrate2;
  119.         float fixedangle = 0.0F;
  120.         public float rotationSpeed;
  121.         public float chardeltarotationspeed;
  122.         public float acceleration;
  123.         float baserunspeed;
  124.         float baserunspeed2;
  125.         public float runspeed;
  126.         public float stopspeed;
  127.         public float jumpheight;
  128.         public float playerfallvelocity;
  129.  
  130.         public float gravity;
  131.  
  132.         public int rotationtype;
  133.  
  134.         float axis2float;
  135.  
  136.         Rigidbody rb;
  137.         float PlayerDrag;
  138.         CapsuleCollider cap;
  139.         MeshCollider meshmc;
  140.         MeshCollider pwcmCol;
  141.  
  142.         ParticleSystem slideps;
  143.        
  144.         public Text jt;
  145.         public PhysicMaterial playermat;
  146.  
  147.         public LayerMask playercolmask;
  148.  
  149.  
  150.         void Start () {
  151.                 //
  152.         //      Slide_PS = GameObject.Find ("SlideParticleSystem");
  153.                 PAxis2 = GameObject.Find("PlayerAxis2");
  154.                 pwcm = GameObject.Find ("pwcm");
  155.                 PCstep = GameObject.Find("PCstep");
  156.                 PChar = GameObject.Find ("PlayerCharacter");
  157.                 PCA = GameObject.Find("PlayerCameraAnchor");
  158.                 PCam = GameObject.Find("MainCamera");
  159.                 PCbase = GameObject.Find("PCBase");
  160.                 PCbase2 = GameObject.Find("PCBase2");
  161.                 PCbase3 = GameObject.Find("PCBase3");
  162.                 playerMC = GameObject.Find ("PlayerMidCol");
  163.                 tgo = GameObject.Find("testGameObject");
  164.                 Axis2Rotation = GameObject.Find("Axis2Rotation");
  165.                 Axis2baserotation = GameObject.Find("Axis2baserotation");
  166.                 OBwalljump = GameObject.Find("OBwalljump");
  167.                 booluturn1 = false;
  168.                 booluturn2 = false;
  169.                 booluturn3 = false;
  170.                 booluturn4 = false;
  171.  
  172.                 slopeupright = tgo.transform.up;
  173.  
  174.                 baserunspeed = 1.0f;
  175.                 baserunspeed2 = 1.0f;
  176.  
  177.                 jumptext = GameObject.Find ("JumpText");
  178.                
  179.                 jt = jumptext.GetComponent<Text>();
  180.                 rb = PChar.GetComponent<Rigidbody>();
  181.  
  182.                 Indicator = GameObject.Find ("Indicator");
  183.                 rend = (Renderer)Indicator.GetComponent<Renderer> ();
  184.                 rend.enabled = false;
  185.  
  186.                 Physics.sleepThreshold = 0;
  187.  
  188.                 fallingtimer = 0.0f;
  189.                 jumpintervaltimer = 0.0f;
  190.                 groundtimer = 0.0f;
  191.                 movingtimer = 0.0f;
  192.                 stoptimer = 0.0f;
  193.                 grounded = true;
  194.  
  195.                 playerHeight = GameObject.Find ("PlayerCharacter").GetComponent<CapsuleCollider>().height;
  196.                 playerRadius = GameObject.Find ("PlayerCharacter").GetComponent<CapsuleCollider>().radius;
  197.  
  198.                 cap = PChar.GetComponent<CapsuleCollider>() as CapsuleCollider;
  199.                 pwcmCol = pwcm.GetComponent<MeshCollider>() as MeshCollider;
  200.  
  201.                 meshmc = playerMC.GetComponent<MeshCollider>() as MeshCollider;
  202.  
  203.                 slideps = GameObject.Find("SlideparticleSystem").GetComponent<ParticleSystem> () as ParticleSystem;
  204.  
  205.                 PlayerDrag = 1.0f;
  206.                 rb.drag = PlayerDrag;
  207.  
  208.  
  209.  
  210.         //      cap.enabled = true;
  211.                 pwcmCol.enabled = true;
  212.  
  213.                 meshmc.enabled = false;
  214.  
  215.  
  216.                 playermat.staticFriction = 0.0f;
  217.                 playermat.dynamicFriction = 0.0f;
  218.                 cap.material = playermat;
  219.  
  220.                 ledgecounter = 0;
  221.                 allowjump = false;
  222.  
  223.                 stickmoving = false;
  224.                 grounddelaybool = false;
  225.         }
  226.  
  227.  
  228.  
  229.  
  230.         void Update () {
  231.  
  232.                 //horizontal
  233.                 leftstickvalx = Input.GetAxis ("KBHorizontal");
  234.                 leftstickvaly = Input.GetAxis ("KBVertical");
  235.                                                                                 //horizontalRJoy
  236.                 rightstickvalx = Input.GetAxis ("Mouse X");
  237.                 rightstickvaly = Input.GetAxis ("Mouse Y");
  238.  
  239.                 stickInput = new Vector2 (leftstickvalx, leftstickvaly);
  240.                 Vector2 stickInputRight = new Vector2 (rightstickvalx, rightstickvaly);
  241.  
  242.                 rvMag = Mathf.Sqrt ((leftstickvalx * leftstickvalx) + (leftstickvaly * leftstickvaly));
  243.  
  244.  
  245.  
  246.                 if (!Input.GetButton ("LockBumper")) {
  247.                         lockbool = true;
  248.                 } else {
  249.                         lockbool = false;
  250.                 }
  251.                
  252.  
  253.                 /*
  254.  
  255.                 if (stickInput.magnitude < deadzone) {
  256.                         stickmovingright = false;
  257.                 } else {
  258.                         stickmovingright = true;
  259.                 }
  260.  
  261.                 //deadzoneshift
  262.                 if (stickInput.magnitude > 0.6f) {
  263.                         deadzone = 0.56f;
  264.                 } else {
  265.                         deadzone = Mathf.Lerp( deadzone, 0.12f, Time.fixedDeltaTime * 12);
  266.                 }
  267. */
  268.  
  269.                 //DEADZONE
  270.  
  271.  
  272.  
  273.  
  274.                 ///////
  275.                 rvRight = Input.GetAxis ("Horizontal") * (runspeed * baserunspeed * baserunspeed2);
  276.                 rvForward = Input.GetAxis ("Vertical") * (runspeed * baserunspeed * baserunspeed2);
  277.                 ////////
  278.                
  279.                
  280.                 //      tgo.transform.position = new Vector3 (transform.position.x, transform.position.y + 0.2f, transform.position.z);
  281.                 tgo.transform.position = PCbase2.transform.position;
  282.                 PAxis2.transform.position = transform.position;
  283.  
  284.  
  285.                 /////////rotate the player
  286.                 xrotation = -Input.GetAxis ("Mouse X") * rotationSpeed;
  287.                 xrotation2 += xrotation * Time.fixedDeltaTime;
  288.  
  289.  
  290.                 ///////////
  291.        
  292.                 if (Input.GetButton ("Reset")) {
  293.                         transform.position = new Vector3 (0, 1, 0);
  294.  
  295.                         rb.velocity = new Vector3( 0, 0, 0 );
  296.                         PCA.transform.position = new Vector3( 0, 1, 0 );
  297.                         transform.rotation = Quaternion.AngleAxis (0, Vector3.up);
  298.                         PCA.transform.rotation = Quaternion.AngleAxis (0, Vector3.up);
  299.                         xrotation2 = 0.0f;
  300.                 }
  301.  
  302.  
  303.                 if ( grounded == true && !Input.GetButton ("Jump") && allowjump == false) {
  304.                         allowjump = true;
  305.                 }
  306.        
  307.                 vx = rb.velocity.x;
  308.                 vz = rb.velocity.z;
  309.                 dt = Time.deltaTime;
  310.                 frameCount = Time.frameCount;
  311.  
  312.                 if (grounded == true) {
  313.                         airtime = 0.0f;
  314.                         groundtimer += Time.deltaTime;
  315.  
  316.                         if ( stickInput.magnitude < deadzone ){
  317.                                 stoptimer += Time.deltaTime;
  318.                         }
  319.                         else {
  320.                                 stoptimer = 0.0f;
  321.                         }
  322.  
  323.                         if ( stickInput.magnitude > deadzone ){
  324.                                 movingtimer += Time.deltaTime;
  325.                         }
  326.                         else {
  327.                                 movingtimer = 0.0f;
  328.                         }
  329.                 } else {
  330.                         airtime += Time.deltaTime;
  331.                         groundtimer = 0.0f;
  332.                 }
  333.  
  334.                 if (groundtimer > 0.1f) {
  335.                         grounddelaybool = true;
  336.                 } else {
  337.                         grounddelaybool = false;
  338.                 }
  339.                 /////a
  340.                
  341.  
  342.  
  343.  
  344.                 jt.text = "rvmag2 " + rvMag2.ToString ()
  345.                 + "\ntan angle 2" + stickAngleTan2.ToString ()
  346.                 + "\nbase run speed2:" + baserunspeed2.ToString ()
  347.                 + "\ngrounded " + grounded.ToString ()
  348.                 //+ "\ny velocity  " + playerfallvelocity.ToString ()
  349.                                 + "\nvelmag " + velMag.ToString ()
  350.                                 + "\nvelmag2 " + velMag2.ToString ()
  351.                          + "\n jumpintervaltimer" + jumpintervaltimer.ToString()
  352.                                 + "\nstickangle2 " + leftStickAngle2.ToString ()
  353.                                 + "\nmovetime " + movingtimer.ToString ()
  354.                        
  355.                                 ;
  356.                 /*
  357.                 jt.text = "jumptimer " + jumpintervaltimer.ToString ()
  358.                         + "\ngroundtimer " + groundtimer.ToString ()
  359.                         + "\ncanjump " + allowjump.ToString ()
  360.                         + "\nledgefall " + ledgefall.ToString ()
  361.                         + "\nledgecount " + ledgecounter.ToString ()
  362.                         + "\nvelocity " + rb.velocity.ToString ()
  363.                         + "\nframecount " + frameCount.ToString ()
  364.                                 ;
  365.                 */
  366.  
  367.  
  368.  
  369.         }
  370.  
  371.  
  372.  
  373.         void FixedUpdate () {
  374.  
  375.                 if (rb.velocity.sqrMagnitude < 0.1f) {
  376.                         velMag = 0;
  377.                 } else {
  378.                         velMag = Mathf.SmoothDamp (velMag, rb.velocity.sqrMagnitude, ref velmagvel, Time.fixedDeltaTime);
  379.                 }
  380.  
  381.  
  382.                 if (stickmoving == true) {
  383.                         xrotation3 = Mathf.LerpAngle(xrotation3, xrotation2, deltarotationrate2 * Time.fixedDeltaTime);
  384.                 }
  385.  
  386.  
  387.                 //DEADZONE
  388.                 if (stickInput.magnitude < deadzone) {
  389.                         stickInput = Vector2.zero;
  390.                         leftStickAngle2 = Mathf.LerpAngle (leftStickAngle2, leftStickAngle2, Time.fixedDeltaTime * 2);
  391.                         stickmoving = false;
  392.                 } else {
  393.                        
  394.                         leftstickangle = Mathf.Atan2 (leftstickvalx, leftstickvaly) * Mathf.Rad2Deg;
  395.                         leftStickAngle2 = Mathf.LerpAngle (leftStickAngle2, leftstickangle, deltarotationrate2 * Time.fixedDeltaTime);
  396.                        
  397.                         //rounds out diagonals into circle
  398.                         rvMag2 = Mathf.Clamp ((rvMag - deadzone) / (1 - deadzone), 0.0f, 1.0f);
  399.                         stickmoving = true;
  400.                 }
  401.  
  402.  
  403. ////////////////////////////////////////////////////////////////
  404.  
  405.                 rb.drag = PlayerDrag;
  406.                 rb.angularDrag = 0;
  407.  
  408.                 playerfallvelocity = rb.velocity.y;
  409.  
  410.                 //walljumpable
  411.                 RaycastHit walljumpable;
  412.                 if (Physics.Raycast (OBwalljump.transform.position, transform.forward, out walljumpable, 1.2f, playercolmask)) {
  413.                         walljumpraybool = true;
  414.                 } else {
  415.                         walljumpraybool = false;
  416.                 }
  417.  
  418.                 //grounded
  419.                 RaycastHit groundhit;
  420.                 if ( Physics.Raycast(PCbase.transform.position, -PCbase.transform.up, out groundhit, 0.04f, playercolmask )) {
  421.                         distanceToGround = groundhit.distance;
  422.                         groundhitbool = true;
  423.                 } else {
  424.                         groundhitbool = false;
  425.                 }
  426.  
  427.                 /*
  428.                 RaycastHit groundhitray;
  429.                 if (Physics.Raycast (PCbase2.transform.position, -transform.up, out groundhitray, playercolmask )) {
  430.                         rayDistanceToGround = groundhitray.distance;
  431.                         groundhitraybool = true;
  432.                 } else {
  433.                         groundhitraybool = false;
  434.                 }
  435.                 */
  436.  
  437.                 //step
  438.                 RaycastHit stepray;
  439.                 if (Physics.Raycast (PCstep.transform.position, -PCstep.transform.up, out stepray, 3.0f, playercolmask )) {
  440.                         stepraybool2 = true;
  441.                         if ( stepray.distance <= 0.215f ) {
  442.                         stepraybool = true;
  443.                         } else {
  444.                                 stepraybool = false;
  445.                         }
  446.                 } else {
  447.                         stepraybool = false;
  448.                         stepraybool2 = false;
  449.                 }
  450.  
  451.                 Axis2Rotation.transform.rotation = Quaternion.AngleAxis (stickangle3, Axis2baserotation.transform.up)* Quaternion.AngleAxis( -xrotation3, Vector3.up ) * Quaternion.FromToRotation( Vector3.up, Axis2baserotation.transform.up );
  452.         //      Axis2Rotation.transform.rotation.y = leftStickAngle2;
  453.                 //              stickangle3 = Mathf.LerpAngle (stickangle3, leftStickAngle2, 1000 * Time.deltaTime);
  454.  
  455.                 if ( rotationtype == 0 ) {
  456.                 if (leftStickAngle2 > 316 || leftStickAngle2 < 44 || leftStickAngle2 > 46 && leftStickAngle2 < 134 || leftStickAngle2 > 136 && leftStickAngle2 < 224 || leftStickAngle2 > 226 && leftStickAngle2 < 314) {
  457.                         //character rotation code
  458.                         //FIX timer when u-turning (breaks when tapping random direction keys really fast)
  459.                         if (leftStickAngle2 > 316 || leftStickAngle2 < 44) {
  460.  
  461.                                 if ( lastrotation == 180 ) {
  462.                                         stickangle3 = 0;
  463.                                 } else {
  464.                                         stickangle3 = Mathf.LerpAngle (stickangle3, 0, deltarotationrate2 * Time.fixedDeltaTime);
  465.                                 }
  466.  
  467.                                 lastrotation = 0;
  468.  
  469.                         }
  470.                         if (leftStickAngle2 > 46 && leftStickAngle2 < 134) {
  471.  
  472.                                 if ( lastrotation == 270 ) {
  473.                                         stickangle3 = 90;
  474.                                 } else {
  475.                                         stickangle3 = Mathf.LerpAngle (stickangle3, 90, deltarotationrate2 * Time.fixedDeltaTime);
  476.                                 }
  477.  
  478.                                 lastrotation = 90;
  479.  
  480.                         }
  481.                         if (leftStickAngle2 > 136 && leftStickAngle2 < 224) {
  482.  
  483.                                 if ( lastrotation == 0 ) {
  484.                                         stickangle3 = 180;
  485.                                 } else {
  486.                                         stickangle3 = Mathf.LerpAngle (stickangle3, 180, deltarotationrate2 * Time.fixedDeltaTime);
  487.                                 }
  488.  
  489.                                 lastrotation = 180;
  490.  
  491.  
  492.                         }
  493.                         if (leftStickAngle2 > 226 && leftStickAngle2 < 314) {
  494.  
  495.                                 if ( lastrotation == 90 ) {
  496.                                         stickangle3 = 270f;
  497.                                 } else {
  498.                                         stickangle3 = Mathf.LerpAngle (stickangle3, 270, deltarotationrate2 * Time.fixedDeltaTime);
  499.                                 }
  500.  
  501.                                 lastrotation = 270f;
  502.  
  503.  
  504.  
  505.                                 //\\\\\\\\
  506.                                 /*
  507.                                 if ( lastrotation == 90 ) {
  508.                                         booluturn4 = true;
  509.                                 }
  510.                                
  511.                                 if ( booluturn4 == true && lastrotation == 90 ) {
  512.                                         movingtimer = 0.0f;
  513.                                         lastrotation = 270;
  514.                                 }
  515.                                
  516.                                 if ( movingtimer > 0.5f ) {
  517.                                         stickangle3 = 270;
  518.                                         lastrotation = 270;
  519.                                         booluturn4 = false;
  520.                                 } else {
  521.                                         if ( booluturn4 == true )
  522.                                                 stickangle3 = Mathf.LerpAngle (stickangle3, lastrotation, 50 * Time.fixedDeltaTime);
  523.                                 }
  524.                                
  525.                                 if ( lastrotation != 90 && booluturn4 == false ) {
  526.                                         stickangle3 = Mathf.LerpAngle (stickangle3, 270, 50 * Time.fixedDeltaTime);
  527.                                         lastrotation = 270;
  528.                                 }
  529.                         */
  530.                                 /////////////   //
  531.  
  532.                                 }
  533.                
  534.                         } else {
  535.                                 stickangle3 = Mathf.LerpAngle (stickangle3, lastrotation, deltarotationrate2 * Time.fixedDeltaTime);
  536.                         }
  537.                 } else {
  538.                         stickangle3 = Mathf.LerpAngle (stickangle3, leftStickAngle2, 25 * Time.fixedDeltaTime);
  539.                 }
  540. /*              Axis2Rotation.transform.rotation = Mathf.SmoothDampAngle
  541.                         ( 1,
  542.                          Quaternion.AngleAxis( leftStickAngle2 , Vector3.up ),
  543.                          ref axis2float, 5 * Time.deltaTime)
  544.                                 ;
  545.  
  546. */
  547.  
  548.  
  549.  
  550.                 if ( groundhitbool == true ) {
  551.  
  552.                                 jumpintervaltimer += Time.deltaTime;
  553.                                 pwcmCol.enabled = true;
  554.                                 meshmc.enabled = false;
  555.  
  556.                                 //stopping slide force
  557.                                 if ( stickmoving == false && !Input.GetButton("Jump") && jumpintervaltimer > Time.deltaTime ) {
  558.                                         PlayerDrag = Mathf.Lerp(0.0f, 15.8f, 3.2f * 100 * Time.fixedDeltaTime);
  559.                                 } else {
  560.                                         PlayerDrag = 15.8f;
  561.                                 }
  562.  
  563.                                 if ( Input.GetButton ("Jump") && jumpintervaltimer > Time.deltaTime && allowjump == true && grounddelaybool == true ){
  564.                                         slideps.enableEmission = false;
  565.                                         transform.up = tgo.transform.up;
  566.                                         if ( stickmoving == true ){
  567.                                         transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up )* Quaternion.AngleAxis( -xrotation3, Vector3.up )   * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  568.                                         } else {
  569.                                                 transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up )* Quaternion.AngleAxis( -xrotation2, Vector3.up )   * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  570.                                                 }
  571.                                 } else {
  572.                                         ////
  573.                                         slideps.enableEmission = false;
  574.                                         transform.up = groundhit.normal;
  575.                                         if ( stickmoving == true ){
  576.                                                 transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhit.normal )* Quaternion.AngleAxis( -xrotation3, Vector3.up )   * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  577.                                         } else {
  578.                                                 transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhit.normal )* Quaternion.AngleAxis( -xrotation2, Vector3.up )   * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  579.                                         }
  580.                                 }
  581.  
  582.  
  583.                                
  584.                                 if ( movingtimer == 0f && velMag > 1f ) {
  585.                                         slideps.enableEmission = true;
  586.                                 }
  587.                                 else {
  588.                                         slideps.enableEmission = false;
  589.                                 }
  590.  
  591.  
  592.                                 //ideal values: friction 0.1f | rvUp -10f
  593.                                 grounded = true;
  594.                         //      rend.enabled = true;
  595.                                 playermat.staticFriction = 0.0f;
  596.                                 playermat.dynamicFriction = 0.0f;
  597.  
  598.                                 fallingtimer = 0.0f;
  599.  
  600.                                 //////////
  601.                                 /// stop to full run speed
  602.                                 if (stickmoving == true){
  603.                                         baserunspeed2 = Mathf.Lerp(baserunspeed2, 1.0f, Time.fixedDeltaTime * 2.3f);
  604.                                 }
  605.                                 else {
  606.                                         baserunspeed2 = Mathf.Lerp(baserunspeed2, 0.14f, Time.fixedDeltaTime * 3.6f);
  607.  
  608.                                 }
  609.  
  610.                                 baserunspeed = 1.0f;
  611.  
  612.                                 rvUp = -gravity;
  613.  
  614.                                 //////////
  615.  
  616.                                 rb.AddForce (transform.up * (rvUp * 100* Time.fixedDeltaTime ), ForceMode.Force);
  617.  
  618.                         } else {
  619.  
  620.                                 slideps.enableEmission = false;
  621.                                 transform.up = tgo.transform.up;
  622.  
  623.                                 pwcmCol.enabled = false;
  624.                                 meshmc.enabled = true;
  625.  
  626.                                 jumpintervaltimer = 0f;
  627.  
  628.                         //      transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhitray.normal ) * Quaternion.AngleAxis( -xrotation2, Vector3.up );
  629.                         //      transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhitray.normal ) * Quaternion.AngleAxis( -xrotation2 , Vector3.up )  * Quaternion.AngleAxis( leftstickangle , Vector3.up );
  630.                                 //stick input
  631.                                 if ( stickmoving == true ){
  632.                                         transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up )* Quaternion.AngleAxis( -xrotation3, Vector3.up )   * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  633.                                 } else {
  634.                                         transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up )* Quaternion.AngleAxis( -xrotation2, Vector3.up ) * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  635.                                 }
  636.                                
  637.                                 PlayerDrag = 0f;
  638.                                 grounded = false;
  639.                                 rvUp = -gravity;
  640.                                 playermat.staticFriction = 0.0f;
  641.                                 playermat.dynamicFriction = 0.0f;
  642.  
  643.  
  644.         //                      PlayerDrag = 5.568f;
  645.         //                      baserunspeed = 0.15f;
  646.  
  647.                
  648.                                 if (stickmoving == true){
  649.                                         baserunspeed2 = Mathf.Lerp(baserunspeed2, 1.0f, Time.fixedDeltaTime * 4.1f);
  650.                                 }
  651.                                 else {
  652.                                         baserunspeed2 = 0.30f;
  653.                                 }
  654.        
  655.                                 baserunspeed = 0.030f;
  656.                 //              baserunspeed2 = baserunspeed2;
  657.                                 fallingtimer += Time.deltaTime;
  658.  
  659.                                 rb.AddForce (transform.up * (rvUp * 100 * Time.fixedDeltaTime), ForceMode.Force);
  660.  
  661.                 }
  662.  
  663.  
  664.  
  665.                 /////
  666.                 //////player movement
  667.                 if (stickmoving == true) {
  668.                         rb.AddForce (transform.forward * ((rvMag2 * (runspeed * baserunspeed * baserunspeed2)) * runacceleration * 100 )
  669.                                      * Time.fixedDeltaTime , ForceMode.Force );
  670.                 }
  671.         //      rb.AddForce( transform.right * rvRight * runacceleration, ForceMode.Force );
  672.  
  673.  
  674.  
  675.                 //jumping
  676.                 if (grounded == true && Input.GetButton ("Jump") && jumpintervaltimer > Time.deltaTime && allowjump == true) {
  677.                         slideps.enableEmission = false;
  678.                         PlayerDrag = 0.0f;
  679.                         transform.position = PCbase2.transform.position;
  680.                         transform.rotation = Quaternion.FromToRotation( Vector3.up, tgo.transform.up ) * Quaternion.AngleAxis( -xrotation3 , Vector3.up )  * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  681.                         transform.up = tgo.transform.up;
  682.                         rb.AddForce (PChar.transform.up * jumpheight, ForceMode.Impulse);
  683.                         if ( stickmoving == true ){
  684.                                 transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhit.normal )   * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  685.                         } else {
  686.                                 transform.rotation = Quaternion.FromToRotation( Vector3.up, groundhit.normal )   * Quaternion.AngleAxis( leftStickAngle2 , Vector3.up );
  687.                         }
  688.                         jumpintervaltimer = 0.0f;
  689.                         allowjump = false;
  690.                         airtime = 2.0f;
  691.                 } else {
  692.                 }
  693.                  
  694.                 //need to fix the logic for the jump button when held     //0.38f
  695.         //      if ( allowjump == true && distanceToGround > 0.12f && distanceToGround <= 0.2f && grounddelaybool == true && groundhitbool == true && stepraybool == false ){
  696.  
  697.                 // add to if....
  698.                 if ( allowjump == true && grounded == true && distanceToGround > gdval2 && groundhitbool == true ){
  699.                         transform.position = groundhit.point;
  700.                         //do magnitude of speed determines float value
  701.                         rb.AddForce( transform.up * -76.0f * 100 * Time.fixedDeltaTime, ForceMode.Impulse );
  702.                 }
  703.  
  704.                 //ledge falling
  705.                 if (grounded == false && allowjump == true && airtime < dt) {
  706.                         ledgefall = true;
  707.                 }
  708.  
  709.  
  710.  
  711.         }
  712. }
RAW Paste Data
Top