Advertisement
Guest User

Jittery Movement

a guest
May 24th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.81 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class RaycastMovement : MonoBehaviour
  5. {
  6.  
  7.     public int speed;
  8.     public int jumpStrength;
  9.     public int controlledChar;
  10.     public bool useForce;
  11.     public bool lockRotation;
  12.     public bool allowMoving;
  13.     public bool isControlled;
  14.     public float topSpeed;
  15.     public LayerMask groundLayer;
  16.     public LayerMask playerLayer;
  17.     bool moveRight = false;
  18.     bool moveLeft = false;
  19.     bool jump = false;
  20.     bool jumpAllowed = false;
  21.     bool canJump = false;
  22.     bool playLanding = false;
  23.     public float distance;
  24.     float leftOffset;
  25.     float verticalOffset;
  26.     float verticalDistance;
  27.     string playerTag;
  28.  
  29.     RaycastHit2D hit;
  30.     float dist;
  31.     Vector2 dir;
  32.  
  33.     string leftRay;
  34.     string rightRay;
  35.  
  36.     Rigidbody2D rb;
  37.     Animator anim;
  38.     public CameraCtrl cameraCtrl;
  39.  
  40.     void Start()
  41.     {
  42.         dist = 10;
  43.         dir = new Vector2(0, -1);
  44.  
  45.         anim = GetComponent<Animator>();
  46.         rb = GetComponent<Rigidbody2D>();
  47.         cameraCtrl = GameObject.Find("mainCamera").GetComponent<CameraCtrl>();
  48.  
  49.         if (rb.GetComponent<Transform>() == GameObject.Find("redPlayer").GetComponent<Transform>())
  50.         {
  51.             controlledChar = 1;
  52.             //distance = 0.5f;
  53.         }
  54.         else if (rb.GetComponent<Transform>() == GameObject.Find("greenPlayer").GetComponent<Transform>())
  55.         {
  56.             controlledChar = 2;
  57.             //distance = 3f;
  58.         }
  59.  
  60.         //LOCK ROTATION
  61.         if (lockRotation)
  62.             rb.constraints = RigidbodyConstraints2D.FreezeRotation;
  63.     }
  64.  
  65.     void Update()
  66.     {
  67.         //DEBUG
  68.         //Debug.Log("Speed = " + rb.velocity.x);
  69.         //CHECK FOR INPUT RIGHT
  70.         if (isControlled)
  71.         {
  72.             if (Input.GetAxisRaw("Horizontal") > 0)
  73.             {
  74.                 if (allowMoving)
  75.                 {
  76.                     moveRight = true;
  77.                 }
  78.             }
  79.             else moveRight = false;
  80.  
  81.             //CHECK FOR INPUT LEFT
  82.             if (Input.GetAxisRaw("Horizontal") < 0)
  83.             {
  84.                 if (allowMoving)
  85.                 {
  86.                     moveLeft = true;
  87.                 }
  88.             }
  89.             else moveLeft = false;
  90.  
  91.             //CHECK FOR INPUT UP
  92.             if (Input.GetAxisRaw("Vertical") > 0)
  93.             {
  94.                 if (allowMoving)
  95.                 {
  96.                     jumpAllowed = true;
  97.                 }
  98.             }
  99.             else jumpAllowed = false;
  100.  
  101.             //NEW JUMP USING RAYCAST
  102.             IsGrounded();
  103.             //isOnPlayer();
  104.         }
  105.         if (Input.GetKeyDown("space"))
  106.             SwitchControlled();
  107.     }
  108.  
  109.     bool IsGrounded()
  110.     {
  111.         if (controlledChar == 1)
  112.         {
  113.             distance = 0.495f;
  114.             verticalDistance = 0.47f;
  115.             playerTag = "redPlayer";
  116.             leftRay = "leftRayPositionRed";
  117.             rightRay = "rightRayPositionRed";
  118.         }
  119.         else if (controlledChar == 2)
  120.         {
  121.             distance = 1.45f;
  122.             verticalDistance = 0.23f;
  123.             playerTag = "greenPlayer";
  124.             leftRay = "leftRayPositionGreen";
  125.             rightRay = "rightRayPositionGreen";
  126.         }
  127.         if (isControlled)
  128.         {
  129.             Vector2 leftRayPosition = (GameObject.Find(leftRay).transform.position);
  130.             Vector2 rightRayPosition = (GameObject.Find(rightRay).transform.position);
  131.             /*Vector2 horizontalOffset = new Vector2(0.48f, 0);
  132.             Vector2 position = transform.position;
  133.             Vector2 distance = new Vector2(0, -0.5f);
  134.             Debug.DrawRay(position - horizontalOffset, distance, Color.green);
  135.             */
  136.             Vector2 position1 = transform.position;
  137.             Vector2 position2 = new Vector2(0, distance);
  138.             Vector2 position = position1 - position2;
  139.             Vector2 distance1 = new Vector2(0, -0.025f);
  140.             Vector2 offsetR = new Vector2(verticalDistance, 0);
  141.  
  142.             Debug.DrawRay(rightRayPosition, distance1, Color.green);
  143.             Debug.DrawRay(leftRayPosition, distance1, Color.green);
  144.             Debug.DrawRay(position, distance1, Color.green);
  145.  
  146.             RaycastHit2D hit = Physics2D.Raycast(position, Vector2.down, 0.025f, groundLayer);
  147.             RaycastHit2D hitR = Physics2D.Raycast(rightRayPosition, Vector2.down, 0.025f, groundLayer);
  148.             //RaycastHit2D hitL = Physics2D.Raycast(position-offsetR, Vector2.down, 0.025f, groundLayer);
  149.             RaycastHit2D hitL = Physics2D.Raycast(leftRayPosition, Vector2.down, 0.025f, groundLayer);
  150.  
  151.             if (hit.collider != null)
  152.             {
  153.                 return true;
  154.             }
  155.             if (hitR.collider != null)
  156.             {
  157.                 return true;
  158.             }
  159.             if (hitL.collider != null)
  160.             {
  161.                 return true;
  162.             }
  163.         }
  164.         return false;
  165.     }
  166.  
  167.     /*bool isOnPlayer()
  168.     {
  169.         if (controlledChar == 1)
  170.         {
  171.             distance = 0.025f;
  172.             leftOffset = 0.48f;
  173.             verticalOffset = 0.5f;
  174.         }
  175.         else if (controlledChar == 2)
  176.         {
  177.             distance = 0.025f;
  178.             leftOffset = -0.241f;
  179.             verticalOffset = 1.45f;
  180.         }
  181.         if (isControlled)
  182.         {
  183.             /*Vector2 horizontalOffset = new Vector2(0.48f, 0);
  184.             Vector2 position = transform.position;
  185.             Vector2 distance = new Vector2(0, -0.5f);
  186.             Debug.DrawRay(position - horizontalOffset, distance, Color.green);
  187.             */
  188.             //Vector2 position = transform.position;
  189.  
  190.             //RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, playerLayer);
  191.             /*if (hit.collider != null)
  192.             {
  193.                 return true;
  194.             }
  195.         }
  196.         return false;
  197.     }*/
  198.  
  199.     private bool isGoingDown()
  200.     {
  201.         if (rb.velocity.y < -2) {
  202.             return true;
  203.         }
  204.         return false;
  205.     }
  206.  
  207.     private void FixedUpdate()
  208.     {
  209.         playLanding = isGoingDown();
  210.         //ADD ENABLEJUMP FORCE
  211.         if (isControlled)
  212.         {
  213.             if (canJump)
  214.             {
  215.                 if (jumpAllowed)
  216.                 {
  217.                     if (rb.velocity.y == 0)
  218.                     {
  219.                         rb.velocity = new Vector2(rb.velocity.x, jumpStrength);
  220.                         anim.SetInteger("State", 11);
  221.                     }
  222.                 }
  223.             }
  224.         }
  225.  
  226.         //ADD FORCE RIGHT
  227.         if (moveRight)
  228.         {
  229.             if (rb.velocity.x < topSpeed)
  230.             {
  231.                 {
  232.                     if (useForce)
  233.                     {
  234.                         rb.AddForce(transform.right * speed * Time.fixedDeltaTime * 100f, ForceMode2D.Force);
  235.                     }
  236.                     else
  237.                     {
  238.                         rb.velocity = new Vector2(speed * Time.fixedDeltaTime * 100f, rb.velocity.y);
  239.                     }
  240.                 }
  241.             }
  242.         }
  243.  
  244.         //ADD FORCE LEFT
  245.         if (moveLeft)
  246.         {
  247.             if (rb.velocity.x > -topSpeed)
  248.             {
  249.                 if (useForce)
  250.                 {
  251.                     rb.AddForce(-transform.right * speed * Time.fixedDeltaTime * 100f, ForceMode2D.Force);
  252.                 }
  253.                 else
  254.                 {
  255.                     rb.velocity = new Vector2(-speed * Time.fixedDeltaTime * 100f, rb.velocity.y);
  256.                 }
  257.             }
  258.         }
  259.  
  260.         //ADD JUMP VELOCITY
  261.         if (!IsGrounded())
  262.         {
  263.             return;
  264.         }
  265.         else
  266.         {
  267.  
  268.             if (jumpAllowed && IsGrounded())
  269.             {
  270.                 rb.velocity = new Vector2(rb.velocity.x, jumpStrength);
  271.                 anim.SetInteger("State", 11);
  272.             }
  273.         }
  274.     }
  275.  
  276.     private void OnCollisionEnter2D(Collision2D collision)
  277.     {
  278.         if (collision.gameObject.CompareTag("Ground") && playLanding)
  279.         {
  280.             anim.SetInteger("State", 21);
  281.         }
  282.  
  283.         if (collision.gameObject.CompareTag("redPlayer") || collision.gameObject.CompareTag("greenPlayer") && playLanding)
  284.         {
  285.             anim.SetInteger("State", 21);          
  286.         }
  287.     }
  288.    
  289.  
  290.  
  291.     /*void Jump()
  292.     {
  293.         Debug.Log("VOID JUMP");
  294.         jumpAllowed = true;
  295.         anim.SetInteger("State", 21);
  296.     }*/
  297.  
  298.     void SwitchControlled()
  299.     {
  300.         if (isControlled)
  301.         {
  302.             isControlled = false;
  303.             cameraCtrl.ChangeTarget();
  304.         }
  305.         else
  306.         {
  307.             isControlled = true;
  308.         }
  309.     }
  310.  
  311.     void OnTriggerStay2D(Collider2D other)
  312.     {
  313.         canJump = true;
  314.     }
  315.  
  316.     void OnTriggerExit2D(Collider2D other)
  317.     {
  318.         canJump = false;
  319.     }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement