Make player die if they fall off the platform - unity3d

So, iam making a 3d endless runner game with unity. I want the game to stop(just like when the player hits an obstacle) when my character falls of the platform. Keep in mind that when starting the game the character's ***y position is -1.4 ***
I tried this code but it didnt work:
if ( transform.position.y < -1.5)
{
//printing game over-stopping the game
SC_PlayerMove.instance.gameOver = true;
}

I tried your code, and it worked for me when I placed it inside the update function. If you add a Debug.Log inside the if statement, check that it prints when the condition occurs. If it does, then it might be an issue with how you're handling game over.

Related

Checking raycast hit on tagged collider

I'm trying to make a shooting game and I wanted to check if an enemy is hit using a raycast. Here is the code:
void CheckForShooting()
{
Vector3 mousePos = Input.mousePosition;
RaycastHit2D bulletCheck = Physics2D.Raycast(gunPoint.position,Camera.main.ScreenToWorldPoint(mousePos), gunRange);
Debug.DrawLine(gunPoint.position,Camera.main.ScreenToWorldPoint(mousePos),Color.white);
if(Input.GetButtonDown("Fire1"))
{
if (bulletCheck.collider.tag =="Enemy")
{
print("Hit");
}
}
}
However, even if the raycast is right on top the red enemy the console doesn't print "Hit" and I get the error "NullReferenceException: Object reference not set to an instance of an object", the line that is getting this error is this one bulletCheck.collider.tag =="Enemy".
Here is a ss:
Screenshot]
You need to make a raycast everytime you click the Fire1
Look at this official unity site: https://learn.unity.com/tutorial/let-s-try-shooting-with-raycasts# its explained how to shoot with raycasts.
I wish i could help you directly but i haven't entered Unity in over a year. I hope this helps but for such simple questions its faster to make a google search in my opinion
You just need to check if collider you were supposed to hit is null or not (if it was actually hit). You shoot the ray and expect it to always hit the target in your current code. Just check:
if(bulletCheck.collider != null)
and the code will only run if there was a hit. Only then you can check what was hit.
Also follow the advice and learn about NullReferenceException, it is the most common and basic exception, also one of the easiest to solve.

unity collision doesnt work, what is the problem in my code?

im trying to track collsion on a unity project and i dont get whats wrong
thats the code i'm working with.
im trying to destroy an object on collision but it doesnt work for some reason.
void OnCollisionEnter(Collision col)
{
if (col.collider.gameObject.tag == "Enemy")
{
Destroy(col.gameObject);
Debug.Log("collided");
}
}
it hits the object but nothing happends i dont get a message and the object doesnt get destroyed
Assuming that the script you posted is a component of some kind of a character (which it should be since you are looking for an Enemy using its tag) and you have proper Rigidbody components with proper settings and Colliders, you should use:
void OnCollisionEnter(Collision col)
{
// You can also use col.gameObject.tag == "Enemy" since it does the same thing as CompareTag().
// Although using col.gameObject.tag == "Enemy" is less performant.
if (col.gameObject.CompareTag("Enemy"))
{
// Using print to get proper information on what is happening.
print("destroyed " + col.gameObject.name);
// Destroying the gameObject with the tag "Enemy"
Destroy(col.gameObject);
}
}
If you wanted to destroy the player object then you should change the tag accordingly and have this script as a component of the enemy.
The most common issues if nothing is showing up can be:
the object has no RigidBody (or RigidBody2D if you are in a 2D project) component
the 2 objects are in 2 layers which don't collide with others (or the same layer which don't collide with itself). Check both objects' layers, and go to Edit -> Project Settings -> Physics (or Physics2D if the project is in 2D) and go at the bottom and check that the layers collide with each other (the check box is on)
one of the 2 colliders is in 2D and the other in 3D

Why 'Is Kinematics' works different on HoloLens?

