Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by imgregduh · Jun 21, 2013 at 06:11 PM · gameobjecttouchcube

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image imgregduh · Jun 22, 2013 at 01:26 AM 0
Share

this works but i wonder if there is a way to make one script for all the cubes ins$$anonymous$$d of multiple scripts

avatar image DricoJD · Jun 22, 2013 at 08:28 AM 0
Share

@imgredug For that to happen you need to use variables and basicly say when the mouseisdown and it is variable.

avatar image Kreg Neg · Jun 22, 2013 at 04:00 PM 0
Share

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.

avatar image imgregduh · Jun 22, 2013 at 04:05 PM 0
Share

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

avatar image Kaivo · Jun 22, 2013 at 11:42 PM 0
Share

@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?

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image imgregduh · Jun 21, 2013 at 07:03 PM 0
Share

that works for touching thought that was only for mouse hovering ?

avatar image
0

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image imgregduh · Jun 21, 2013 at 10:16 PM 0
Share

this isnt working

avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KoshX87 · Jun 22, 2013 at 01:56 AM 0
Share

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.

avatar image KoshX87 · Jun 22, 2013 at 02:09 AM 0
Share

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.

avatar image DricoJD · Apr 12, 2014 at 12:39 PM 1
Share

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

avatar image AlucardJay · Apr 12, 2014 at 01:34 PM 1
Share

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

avatar image zero_null · Apr 15, 2014 at 06:18 AM 0
Share

woooohhhh !!!!!! Weird !!! Ok SOrry I am just learning sir :(

  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Select 2 out of 3 gameobject before doing something. 0 Answers

Detection if a GameObject is below you or next to you? 1 Answer

change the front point or head of a GameObject 1 Answer

How to drag two Cubes by Touch At the same time? 0 Answers

Moving GameObjects with mouse and check contains 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges