- Home /
Question by
rainbowwstarrs · Sep 18, 2016 at 02:01 PM ·
collisionenemyscore
Coins and Enemies 3d
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<CharacterController>();
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<Score>().OnDeath();
// GetComponent<CoinBehavior>().OnTriggerEnter();
}
private void CoinScore()
{
isDead = false;
GetComponent<PickUpScript>().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;
}
Comment
Answer by Dream_in_code · Sep 18, 2016 at 02:45 PM
Are you using tags? If you are i think you have tagged the coin and enemy with the same name.
check the inspector of your gameobject
screenshot-1.jpg
(11.6 kB)
Your answer
![](https://koobas.hobune.stream/wayback/20220612092944im_/https://answers.unity.com/themes/thub/images/avi.jpg)