Stop objects from passing-through eachother?

I’ve never did anything to do with collision, so please use simple terms when explaining stuff to me.

So I have a sphere that has a “sphere collider”, and the levels in my game are 3D models on their own, and I’m applying “mesh collidors” to them. I want a script that will stop the sphere from passing through the levels when it collides with them. The levels are squares with paths cut into them (the paths are made of rectangles and curves, so it’s a pretty simple model and there’s no problem with using a mesh collider).

If you use Rigidbodies they will automatically react to collisions.

How exactly do I use a rigidbody? (I know where it is, and some of the options, but I don’t know how to operate it correctly)

https://unity3d.com/learn/tutorials/modules/beginner/physics/rigidbody

I still can’t get it to work. But I think the reason for that is, that I’m using a script which keeps the sphere at the cursor’s position at all times.

Here’s the script:

using UnityEngine;
using System.Collections;
public class SphereMover : MonoBehaviour
{
    // Use this for initialization
    void Start() {}
    // Update is called once per frame
    void Update()
    {
        Vector3 mousePos = Input.mousePosition;
        mousePos.z = Camera.main.transform.position.y - 205.25f;
        var newPos = Camera.main.ScreenToWorldPoint(mousePos);
        newPos.y = 205.25f;
        transform.position = newPos;
    }
}

As you can see, the script also keeps the sphere at the same height, this is just to insure that the sphere doesn’t go outside of the level, or drop on the floor, which would cause it to collide with the level and ultimately reset the scene.

I think that the way to fix this would be to write a script that resets the scene as soon as the sphere collides with anything, instead of just stopping it from passing through. This was my ultimate goal with the collision, but I just wanted to stop the objects from passing through each other to test if the collision works. Now that I think about it, I could’ve also done it by making it reset the scene.

Sorry about that, this is only my 1st 3D game, and I’m only just starting with the very basics or coding in C#.

If it wouldn’t be too much to ask, could you please provide a script that will reset the scene once the rigidbody detects collision?

using UnityEngine;
public class Load : MonoBehaviour 
{
      void OnCollisionEnter() 
      {
         Application.LoadLevel(Application.loadedLevel);
      }
}

Is there anything specific I need to call the script? And what do I have to attach it to?

Thats an incredibly rudimentary thing to ask how to do. It doesn’t sound like you know the basics so you should start at the beginning and learn basics through the tutorials that Unity provides.

As I stated previously “I’m only just starting with the very basics of coding in C#”. Sorry, should have phrased that correctly, what I meant was I’ve just started to learn the very basics.

Also I asked those 2 questions in the previous reply since when I try to attach it to anything, it gives me this error.2204627--146477--attaching script error.png (I named the script “ResetLevel”)

The name of the script and the name of the class need to match up.

What’s the class name? (again, I’m just starting to learn. :wink: )

He called it Load

Ok, so I named the script “Load” and I’m no longer getting the error, however it’s still not resetting the scene.

Whoops, sorry again. It’s night time and my brain isn’t at it’s full capability.

It is resetting, it’s just that since the sphere is at the mouse’s position at all times, you can’t see it resetting.

Can someone please provide a script that will freeze my original script for 3 seconds whenever the scene starts, and change the cursor’s position to the sphere’s position once, and also freeze the cursor position for that 3 seconds (make it unable to move the cursor, even if the mouse if moving), so that the player will always start at the same exact starting position. (The starting position will be different for each level)

I chose 3 seconds, because I’m planning on adding an animation at the start of the scene which will make he sphere appear and will last 3 seconds.

bump.

bump.

Anyone?

basic way of implementing a timed delay is covered in the example in

you’ll need to setup a boolean which is set to false by default then after the delay is set to true.

The script you want to control will need an if statement to check the boolean you’ve setup. Give it a go and if you have trouble post what you have and we’ll guide you through :slight_smile: