I have problem with a simple spike script. I have attached the following script to the spike.
View it works also. The spikes are used in a jump down game. After the player has already jumped down several platforms the script is resolved even if the player does not touch the spikes.
public class CollisionSpikes : MonoBehaviour
{
private void OnCollisionEnter2D(Collision2D other)
{
SceneManager.LoadScene(0);
}
}
Here is a picture of how the spikes and their BoxCollider2D are connected to each other.
image
I first suspected when all colliders touched each other that this would lead to this error. But the problem still occurred. Here is a video so you can get an impression of the error:
youtube video
Does anyone here have an idea how I can fix this problem? I am relatively new to Unity and C# and unfortunately I am stuck here.
Thanks for your help. :)
Try to place a Debug.Log(other) to find out what is colliding with your spikes. Because you are not filtering it anything that touches it will trigger that code. My advice to you is to filter it. Use some tags.
Do something like
void OnCollisionEnter2D(Collision2D other)
{
Debug.Log(other); // Find out what is triggering the function
if(other.gameObject.tag == "player") {
SceneManager.LoadScene(0);
}
}
Related
just finishing off my demo scene but im faced with an issue, when the enemy boss is killed (disabled) i need the wall behind him to be disabled too automatically. However i cant figure it out, im horrible with script and intend to do a crashcourse youtube tutorial over my december break lol.
But right now i dont understand how thi sis supposed to work, ive done something similar where an item is destroyed upon coming into contact with the player which disables a wall further along in the game:
public class potionpickup : MonoBehaviour
{
public Collider2D _FF;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
_FF.enabled = false;
//this will immediately change the color to the transparent.. and only for that specific object
Renderer renderer = _FF.GetComponent<Renderer>();
renderer.material.color = Color.clear;
Destroy(gameObject);
}
}
And that works, but i cant seem to use the same method for this and googling it is getting me nowhere. Any help would be appreciated :D
for reference, the enemy is just called Enemy and the wall is called PassageWay
What i tried:
about 30 formats of script were tried each with about 2-6 errors i couldnt understand how to fix like "have you misused an assebly reference?" and im like "yeah, probably, got a solution or you just gonna tell me im wrong?"
I am working on a driving game for a project. My issue is that I'm unable to detect collision (I want to place the car to a checkpoint whenever it goes off the road) between the wheels of my car and the terrain. I tried to use this simple script but it does not seem to work:
using UnityEngine;
public class CarCollision: MonoBehaviour
{
private void OnCollisionEnter(Collision other)
{
if (other.collider.name == "Terrain")
{
Debug.Log("We got off the road!");
}
}
}
Is it a 2d game? If yes, use OnCollisionEnter2D instead of OnCollisionEnter.
Is the script attached to the gameobject?
Are you sure that the terrain has a collider which is not marked as trigger?
Try copying and pasting this same thing in the main controller.
I hope it worked. Don't forget to mark my answer as accepted if it worked!
(I am a new person in stackoverflow, might ask a bit different)
How to make a platformer in Unity?
Unity 2020.2.0a11.1312.3
Info before start: I want to make a mobile game.
(with some buttons)
Hello, I want to make a platformer in Unity.
I tried to use Rigidbody2D.AddForce() method but I couldn't get it working.
Can somebody help me?
I don't usually work on mobile games in Unity but these are generally the reasons why rigidbodies don't work.
Make sure to connect the script to the object with the rigidbody by dragging the script on to the object.
Use RigidBody2D Rigidbody2D = GetComponent<RigidBody2D>() at the start of your script.
If you're using the AddForce() method in the Start() function then you need to move it to the FixedUpdate() function.
Sometimes the movement does work but it doesn't move the object very fast because there isn't enough force so you might have to increase the force.
Check to make sure that there are no movement constraints applied to the Rigidbody.
Since you're working with buttons you will have to link the buttons to the script with the rigidbody like:
public void OnPointerDown(PointerEventData eventData)
{
//Start moving logic
moving = true
}
public void OnPointerUp(PointerEventData eventData)
{
//Stop moving logic
moving = false
}
private void FixedUpdate(){
if(moving){
//Moving
Rigidbody2D.AddForce(force * Time.deltaTime)
}else{
//Not moving
}
}
Since OnPointerUp/Down is only called once when the event happens you need to put your start moving logic in OnPointerDown and stop moving the object in OnPointerUp.
So I am working on a space shooter game, and I was changing the size of the box collider. And when I turned the game on there weren't any collisions happening between the player and the enemy. Collisions between the enemy and laser still worked though. I can't put images in here, but Stack Overflow provided me with some links to the images. If I didn't provide you with enough of the code or anything else, please say so. I don't know what is going wrong and it's killing me. So can you see what is wrong?
ps: I'm very new to Unity
Player code:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Enemy")
{
Damage();
}
}
Enemy code
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Laser")
{
Destroy(other.gameObject);
Destroy(this.gameObject);
}
if (other.tag == "Player")
{
Destroy(this.gameObject);
}
}
Player Inspector
Enemy Inspector
Laser Inspector
[by "there weren't any collisions happening" do you mean
the method "OnTriggerEnter" is not called or
the methods in your if statements are not called?
For 1. Check if the player has a 2Dcollider attached.
For 2.: you use the "tag", did you tag the Player?
to fix some mistakes it is helpful to use:
Debug.Log("Enter message");
so you get a message in the console and can see if your Trigger even works or your if statement is the problem.]
edit: i looked up the inspectors and the player collider has the "IsTrigger" unchecked. not sure if that's the problem.
Your collider is unchecked in your player, also you need to check trigger in that collider.
Currently I have implemented distance grabber in Unity. My problem is that I would like to disable physics between player and object for countering a wide range of bugs.
I would like to know what would be the correct way to disable the collisions between objects in VR setting.
My current code; does not work in implementation, and is used as extensive module for distance grabbable object. I don't care about the optimality of the code during run-time at the moment, as first step is to actually get the thing to do the bidding.
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
//this.gameObject.GetComponent<Collider>().enabled = false;
//this.gameObject.transform.parent = collision.gameObject.transform;
Physics.IgnoreCollision(this.gameObject.GetComponent<Collider>(), collision.gameObject.GetComponent<Collider>(), true);
}
}
private void OnCollisionStay(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
Physics.IgnoreCollision(this.gameObject.GetComponent<Collider>(), collision.gameObject.GetComponent<Collider>(), true);
}
}
private void OnCollisionExit(Collision collision)
{
this.gameObject.GetComponent<Collider>().enabled = false;
if (collision.gameObject.tag == "Player")
{
//this.gameObject.transform.parent = null;
Physics.IgnoreCollision(this.gameObject.GetComponent<Collider>(), collision.gameObject.GetComponent<Collider>(), true);
}
}
The commented part I tried testing on, but the OVR distance grabber is designed so that it breaks if I change the parent of object during runtime.
I have checked that most of these problems are not relevant in older versions of Oculus Integration, especially before 1.39.
Use the layer based collision detection feature. Make one group for the player, one group for the grabbed object, and one for everything else. When you grab something, move it to the grabbed object layer, and when you let it go, put it back in the everything layer.
Check the checkboxes in edit->project settings->physics so that the player layer and the grabbed object layer don't collide.
Okay, so the problem is not unambigious.
We can implement a system, which considers deltatime, but the real problem is with the Oculus Rift grabbing system.
I asked around and found out the easiest way to deal with this problem is to discard the Avatar prefab and directly add the CharacterController to the CameraRig gameObject. This way we don't need to deal with the libraries included within Oculus Rift integration package.