I'm trying to get my sprite to pick up a cube when it encounters one, if it isn't already carrying one. If it is, drop the cube it is carrying.
This is what I have right now.
void OnTriggerEnter(Collider other)
{
if (other.Tag == "cube")
{
other.Transform.position = this.Transform.position;
}
}
I tried telling the cube to become a child of the sprite. Wasn't working. All this does is put the cube to the sprites position when the trigger is entered, but the cube stays in that position while the sprite wonders off.
With this code,you can change the cubes position to the players position just one time.If you want cube to move with your character than you should make child of your character.
Try using this code;
void OnTriggerEnter(Collider other)
{
if (other.tag == "cube")
{
other.transform.parent = gameObject.transform;
}
}
PS:I don't have access to Unity now.Some coding mistakes can occur.
Related
I'm trying to make a game with "Death Blocks" that move the player to the respawn point after triggering the OnTrigger method. Here is the code:
public class DeathBlock : MonoBehaviour
{
public GameObject respawnPoint;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.name == "Player")
{
collision.gameObject.transform.position = respawnPoint.transform.position;
}
}
}
Edit: I found out the problem. The camera was moving on the Z axis as well so clamp the cameras Z axis position to whatever you had it to on scene startup (-10 for me), and the character remains on screen.
Do either the respawn or the player have a parent game object? When you change the transform.position you change the local transform, if there is a parent object it will multiply the rotation/scale of the parent. You can also while the game is running click on 'scene' in the editor, then click on the player in the inspect, hit 'f' and find out where it is or what it doing, and whether its set active.
I'm creating a 2d game and I want to make it so that tiles are destructible whenever a projectile collides with a tile. The problem is I don't know how to get the tile that the projectile is colliding with, I would like to make it so that it area of damage.
However for now I want to make it so that once the projectile reaches the ground it destroys the tile it's sitting on I've made a collider2d to help me with that but I still have no idea how to get the tile that's the projectile is sitting on.
IEnumerator OnCollisionEnter2D(Collision2D collision)
{
//Projectile has reached the ground and is in collision with some tile
Vector2 hit = gameObject.transform.position;
Debug.Log("y" + hit.y);
hit.y =- 3; // i have no idea what y i should put in order to get the tile
if (x.gameObject.tag != "Player")
{
Collider2D[] collidedwith = Physics2D.OverlapCircleAll(this.gameObject.transform.position, radius);
//what should the hit vector be in order to destroy the tile.
tilemap.SetTile(tilemap.WorldToCell(hit), null);
}
}
A screenshot for better explanation (I would like to get rid of the blue tile):
If possible I would like to make it into an aoe projectile but for now it's not necessary
Assuming you have colliders in place, that is:
Collider in the rocket with is trigger selected (to allow OnTriggerEnter2D be triggered)
Collider on the tiles to actually trigger the OnTriggerEnter2D of the rocket during the collision
Now, to detect collisions between rocket and tiles, do the following:
Give the tile gameObject (prefab) the tag name tile
Replace your code in the rocket with:
Script
// It will be triggered the the rocket crashes against the tile
void OnTriggerEnter2D(Collider2D col)
{
if(collision.gameObject.tag == "tile")
{
// Destroy the tile the rockets collided with
Destroy(collision.collider.gameObject);
// Destroy the rocket itself
Destroy(gameObject);
}
}
ok i came up with a solution but this solution doesn't really have the feature i really wanted which is, i want the projectile to do aoe damage
Tilemap tilemap = GetComponent<Tilemap>();
Vector3 hitPosition = Vector3.zero;
foreach (ContactPoint2D hit in collision.contacts)
{
Debug.Log(hit.point);
hitPosition.x = hit.point.x - 0.1f;
hitPosition.y = hit.point.y - 0.1f ;
tilemap.SetTile(tilemap.WorldToCell(hitPosition), null);
}
I've joined a game jam, and I'm making a 2D platformer in Unity for it. For this I've created a Jump Pad. Whenever the player character stands on it they'll jump. The problem is that the jump doesn't look like a jump, it looks more like a teleportation upwards. Any fixes? Code below!
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Spike") {
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
} else if (other.tag == "Jump Pad") {
rb.AddForce(new Vector2(0f, jumpSpeed));
}
}
Edit
https://imgur.com/a/7MM4QJF
this code worked for me
private void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "player")
{
//you can change 20 to whatever number you want
other.GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 20), ForceMode2D.Impulse);
}
}
Note : be sure to tag the player "player"
Note 2 : be sure is trigger is off
You can try to add upward force like below
rb.AddForce(transform.up*jumpSpeed);
Note: Set a 'realistic' jumpSpeed.
DUDE. lol so i just tried this and what do u know it was giving me trouble, but what i did was take that trigger script off the player and have the jumppad detect the trigger.
if (other.tag == "Player")
{
other.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, jumpSpeed));
}
tag ur player player and put this on the jump pad and set to istrigger
In the game you controll a ball (Sphere) and two types of boxes falling down: deathCube and goldCube. When the Sphere hit the DeathCube, then the Sphere is destroyt, but it not get destroyed and I don't know why. The cubes are prefabs and they have a tag(DeathCube, GoldCube).
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DeathCube")
{
Destroy (gameObject);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "GoldCube")
{
gold++;
}
}
If the Sphere hit the goldCube you get points, but this doesn't work too.
Try merging the two OnTriggerEnter's into one.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "DeathCube")
{
Destroy (gameObject);
}
if (other.gameObject.tag == "GoldCube")
{
gold++;
}
}
I believe that the second one is overriding the first, never allowing the Destroy() to be called. I would've assumed the compiler would throw an error with this, but you don't seem to have indicated that.
If you don't have a rigidbody attached to at least one of the objects in the collision (ball or cube), then the trigger event won't be initiated.
From the documentation:
Notes: Trigger events are only sent if one of the colliders also has a rigidbody attached
Source: https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html
I'm trying to make a BTD game. Since different balloons (enemies) have different speeds, they collide with each other when traveling on the path. I'm currently using this code:
void OnCollisionEnter (Collision coll)
{
if (coll.gameObject.tag == "Enemy")
{
Physics.IgnoreCollision(coll.collider, gameObject.GetComponent<SphereCollider>());
}
}
However, it doesn't appear to be working at all. The enemies still collide with each other. On the otherhand, the collision with the enemies and bullets from towers is working.
void OnTriggerEnter (Collider col)
{
if (col.tag == "Bullet")
{
CurrentHP -= col.GetComponent<TackShooterBullet>().Damage;
}
I've tried layer-collision (enemies to same layer & unchecking of the same layer collision in the layer collision matrix, but that doesn't work either. The enemy contains a sphere mesh filter, sphere collider, mesh renderer, rigidbody, material, and 2 scripts. Is there a better way to avoid collisions between the enemies. I'm asking this question since I've seen duplicates, but their solutions aren't working at all. I can provide more of my code if needed.
Edit for Clarity: Again, what I'm trying to accomplish is have the enemies be able to go through each other.
Edit (Fixed Problem): I found out to avoid Enemy Collisions, I could also remove rigidbody. However, removing the ridigbody would mess up the bullet --> enemy trigger in the enemy class. Therefore, I just wrote the collision between bullet & enemy in the bullet class instead.
using UnityEngine;
public class TackShooterBullet : MonoBehaviour {
private GameObject target;
public float Damage;
// Use this for initialization
void Start () {
target = transform.parent.GetComponent<TackShooterRange>().Target; // Target = Enemy[0] (First Enemy To Enter Range - Enemy is Removed from JList when exiting Range)
}
// Update is called once per frame
void Update()
{
Damage = gameObject.transform.parent.transform.parent.GetComponent<TackShooterLimitingRange1>().level * 20; // Upgrade Level * 20 = Damage Done
if (target == null) // If Enemy Exits Range
{
Destroy(gameObject); // Destroy Bullet
}
if (target != null) // Enemy Exists In Range
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, 20 * Time.deltaTime); // Bullet Follows Enemy
Destroy(gameObject); // Destroy Bullet Upon Contact With Enemy
target.GetComponent<HealthOfEnemy>().CurrentHP -= Damage; // Enemy Loses Health
}
}
This allowed me to remove the OnTriggerEnter & OnCollisionEnter methods as well as the RigidBody from the Enemy Class as stated before, so these properties no longer affect the collisions between Enemies.
Unity has a built in function for easier collision detection called layer-based collision detection:
https://docs.unity3d.com/Manual/LayerBasedCollision.html
The documentation is really good. Just comment if you need further clarification.