Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Enemy not getting hit (Collider and Raycast)

$
0
0
I tried getting my enemy to fall underneath the ground and when it reaches Y: -20 it destroys which is in a separate code. The enemy. is not moving when my Player jumps on top of it. Here is the code where I disabled the BoxCollider: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Player_Move : MonoBehaviour { public int playerSpeed = 10; public int playerJumpPower = 1250; private float moveX; public bool isGrounded; public float distanceToBottomOfPlayer; private void Start() { Data_Management.data_Management.LoadData(); } // Update is called once per frame void Update() { PlayerRaycast(); PlayerMove(); } private void PlayerMove() { //CONTROLS moveX = Input.GetAxis("Horizontal"); if (Input.GetButtonDown("Jump") && isGrounded == true) { Jump(); } //ANIMATIONS if (moveX != 0) { GetComponent().SetBool("IsRunning", true); } else { GetComponent().SetBool("IsRunning", false); } //PLAYER DIRECTION if (moveX < 0.0f) { GetComponent().flipX = true; } else if (moveX > 0.0f) { GetComponent().flipX = false; } //PHYSICS gameObject.GetComponent().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent().velocity.y); } void Jump() { //JUMPING CODE GetComponent().AddForce(Vector2.up * playerJumpPower); isGrounded = false; } private void OnCollisionEnter2D(Collision2D col) { if (col.gameObject.tag == "Ground") { isGrounded = true; } } public void PlayerRaycast() { { RaycastHit2D rayUp = Physics2D.Raycast (transform.position, Vector2.up); if (rayUp != null && rayUp.collider != null && rayUp.distance < 0.9f && rayUp.collider.tag == "Lucky_Block") Destroy(rayUp.collider.gameObject); } RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down); if (hit != null && hit.collider != null && hit.distance < 0.9f && hit.collider.tag == "Enemy") { GetComponent().AddForce(Vector2.up * 1000); hit.collider.gameObject.GetComponent().AddForce(Vector2.right * 200); hit.collider.gameObject.GetComponent().gravityScale = 8; hit.collider.gameObject.GetComponent().freezeRotation = false; hit.collider.gameObject.GetComponent().enabled = false; hit.collider.gameObject.GetComponent().enabled = false; } if (hit != null && hit.collider !=null && hit.distance < 0.9f && hit.collider.tag != "Enemy") { isGrounded = true; } } } It starts on public void PlayerRaycast()

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>