What is the best way to detect if I passed obstacles (succeed to not collide with them) in an endless runner game?
What I would do is to place an empty GameObject (no 3d/3d model attached to them) in the parts of the path which are clear (no obstacles). And to those empty GameObjects I would add a collider which is triggered.
Then I will add a script to those empty GameObjects which will have this piece of code:
void OnTriggerEnter(Collider other) {
//Do something, like for example increase a counter of points
// or show a success message...
}
So when the player goes through these empty GameObjects, it will trigger some actions that represents success.
Related
I'm a new to game developing, and I'm making my first game in Unity, which is a top-down, 2D survival type game. In order to detect when the player hits a tree or other world object, I added invisible triggers on each side of the player, which I set active whenever you click. Whenever either the player or the target is moving, this system works perfectly, however, when the target is not moving, like a tree, the collision is not detected. I figure that the OnTriggerEnter function only works when a moving object collides with the trigger, however, I have no idea how to do it otherwise. Is there another function I can use, or some way I can fix this?
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("hit");
if (other.gameObject.tag == "Tree")
{
Debug.Log("hit tree");
other.gameObject.GetComponent<TreeScript>().treeHealth--;
}
}
You can try the:
private void OnCollisionEnter2D(Collision2D col)
{
}
If 2 objects collide with each other without using the "Trigger" option, the OnCollisionEnter2D will be called.
A quick example where to use the 2 different methods are as follow:
Use the OnTriggerEnter2D() method in a racing game where you want to detect when a car goes through the finish line and you want to use a trigger because you don't actually care for the collision.
Use the OnCollisionEnter2D method to detect when 2 cars collide with each other and you don't want the 2 object to go through each other.
I've been trying to figure out how to detect collision to make a simple combat system, but the commands aren't returning what I want them too. What I'm using right now (in the unity engine):
using UnityEngine;
public class swordDetect : MonoBehaviour
{
void OnCollisionEnter(UnityEngine.Collision CollisionInfo)
{
if (CollisionInfo.gameObject.name == "swordCollide")
{
Debug.Log("it work again");
}
}
}
This isn't returning anything, and its attached to a rock that I've been using to test it.
The swordCollide object is attached to the players sword, but nothing happens when it collides with the rock.
Make sure that your Gameobject has a collider on it, which is not set to Trigger (because then it can't receive collision, but will only trigger). I would always rather tag my Gameobjects and check for the tag than for the name, because it is less error prone. If the colliding Gameobject is a Trigger, you need to check
private void OnTriggerEnter(Collider other){}
Best watch the Unity tutorials about collision https://unity3d.com/learn/tutorials/topics/physics/colliders?playlist=17120
and check the documentation https://docs.unity3d.com/ScriptReference/Collider.html
Both are easy to understand and go with some sample code
This question already has answers here:
How to find child of a GameObject or the script attached to child GameObject via script
(4 answers)
Closed 4 years ago.
I want to access the animator component of my player character. The character is spawned under the GameObject Character position, which it self is the child of Game Manager.
The character prefabs have various names, so I cannot find them through exact name. So its easier to just get the only child of Character position.
Game Manager
Character position
Player Prefab
Ive searched online and tried GetChild by index and GetComponentInChildren. None of them work. Below is the script I wrote for this:
private Animator archerAnimator;
private float startSpeed;
GameObject charPos;
GameObject archer_;
// Use this for initialization
void Start () {
charPos = GameObject.Find("Game manager/Character position");
Debug.Log(charPos);
archer_ = charPos.transform.GetChild(0).gameObject;
archerAnimator = charPos.GetComponentInChildren<Animator>();
Debug.Log(archerAnimator);
}
charPos is found, but for archer_ I get the error, Transform child out of bounds. The player archer is not there but is spawned at run time when the scene starts, is this the reason it cannot find it so quickly?
Some guidance would be appreciated.
Thank you
I think you're scanning for the player too early. You should reverse your discovery logic. Instead of scanning for the player and its Animator, you should put a script on the player itself that runs after it is created and reports itself to the game manager or whatever object needs access to it, something like this:
void Start() { GetComponentInParent<GameManager>().OnPlayerSpawned(this); }
I'll also mention that some script finding an object by name and accessing its components is a generally bad idea. Here's a design guideline to always keep in mind: You should traverse Unity's object hierarchy as infrequently as possible, and even if you do, you should only traverse objects that don't have other scripts attached. In this case, you should also put the logic to control the Animator inside your Player script. Then, you wouldn't need to get a reference to the Animator in the first place.
so i have my player standing next to a cube. the player has a script, which is empty aside from an int which is 43. the same applies to the cube except the int in the cube's script is 42. how do i get(or detect) the int in the cube's script and print it in the console using OnCollisionEnter(or OnTriggerEnter if it is better) like this: ("the cube has the number 42")?
Well you should definitely go through some tutorials before continuing since you don't seem to know even very basic stuff, but to at least point you in the right direction, you would do something like this (assuming C#, not UnityScript):
void OnCollisionEnter(Collision collision)
{
int numberOfCollidedObject = collision.gameObject.GetComponent<objectsScriptNameHere>().variableNameHere;
Debug.Log(numberOfCollidedObject);
}
How did I know how to do that? I looked at the documentation. I can see that when OnCollisionEnter is called it's passed a variable of type Collision. It's hyperlinked in the documentation, so I clicked on Collision and found that it contains a variable called gameObject that contains a reference to the game object of the collider we just hit. I happen to know that to get into another script, you called GetComponent<scriptName>(), and from there any public variables and functions can be accessed.
if you got two collider (player and object who collide the player) you can convex the collider and set isTrigger to true
Then call the function OnTriggerEnter()
void OnTriggerEnter(Collider other) {
Debug.Log(other.name);
}
I've started studying Unity and decided to do some practice. So, I thought that it would be awesome to develope "Flappy Bird" as an experiment. But I've faced with the problem. You know, when you fly between pipes, you earn a point. To do that I made a prefab with two pipes and an empty game object (trigger) between them to detect collision with it. But when I call OnCollisionEnter2D method, it detects collision with a prefab, not with the trigger or pipes in it. Could you help me? How do I detect collision with a child of a prefab?
like what Uri Popov answered, use OnTriggerEnter2D() there are several points you need to do :
1. Put the OnTriggerEnter2D() Script on your empty game object, and don't forget to attached collider2D on your empty object.
2. make sure isTrigger checked in collider2D option
3. also make sure your player has tag, you can make the tag your own, in this example i give "Player" tag to my player game object
4. the example script(i tired it and worked)
public class YourTriggerScript: MonoBehaviour {
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.GetComponent<Collider2D>().tag == "Player")
{
Debug.Log ("Collided");
// do something or you can + your point here
}
}
}
Hope it helped you
if your collider is marked as a Trigger you should use OnTriggerEnter2D().