- Home /
How do i detect if a certain cube is being touched?
I have a number of cubes on the screen and i want to touch one to change scene. All the cubes are flat and do not rotate.
So I'm asking how to make the condition to detect if the cube is being touched and then im going to put this line in the function Application.loadLevel(i);
Answer by mposwalSG · Jun 21, 2013 at 08:08 PM
You can use Camera.ScreenPointToRay() and Physics.Raycast() to do a raycast from the point you are touching on screen. If that ray collides with one of your cubes you can have it load the appropriate scene.
http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenPointToRay.html http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
Answer by Kaivo · Jun 22, 2013 at 12:41 AM
Not sure if this is exactly what you are doing but I had a menu I created with 4 cube objects. I attached a script on each of them that where like that:
using UnityEngine;
using System.Collections;
public class startGame : MonoBehaviour
{
public string level;
void OnMouseDown()
{
Application.loadLevel(level);
}
}
As long as the mouse pointer is hovering the object, clicking calls OnMouseDown().
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnMouseDown.html
this works but i wonder if there is a way to make one script for all the cubes ins$$anonymous$$d of multiple scripts
@imgredug For that to happen you need to use variables and basicly say when the mouseisdown and it is variable.
Or you can use mposwal's idea and use Raycasting to check if your mouse is pointing at a cube and then check if your mouse is down.
yea i have that idea in $$anonymous$$d and the On$$anonymous$$ouseDown one, im just trying to figure out how to have one script for multiple cubes and each cube has a different scene to go to
@imgredug How about creating a single script attached to each object and adding a public string where you place the scene name in the inspector?
Answer by Kreg Neg · Jun 21, 2013 at 07:01 PM
I had the same problem a month ago! I ended up using OnMouseExit and OnMouseEnter found in the docs.
that works for touching thought that was only for mouse hovering ?
Answer by DricoJD · Jun 21, 2013 at 08:10 PM
for touching you could use OnTriggerEnter() Function.
So your code could look like this:
Function OnTriggerEnter()
{
function Application.loadLevel(i);
}
Answer by KoshX87 · Jun 22, 2013 at 01:54 AM
There are many answers so far, and most of the look useful. I enjoy writing scripts and this one took a bit to make, but it will work for any cube you use. You just drag it onto the cube you want and in the inspector, under this code's title there will be a place where you need to input the name of the scene that you want it to load or the number of the scene based on its position in the build settings (i.e Application.LoadLevel(0);) will load the level with index 0. Heres the script:
// You can fill in both. It will by default use the levelNumber for the scene. If you want a cube to go to the third level you have just put in 2 for the levelNumber and leave the levelName blank. If you want to use a level name you just put in the name of the scene that you want to load.
var levelName: string;
var levelNumber: int = 0;
// this is the player or thing that will touch the cube. NOTE: in this script the player has the tag “Player”. To add tags just go to the top of the inspector and add a new tag if there isn’t already one called “Player”. Alternatively, you can call the tag something else, but you need to change this script. ONE MORE THING: if you’re player isn’t going to be in the game until later on, you need to change the start function to Update or do something because, the start function will go off as soon as the cube is in play.
private var player1: GameObject;
function Start(){
player =GameObject.FindWithTag(“Player”);
}
//Here is the function where you touch the cube. Since this is placed on the cube. Your cube will need a trigger, so that the player can touch it. To add a trigger go to: AddComponent => Physics => box collider. This will add you a trigger, which you can adjust in the inspector on the right. In the inspector make sure that the isTrigger option is ticked or this wont work.
function OnTriggerEnter(object: Collider){
if (object == player){
if (levelNumber){
Application.LoadLevel(levelNumber);
}
if (levelName){
Application.loadLevel(levelName);
}
}
}
PS: One last important thing to note is that it is Application.LoadLevel and not Application.loadLevel and not function Application.loadLevel either
Oh one more thing. I wrote this in word. So when you run this in unitron or monodevelop, you $$anonymous$$IGHT have to rewrite the quotation marks before and after (-> "Player" <-), because the fonts sometimes cause problems.
Crap! I was just looking over the script and in the two if statements you need to replace (levelNumber) with (levelNumber != null) and replace (levelName) with (levelName != null). I don't think they will work unless you change them.
Also where the levelNumber is assigned to 0, change var levelNumber: int = 0; to var levelNumber: int; , because you don't want there to be a value. Cause if you type in no levelNumber, it would use 0 anyway if its kept like i've written it up there.
Do not call people a time waster even if the code is not up to scratch! Because these people take time to right code, just ask question
@Adnan2012 your attitude is not welcome here. I don't see any answer by you, you just came here to copy-paste someone else's work (just like your other write-my-code questions). Why don't you spend your time more constructively, ins$$anonymous$$d of plagiarizing and writing nasty comments, how about you learn to code.
woooohhhh !!!!!! Weird !!! Ok SOrry I am just learning sir :(