Overview
I wanted to have a cube, that I can drag around the scene with the components Collider, Rigidbody and ObjectManipulator. In play mode everything works fine, but running it on the hololens, the cube starts flying around after dragging it a few time.
Steps to reproduce (All components are set via editor, not via code)
Create new project and set it up for AR/HoloLens
Import MRTK 2.4
Create cube with a Box Collider. Set Is Trigger = true
Add Rigidbody to cube. Set Use Gravity = false and Is Kinematic = true
Add Object Manipulator to cube. I have a method getting called after On Manipulation Ended, but don't know if thats important.
Expected behavior
The rigidbody is set to Is Kinematic = true and Use Gravity = false, so that the cube stays still/stops moving after releasing dragging it. This works while trying it inside the unity play mode. But running it on the hololens, the cube behaves like Is Kinematic = false and starts flying around after interacting with it. Sometimes after the second drag and sometimes after the third time drag.
Error
Before updating this post, I didnt noticed the development console in left corner of my hololens. At the beginng of the scene I get the message [Physics.PhysX] BV4 midphase only supported on intel platforms but at that moment everything is fine. As the cube begins to fly around I get the a NullReferenceExeption: Object reference not set to an instance of an object.
I fixed my issue. I know the approximate cause, but I do not fully understand it. The method, getting called after OnManipulationEnded caused that.
I have a list, getting filled and drained by OnTriggerEnter/-Exit (exit looks the same, except add→remove):
private void OnTriggerEnter(Collider other){
if (other.gameObject.layer != 31) return;
_objectsCollidingWith.Add(other.gameObject);}
OnManipulationEnded triggered this method:
private int GetMeshes(List<KeyValuePair<Transform, Mesh>> transMeshes){
foreach (GameObject go in _objectsCollidingWith)
{
transMeshes.Add(new KeyValuePair<Transform, Mesh>(go.transform , go.GetComponent<MeshFilter>().mesh));
}
return transMeshes.Count;}
So I got alot of nullreferences from GetMeshes, because some gameobject in the list _objectsCollidingWith were null. Thats because the mesh is getting updated every once in a while. That caused a lot of nullreferences until the cube just flew away.
I used the whole time the logging provider via the device portal and couldnt see what is causing this errors. But after running the project via holographic emulation I could see in the console where they were coming from.
How did I fixed my problem?
I found this post because I realized that my OnTriggerExit didn't get called and cased having null objects and some spatial meshes with the same name triggered OnTriggerEnter very often. Also I added this line in the foreach loop in GetMeshes because once in a while there is still a null object:
if (go == null)
continue;
PS: Please forgive the strange code formatting, somehow the editor here on so does not allow me to place the brackets somewhere else

How to disable a collider when the trigger of another collider has been entered?

I am building a game where the player runs on a path. When the player triggers a collider, 2 enemy objects will spawn.
What I want is when the first collider trigger has been entered, I want the second collider, which is at a certain distance from the first collider, to get disabled for a certain time. How to achieve this?
If you'd like to disable the colliders so they won't hit or rebound off the wall, for example, then you can change your collider's "isTrigger" variable to true, to change it into a trigger volume instead of a solid collider. This has the effect of disabling it - in that it won't cause other objects to stop or rebound if they hit it.
For example:
function Update() {
if (Input.GetKeyDown(KeyCode.X)) {
collider.isTrigger = true;
}
}
Note that things like MouseOver still work.
If you want to disable that completely, you can try collider.enabled = false. I'm not sure if that works or not. If it doesn't, you can always scale down your collider:
var myOldSize:Vector3;
function DisableBoxCollider(myCollider:BoxCollider)
{
//actually just resizes it
myOldSize=myCollider.size;
myCollider.size=Vector3(0,0,0);
}
function EnableBoxCollider(myCollider:BoxCollider)
{
if(myOldSize!=Vector3(0,0,0))
myCollider.size=myOldSize;
}
You can use the above code to integrate it in your own project. I'm not going to spill out all of the code for you because else we'd miss the point of learning to program and post on Stackoverflow in general. But it should help you to get on your way. Try and play some with the code, and if you have questions, get back here and ask them, providing the question with some code to show what you have tried.

Cannot Check Collisions -Unity

I am currently developing an AI demo for a fighting game using Unity game engine. I wrote an OnTriggerEnter that detects fists or kicks; however it detects actions if and only if the characters move. I tried rigidboy.WakeUp() and rigidbody.AddForce( new Vector3(0,0,0) ) but they did not solve the problem.
Problem is like this:
For example, I kick the other character, and my character's leg enters, and exits the collider but, OnTriggerEnter is not called until I move my character.
How can I solve this problem?
Here is my code:
function OnTriggerEnter(collision : Collider)
{
if( collision.gameObject.CompareTag("playerLeg") || collision.gameObject.CompareTag("playerArm" ) )
{
Debug.Log("CPU RıNGO! RıNGO!");
}
}
P.S. Also, I tried RaycastHit, and it did not work...
A bit of a silly question but, do you have IsTrigger=true to any of your objects? Also, both the colliding objects have a rigidbody??