I'm trying to make my player jump only when they are touching the ground and to do so I made an OnCollisionEnter(Collision collision) function, then I made another function called jump which makes the player jump when the space button is pressed the on the CollisionEnter function I call this function however this does not call the jump function, what am I doing wrong? when the player collides with the ground though it does print It's jumping as I have made a Debug.log check.
OnCollisionEnter function is called only once, when your player collide the ground.
If you want to do that you have to use a "isGrounded" boolean variable which you set to true when is grounded (in OnCollisionEnter) and set to false when you press jump.
The jump control must be in the Update function.
Related
Please tell me, there is a function OnTriggerEnter:
void OnTriggerEnter(Collider other) {}
This function matches if an element is in the trigger of another element.
Now this script is on the character.
How can I perform this function for my character by hanging a script for example on terrain?
There is a lot of left out context in this question, and I hate to make assumptions, but I can't yet comment so I will try my best to answer. Assuming that you want to detect if the character is colliding with the terrain, the easiest way to do this, would be to instead use void OnCollisionEnter (). If your player character has a Rigidbody and a collider component, and your terrain has a collider component, you can tag your terrain "Terrain" and detect for collisions with it using the following function in your code:
//Detects a collision and grabs the collider
void OnCollisionEnter (Collider other)
{
//Detects if the object collided with has the "Terrain" tag
if (other.gameObject.tag == "Terrain")
{
//Your code here
}
}
Looks like you want to know how to use OnTriggerEnter
Here are the steps.
Attach Colliders to the objects that are expected to collider. At
least one of the Collider should be marked as trigger.
Attach a non-Kinematic Rigidbody to one of the objects.
attach the script with OnTriggerEnter function to one of the objects. The
function will be called if the collision is detected.
You can read this tutorial on Unity Collision for detailed explanation
My player currently dies when it hits an enemy tagged with "Enemy" and restarts the level using the following script
if (other.gameObject.CompareTag("Enemy"))
{
Destroy(gameObject);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
I wanted my player to kill the enemy when the bottom of my player hits the enemy, so i created a child gameobject called "Feet" and labelled it "Feet" as well, I also added an edge collider to it. I added the following script to it.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
Now the "Enemy" dies when the "Feet" collides with it, but my player also dies, how do i make an exception to the first script so my player doesnt die when "Feet" collides with the object first instead of the "Player"
You should restrict the collider of the player. It seems like your "Feet" is included in the "Body" of the player. So "Feet" and "Body" touch the Enemy at the same time.
So make the collider of the player smaller at the bottom.
Maybe I have a misunderstanding as to how OnTriggerEnter is supposed to work but here is my situation and how it differs from what I would expect.
I've created a very simple test project with a single scene. I have three objects in my scene - a player, an enemy, and an attack. The enemy and player both have rigid bodies and box colliders, neither of which is a trigger. The attack object has a box collider that IS a trigger. For scripts, the player has a script with nothing in it other than an OnTriggerEnter function that logs info to the console. The enemy has a simple script that just enables and disables the attack object in a cyclic pattern. When the attack is enabled it collides with the player.
Github with cloneable project/code: https://github.com/valevalorin/TriggerTest
What I would expect to happen:
The attack object is enabled and collides with the player. No console output is logged. The player object does not have a trigger collider on it so it's OnTriggerEnter should never be called.
What actually happens:
When the attack object collides with the player, the player's OnTriggerEnter function gets called and output is logged to the console.
As far as I can tell, OnTriggerEnter should only be called on objects that actually have a trigger associated to them. Is this not how OnTriggerEnter works?
From the documentation:
Description
OnTriggerEnter is called when the Collider other enters the trigger.
This message is sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger.
Emphasis mine.
i am totally new to c# scripting and unity... i was trying something on unity and created this code and it's woring fine but some times it's just overlapping "wall" tagged object or even getting out of that circle i am using Edge collider 2D on it and polygon collider 2D on my shooter object and this script is attached to shooter object. check the screen shot for the bug.
void OnCollisionEnter2D (Collision2D collider){
if (collider.gameObject.tag == "wall") {
StartCoroutine (shooterscale());
collider.gameObject.GetComponent<bgAnimater> ().animateBg ();
if (turn) {
turn = false;
}
else {
turn = true;
}
}
}
I assume you are using Rigidbody2D on the shooter.
Make sure that Collision detection mode is set to Continous to avoid objects passing through each other.
From Docs:
When the Collision Detection is set to Continuous, GameObjects with
Rigidbody 2Ds and Collider 2Ds do not pass through each other during
an update. Instead, Unity calculates the first impact point of any of
the Collider 2Ds, and moves the GameObject there. Note that this takes
more CPU time than Discrete.
and
When you set the Collision Detection to Discrete, GameObjects with
Rigidbody 2Ds and Collider 2Ds can overlap or pass through each other
during a physics update, if they are moving fast enough. Collision
contacts are only generated at the new position.
Hope this helps
I'm stuck on this sphere has a CircleCollider2D and rectangle has a BoxCollider2D i increase the offset of BoxCollider. But why sphere not collide from it's border. Sphere go inside and then collide just like this.
I want that it's collide when sphere touch it's border.I also check with default radius but the same situation occur.
Trigger
The scripting system can detect when collisions occur and initiate actions using the OnCollisionEnter function. However, you can also use the physics engine simply to detect when one collider enters the space of another without creating a collision. A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts.
http://docs.unity3d.com/Manual/CollidersOverview.html
You need to close isTrigger and use OnCollisionEnter(...).
For example this is my script of wood(which is below ). A smaller wood will be going down.
This is the beginning of my game
If I set isTrigger option true, onCollisionEnter2D function doesn't work. I need isTriggerEnter() method instead. it goes through my wood layer.
But if I set isTrigger false onCollisionEnter2D will be working. It will stay on my wood layer.