My Sprite is Moving against a platform.
I have a script and an unusual one (that's what one of the member said.) My isGrounded isn't using any rayCasting. I didn't have any problem with it but when I created a platform and jump against it:
- My character would fall down slowly.
- If I try to move against the platform, I would be able to move and it would appear as if the character was flying.
So question is, how can I stop all my movements, jumping everything, when I collide like this with a platform?
My Code is:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class pScript : MonoBehaviour { public Rigidbody2D rb; [SerializeField] public Transform[] Points; [SerializeField] public float radius; [SerializeField] public LayerMask layer; public bool jump; public bool isgrounded = false; [SerializeField] public float MoveSpeed; [SerializeField ] public float jumpHigh; public bool facingRight; public Animator animator; public Object CallfPanel; public Object CallfText; public float Move = 8f; public Vector3 Dir; public Vector3 tart; // Use this for initialization void Start () { rb = GetComponent<Rigidbody2D> (); animator = GetComponent<Animator> (); CallfPanel = GameObject.Find ("Panel"); CallfText = GameObject.Find ("Text"); } // Update is called once per frame void Update () { RaycastHit hit; Debug.DrawRay (transform.position, Vector3.right, Color.white, Move); tart = new Vector3 (200, 300, transform.position.z); Dir = Vector3.right; if (jump == true) { if (Physics.Raycast (tart, Dir, out hit, Move)) { jump = false; } } float horizontal = Input.GetAxis ("Horizontal"); animator.SetFloat ("speed", Mathf.Abs (horizontal)); isgrounded = isGrounded (); rb.velocity = new Vector2 (horizontal * MoveSpeed, rb.velocity.y); if (Input.GetKeyDown (KeyCode.Space)) { jump = true; } if (isgrounded && jump == true) { isgrounded = false; rb.velocity = new Vector2 (0, jumpHigh); } if (isgrounded == false) { jump = false; } changeDirections (); } private bool isGrounded(){ if (rb.velocity.y <= 0) { foreach (Transform points in Points) { Collider2D[] collider = Physics2D.OverlapCircleAll (points.position, radius, layer); for (int i = 0; i < collider.Length; i++) { if (collider [i].gameObject != gameObject) { return true; } } } }return false; } public void changeDirections(){ float horizontal = Input.GetAxis("Horizontal"); Vector3 theScale = transform.localScale; if (theScale.x == 1) { facingRight = true; } else { facingRight = false; } if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) { theScale.x *= -1; transform.localScale = theScale; } } public void showTextDuration(){ float lifetime = 4.0f; Destroy (CallfPanel, lifetime); } } 