Problem with adding a obstacle to "Roll a Ball" tutorial
Hi, I recently finished the "Roll a Ball" tutorial from unity's tutorial videos and so thought, maybe I could develop the game some more by adding a spinning wall (to jump over) that kills you when you get in contact whith it. When writing the code for picking up the objects I made a mistake which made the character disappear instead of the objects:
void OnCollisionEnter (Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
gameObject.SetActive(false);
}
}
}
so i figured maybe I can use this failure for creating my obstacle. I added the code to the playerScript and added the necessary tags to the objects in the scene. But for some reason it is not working the way I want it to:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public float jumpPower;
private Rigidbody rb;
private int count;
public Text countText;
public Text winText;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText();
winText.text = "";
}
// Update is called once per frame
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
if (Input.GetKey(KeyCode.Space) && rb.transform.position.y <= 0.6250001f)
{
Vector3 jumping = new Vector3(0, 10, 0);
rb.AddForce(jumping * jumpPower * Time.deltaTime);
}
rb.AddForce(movement * speed * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count +100;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString() + "p";
if (count >=1100)
{
winText.text = "Winner!";
}
}
void OnCollisionDie(Collider other)
{
if (other.gameObject.CompareTag("DeathCube"))
{
gameObject.SetActive(false);
}
}
}
If I set the "Player" to "Is Trigger" it will fall through everything, but if I in the other hand set my object named "DeathCube to "IsTriggered the player just falls through the it and nothing more happens, no disapearing.
I should probably mention that i made a working project where the player rolls in to a wall and disappeares.
How do you fix this problem or add this kind of death-obstacle to the game?
Thanks in advance.
Answer by Chik3r · Feb 26, 2018 at 08:17 PM
Hi @Supermacka First of all: What is 'OnCollisionDie' or when are you calling it? Because that function isn't called when you collide nor when you touch a trigger. What I think you want is to use 'OnCollisionEnter(Collider other)' and that way when you collide with the DeathCube you instantly deactivate
Answer by Supermacka · Feb 26, 2018 at 08:36 PM
@chiker Thank you so much! I didn't know "OnCollisionEnter" was a funtion, i just thought you name your "void" whatever you want, in my case "OnCollisionDie".
Your answer
![](https://koobas.hobune.stream/wayback/20220612153505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
roll the ball collecting cubes not working help 4 Answers
Does anyone understand why I keep getting "ArgumentOutOfRangeException" from my code? 0 Answers
Play audio once not working 0 Answers
my car rolls over to the right always when i run the game and wheels fly away from the car, 2 Answers
Why does the sprite editor not slice the sprite sheet? 1 Answer