Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class RaycastMovement : MonoBehaviour
- {
- public int speed;
- public int jumpStrength;
- public int controlledChar;
- public bool useForce;
- public bool lockRotation;
- public bool allowMoving;
- public bool isControlled;
- public float topSpeed;
- public LayerMask groundLayer;
- public LayerMask playerLayer;
- bool moveRight = false;
- bool moveLeft = false;
- bool jump = false;
- bool jumpAllowed = false;
- bool canJump = false;
- bool playLanding = false;
- public float distance;
- float leftOffset;
- float verticalOffset;
- float verticalDistance;
- string playerTag;
- RaycastHit2D hit;
- float dist;
- Vector2 dir;
- string leftRay;
- string rightRay;
- Rigidbody2D rb;
- Animator anim;
- public CameraCtrl cameraCtrl;
- void Start()
- {
- dist = 10;
- dir = new Vector2(0, -1);
- anim = GetComponent<Animator>();
- rb = GetComponent<Rigidbody2D>();
- cameraCtrl = GameObject.Find("mainCamera").GetComponent<CameraCtrl>();
- if (rb.GetComponent<Transform>() == GameObject.Find("redPlayer").GetComponent<Transform>())
- {
- controlledChar = 1;
- //distance = 0.5f;
- }
- else if (rb.GetComponent<Transform>() == GameObject.Find("greenPlayer").GetComponent<Transform>())
- {
- controlledChar = 2;
- //distance = 3f;
- }
- //LOCK ROTATION
- if (lockRotation)
- rb.constraints = RigidbodyConstraints2D.FreezeRotation;
- }
- void Update()
- {
- //DEBUG
- //Debug.Log("Speed = " + rb.velocity.x);
- //CHECK FOR INPUT RIGHT
- if (isControlled)
- {
- if (Input.GetAxisRaw("Horizontal") > 0)
- {
- if (allowMoving)
- {
- moveRight = true;
- }
- }
- else moveRight = false;
- //CHECK FOR INPUT LEFT
- if (Input.GetAxisRaw("Horizontal") < 0)
- {
- if (allowMoving)
- {
- moveLeft = true;
- }
- }
- else moveLeft = false;
- //CHECK FOR INPUT UP
- if (Input.GetAxisRaw("Vertical") > 0)
- {
- if (allowMoving)
- {
- jumpAllowed = true;
- }
- }
- else jumpAllowed = false;
- //NEW JUMP USING RAYCAST
- IsGrounded();
- //isOnPlayer();
- }
- if (Input.GetKeyDown("space"))
- SwitchControlled();
- }
- bool IsGrounded()
- {
- if (controlledChar == 1)
- {
- distance = 0.495f;
- verticalDistance = 0.47f;
- playerTag = "redPlayer";
- leftRay = "leftRayPositionRed";
- rightRay = "rightRayPositionRed";
- }
- else if (controlledChar == 2)
- {
- distance = 1.45f;
- verticalDistance = 0.23f;
- playerTag = "greenPlayer";
- leftRay = "leftRayPositionGreen";
- rightRay = "rightRayPositionGreen";
- }
- if (isControlled)
- {
- Vector2 leftRayPosition = (GameObject.Find(leftRay).transform.position);
- Vector2 rightRayPosition = (GameObject.Find(rightRay).transform.position);
- /*Vector2 horizontalOffset = new Vector2(0.48f, 0);
- Vector2 position = transform.position;
- Vector2 distance = new Vector2(0, -0.5f);
- Debug.DrawRay(position - horizontalOffset, distance, Color.green);
- */
- Vector2 position1 = transform.position;
- Vector2 position2 = new Vector2(0, distance);
- Vector2 position = position1 - position2;
- Vector2 distance1 = new Vector2(0, -0.025f);
- Vector2 offsetR = new Vector2(verticalDistance, 0);
- Debug.DrawRay(rightRayPosition, distance1, Color.green);
- Debug.DrawRay(leftRayPosition, distance1, Color.green);
- Debug.DrawRay(position, distance1, Color.green);
- RaycastHit2D hit = Physics2D.Raycast(position, Vector2.down, 0.025f, groundLayer);
- RaycastHit2D hitR = Physics2D.Raycast(rightRayPosition, Vector2.down, 0.025f, groundLayer);
- //RaycastHit2D hitL = Physics2D.Raycast(position-offsetR, Vector2.down, 0.025f, groundLayer);
- RaycastHit2D hitL = Physics2D.Raycast(leftRayPosition, Vector2.down, 0.025f, groundLayer);
- if (hit.collider != null)
- {
- return true;
- }
- if (hitR.collider != null)
- {
- return true;
- }
- if (hitL.collider != null)
- {
- return true;
- }
- }
- return false;
- }
- /*bool isOnPlayer()
- {
- if (controlledChar == 1)
- {
- distance = 0.025f;
- leftOffset = 0.48f;
- verticalOffset = 0.5f;
- }
- else if (controlledChar == 2)
- {
- distance = 0.025f;
- leftOffset = -0.241f;
- verticalOffset = 1.45f;
- }
- if (isControlled)
- {
- /*Vector2 horizontalOffset = new Vector2(0.48f, 0);
- Vector2 position = transform.position;
- Vector2 distance = new Vector2(0, -0.5f);
- Debug.DrawRay(position - horizontalOffset, distance, Color.green);
- */
- //Vector2 position = transform.position;
- //RaycastHit2D hit = Physics2D.Raycast(position, direction, distance, playerLayer);
- /*if (hit.collider != null)
- {
- return true;
- }
- }
- return false;
- }*/
- private bool isGoingDown()
- {
- if (rb.velocity.y < -2) {
- return true;
- }
- return false;
- }
- private void FixedUpdate()
- {
- playLanding = isGoingDown();
- //ADD ENABLEJUMP FORCE
- if (isControlled)
- {
- if (canJump)
- {
- if (jumpAllowed)
- {
- if (rb.velocity.y == 0)
- {
- rb.velocity = new Vector2(rb.velocity.x, jumpStrength);
- anim.SetInteger("State", 11);
- }
- }
- }
- }
- //ADD FORCE RIGHT
- if (moveRight)
- {
- if (rb.velocity.x < topSpeed)
- {
- {
- if (useForce)
- {
- rb.AddForce(transform.right * speed * Time.fixedDeltaTime * 100f, ForceMode2D.Force);
- }
- else
- {
- rb.velocity = new Vector2(speed * Time.fixedDeltaTime * 100f, rb.velocity.y);
- }
- }
- }
- }
- //ADD FORCE LEFT
- if (moveLeft)
- {
- if (rb.velocity.x > -topSpeed)
- {
- if (useForce)
- {
- rb.AddForce(-transform.right * speed * Time.fixedDeltaTime * 100f, ForceMode2D.Force);
- }
- else
- {
- rb.velocity = new Vector2(-speed * Time.fixedDeltaTime * 100f, rb.velocity.y);
- }
- }
- }
- //ADD JUMP VELOCITY
- if (!IsGrounded())
- {
- return;
- }
- else
- {
- if (jumpAllowed && IsGrounded())
- {
- rb.velocity = new Vector2(rb.velocity.x, jumpStrength);
- anim.SetInteger("State", 11);
- }
- }
- }
- private void OnCollisionEnter2D(Collision2D collision)
- {
- if (collision.gameObject.CompareTag("Ground") && playLanding)
- {
- anim.SetInteger("State", 21);
- }
- if (collision.gameObject.CompareTag("redPlayer") || collision.gameObject.CompareTag("greenPlayer") && playLanding)
- {
- anim.SetInteger("State", 21);
- }
- }
- /*void Jump()
- {
- Debug.Log("VOID JUMP");
- jumpAllowed = true;
- anim.SetInteger("State", 21);
- }*/
- void SwitchControlled()
- {
- if (isControlled)
- {
- isControlled = false;
- cameraCtrl.ChangeTarget();
- }
- else
- {
- isControlled = true;
- }
- }
- void OnTriggerStay2D(Collider2D other)
- {
- canJump = true;
- }
- void OnTriggerExit2D(Collider2D other)
- {
- canJump = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement