Unity3D - How can I detect collison between my car object and terrain? - unity3d

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!

Related

Problem by collision with OnCollisionEnter2D class

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);
}
}

How to make a platformer in Unity?

(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.

Detecting Collisions in Unity?

I'm making a game in Unity where I want collisions between two moving objects to be detected (One of which is moved by the player using touch. For testing reasons I'm currently writing the script for mouse controls). However for some reason when the game object that is moving moves into the collider field of the object that needs to trigger an event when collided with, nothing happens. I added colliders to both objects and added Is trigger to the collider of the object that needs to trigger the event and as required but it still doesn't work.I tried it with the code that is supposed to trigger the event first and it didn't work and then I simply tried to use debug.log to see if the problem was with the code related to the event I want to get triggered but nothing works. Does anyone know how I might solve this problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Hit Detected");
}
}
You need to put a Rigibody on the 2 objects,
if your project is:
2D: "Gravity Scale" to 0 (so they don't have gravity)
3D: Set checkBox "Use Gravity" to false

There is no reason for this Unity script to make the sprite move, yet it moves

Here's the code:
using UnityEngine;
public class playerMovement : MonoBehaviour {
public Rigidbody2D rb;
public float strength = 100f;
void Start () {
//Initialize the body of the sprite so that forces
//can be applied.
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void FixedUpdate () {
//Note2Self: var says this is a variable of unspecified type.
var touch = new Touch();
/*
if (touch.phase == TouchPhase.Began){
rb.AddForce(transform.forward * strength);
}*/
if (Input.anyKey)
rb.position.Set(0, 100);
}
}
I was trying to practice some basic stuff in Unity (I am not used to programming in IDE's at all, we've just used vim in my program so far) when I happened upon this oddity.
First, I didn't understand why the sprite would move at all when there can't be a touch identification, since I haven't actually tested this on a mobile device. So I commented it out and for some reason the sprite still moves. That code shouldn't be doing anything, yet it is.
I have checked to see if the sprite is using the up-to-date script - it is - and I have checked if the script is targeting the correct rigid body and that it is a rigidbody2D. It is.
What is going on?
If it is just falling it is probably effected by gravity.
You can turn this off in your script by adding rb.gravityScale = 0; at the end of your Start() function
OR
by setting it in the editor inside the rigid body component
I looked in the unity documentation:
https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
which says that applying a Rigidbody2D component to an object will place it under control of the physics engine.
The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be controlled from scripts using forces.
I have run into issues with rigidbodies on more than one occasion, I suggest you check the RigidBody2D component in the unity inspector window and make sure to uncheck use gravity.
Also, you may want to just write a custom script without using a rigidbody. Doing a search on youtube will probably give you exactly what you need for that. Hope this helps!

How to get an OnTriggerEnter2D to recognize objects entering it?

I am trying to create am endless jumper and I want to use a Cube to detect the Platforms and destroy them.
The problem I am having is that the Triggers aren't being recognized by these cubes.
The green is the destroy Cube.
I have the RigidBody2D and BoxCollider2D on the destroyer.
public class DestroyPlatform : MonoBehaviour {
void OnTiggerEnter2D (Collider2D collider)
{
Debug.Log("I am in the destroy Wall");
}
}
This is the code I am using. As I mentinoed I just want it to tell me it triggering. I have seen tons of code on how to destroy it, but this basic code isn't working.
All of the platforms are 2D Sprites and are using the following:
Notice both GameObjects are triggers. I have also tried to put the script on the platform prefabs and the destroyer cube.
Thanks for the help.