Destroy an enemy with spotlight - unity3d

function OnCollisionEnter(theCollision : Collision)
{
if(theCollision.gameObject.name=="Spotlight")
{
Destroy(gameObject);
Debug.Log("Dead");
dead = true;
}
}
This is my code here, I have a spotlight attached to my enemy which acts like a torch, what I want is for the enemies to be destroyed once they walk into the spotlight.
I tested the collider with the player and it works fine but for some reason, when I set it to the spotlight, nothing happens at all.
Can anyone help me out here?

You can ray cast from the origin of the light to your surface. Once you have found the intersection point of this ray with your surface you can control the XZ position of a capsule or sphere collider of size(radius, N, radius) - attach your detection script to that collider and you'll be able to work with it as if it were a physical object in the scene.

Related

Weird 2D physics in unity3d, making "Ballz" kind of game

As you see in the screenshot. When the ball hits between two colliders, it should do what red arrows shows. Instead it does what blue arrow shows. All the boxes in the game are perfectly next to each other.(Used an algorithm to make it perfect.).
I am using unity's own physics system. I only have this piece of code for the ball :
void Start()
{
rb = transform.GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(10.0f, -5.0f) * 41f * Time.deltaTime;
}
What "Ball" game object has;
Sphere Collider2D
Bounce physic in Collider2D {friction set to: 0, bounciness set to: 1}
Those are enough to achieve exactly the same physics of Ballz game.
When the ball hits an edge of a collider, one of these situation happens. This is okay, exactly what i want. But I do not want the same thing happen when the ball hits between two colliders.
I hope i'm clear enough.

Unity collider going through other colliders at times?

I'm trying to create a Pong clone for practice. I setup my project in 2D.
The paddle has the following components:
-Rigidbody2D (set to kinematic)
-BoxCollider2D (is a trigger)
The ball has the following components:
-Rigidbody2D (set to kinematic)
-CircleCollider2D (is a trigger)
The paddle is controlled via dragging (user dragging finger on screen left/right). I used the EasyTouch plugin for this.
Then I move the ball with this script:
void Update () {
transform.position += new Vector3(xSpeed * Time.deltaTime, ySpeed * Time.deltaTime);
}
This is how I detect collisions and redirect the ball once it hits something (Horizontal objects are the top/bottom/paddle while Vertical objects are the left/right screen border):
void OnTriggerEnter2D(Collider2D c)
{
if(c.gameObject.tag.Equals("Horizontal"))
{
ySpeed *= -1;
}
else if(c.gameObject.tag.Equals("Vertical"))
{
xSpeed *= -1;
}
}
The problem is sometimes the ball goes through the paddle which can look glitchy to the end-user. I've searched about this online and I've tried to set the rigidbody's Collision Detection property to Continuous instead of Discrete. But the ball still goes through the paddle at certain times.
Anyone know how to solve this? What am I doing wrong with how I setup/coded my game?
Thanks
You have a very simple concept error.
OnTriggerEnter2D(Collider2D) is to get if the collider has entered other collider. In other words you can go through the object.
you need to use this function instead:
OnCollisionEnter2D(Collision2D coll)
I suggest you to watch this Unity tutorial because it explains all this really good: https://unity3d.com/learn/tutorials/modules/beginner/physics/colliders
Additional info : http://docs.unity3d.com/ScriptReference/Collider2D.html
Thanks.
As the code shows, the transform is controlled by yourself while the continuous detection requires control of physics engine, so try to control the gameObject with physics engine instead give it a position modified by your own code.

Why does not my particle system collision work?

I have added a particle system where I have checked the collider option and added a world particle collider. See the image below.
In the script that is attached to the particle system I have:
void OnParticleCollision(GameObject other) {
Debug.Log("Particle was hit!");
}
The bullets that are fired donĀ“t seem to hit the particles since the above message is not printed. The bullets are spheres with a sphere collider and a rigidbody attached. The rigidbody is set to non-kinematic (the checkbox is not checked) if that matters.
Also, the bullet object has a script attached with the same lines as above:
void OnParticleCollision(GameObject other) {
Debug.Log("Bullet was hit!");
}
But it is not printed as well.
What am I missing?
Double check that you have this script attached to your particle system and not any arbitrary gameobject.
Check if you have "Is trigger" disabled on Sphere Collider, or you're particles too small and don't hit the actual collider.
Tried to simulate your situation, all works fine.

Collision Detection of objects in certain area of camera in Unity3d

I am creating a 3rd person action game where the player is a helicopter and he can shoot other objects while moving. The problem is I am trying to find the enemy objects who are inside a circle in the center of the camera and I need to track them and shoot them.
Raycast wouldn't help as i need a thicker raycast and so I tried spherecast and capsulecast.
I have a GUI element which gives the player idea on where he can shoot.When using Spherecast or Capsulecast, It is working when the enemy is near but when the enemy is far behind I guess the spherecast becomes small while traveling along z and doesn't hit the object most times.
if (Physics.SphereCast (startPoint, 1f, transform.forward, out hit)) {
if (hit.collider.CompareTag ("Shootable") ){
Debug.Log(hit.collider.name);
Destroy(hit.collider.gameObject);
}
}
I have seen raycast from camera and so i was wondering if there is something to do like circlecast from the camera which would be appropriate for this. If not how can I proceed?
Any help is really appreciated.
If you want to detect whether enemies lie within a conical area in front of your camera, using a SphereCast or RayCast will not be able to meet your needs.
Instead, you might consider checking the angle between an enemy's relative position and your camera's forward vector, to see if it is below a particular value, and hence within the cone.
For a 60-degree field of view, and assuming you store your enemy Transform components in an array/List, your code might look like:
foreach (Transform enemy in enemies){
if (Vector3.Angle(transform.forward, enemy.position - transform.position) < 30){
Destroy(enemy.gameObject);
}
}
Hope this helps! Let me know if you have any questions. (Answer adapted from this Unity question.)

unity3d - Instantiate and fire bullet relative to player rotation in 3D

This is a top-down view but in 3D coordinates, I would like to instantiate and fire a bullet from the player's gun. This script is on a spawner object at the end of the barrel. Also tried putting the script on the player itself but also didn't work.
GameObject projectile = Instantiate (bullet, transform.position, transform.rotation) as GameObject;
projectile.GetComponent().AddForce(transform.forward * speed);
The problem is the bullets doesn't behave as intended, instead they don't appear relative to the player rotation they just go in a very different direction. Shouldn't "Transform.Forward" mean forward in the Z position regarding the object's transform ?