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

Coins and Enemies 3d

$
0
0
My player, when colliding with an enemy dies, which is correct but when it collides with a coin it still dies T_T Here is the script for the PlayerMotor: using UnityEngine; using System.Collections; public class PlayerMotor : MonoBehaviour { private CharacterController controller; private Vector3 moveVector; private float speed = 5.0f; private float verticalVelocity = 0.0f; private float gravity = 20.0f; private float animationDuration = 3.0f; private bool isDead = false; private float startTime; private Transform cointrans; // Use this for initialization void Start() { controller = GetComponent(); startTime = Time.time; } // Update is called once per frame void Update() { if (isDead) return; if (Time.time-startTime < animationDuration) { controller.Move(Vector3.forward * speed * Time.deltaTime); return; } moveVector = Vector3.zero; if (controller.isGrounded) { verticalVelocity = -0.5f; } else { verticalVelocity -= gravity * Time.deltaTime; } //X - left and right //moveVector.x = Input.GetAxisRaw("Horizontal") * speed; //Rect bounds = new Rect(0, 0, Screen.height / 2, Screen.width); //if (Input.GetMouseButtonDown(0) && bounds.Contains(Input.mousePosition)) if (Input.GetMouseButton(0)) { //are we holding click on the right side if (Input.mousePosition.x > Screen.width / 2) moveVector.x = speed; else moveVector.x = -speed; } //Y - up and down moveVector.y = verticalVelocity; //Z - forward and backward moveVector.z = speed; controller.Move(moveVector * Time.deltaTime); } public void SetSpeed(float modifier) { speed = 8.0f + modifier; } //It is being called everytime out capsule hits something private void OnControllerColliderHit(ControllerColliderHit hit) { //if (GameObject.FindWithTag("Enemy") == null) //cointrans = GameObject.FindGameObjectWithTag("Coin").transform; if (hit.point.z >= transform.position.z + controller.radius && GameObject.FindWithTag("Enemy") == null) Death(); else CoinScore(); } private void Death() { isDead = true; GetComponent().OnDeath(); // GetComponent().OnTriggerEnter(); } private void CoinScore() { isDead = false; GetComponent().CoinScores(); } } and this is for the coin using UnityEngine; using System.Collections; using UnityEngine.UI; public class PickUpScript : MonoBehaviour { public Text coinText; public float coin; public DeathMenu deathMenu; void Update() { transform.Rotate(Vector3.up, Time.deltaTime * rotateSpeed); } public void OnTriggerEnter(Collider col) { // UIManager.Instance.IncreaseScore(ScorePoints); PlayerPrefs.SetFloat("Banana Milk: ", coin); Destroy(this.gameObject); } public void CoinScores() { if (PlayerPrefs.GetFloat("Highscore") < coin) { //PlayerPrefs.SetFloat("Highscore", score); PlayerPrefs.SetFloat("Banana Milk: ", coin); } //PlayerPrefs.SetFloat("Banana Milk: ", coin); // Debug.Log(); deathMenu.ToggleEndMenu(coin); } public int ScorePoints = 100; public float rotateSpeed = 50f; }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



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