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 MaleficVision · Aug 19, 2012 at 02:48 PM · androidobjects

Making Object Clickable(Need Guidance ASAP)

So I've been learning alot of things from the forums about using Unity and coding certain aspects of the application that I'm developing.

Now, part of our application is to click on a building and make it load another screen then displaying information about that building.

I searched the forums and most answers point to Raycasting.

I've read here that it

calculates the direction of a line corresponding to a screen position for the given camera. This line, in the form of a Ray object, can then be passed to Physics.Raycast. This function searches along the line from its origin until it makes contact with an object. The object returned by Physics.Raycast (in the RaycastHit parameter) is the object at the original screen position.

I've 1) downloaded his demo code and attached it to my First Person Controller. 2) Set the target1 parameter to my test model(a small building made in google sketchup) 3) Set the target2 parameter to my terrain. 4) I've also put a box collider on my test model. 5) I didn't edit any of the code.

I click play and when I click on the Test model it prints "Hit Nothing" but when I click on the terrain,it prints "Hit Target 2".

Can someone explain to me further how raycasting works?

What I know is that it returns the position and identifies the object where you "clicked" based on the distance calculated from the direction of the line corresponding to the screen position for the given camera..

I just need to be able to grasp this concept for me to get going in the development of our project.

I need to make the model be highlighted on hover and clickable and make it load another scene once it is clicked.

Is there any other alternatives on how to achieve what I want?

Please help me. Thank you in advance!

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

2 Replies

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

Answer by aldonaletto · Aug 19, 2012 at 03:42 PM

Physics.Raycast checks if the logical ray passed as argument hits some collider. If so, it returns true and fills the RaycastHit structure with info about the object. The object must have a collider, or it won't work. In your case, many things may be going wrong: the collider may be too small, or badly positioned, or something else is in front of it and blocking the building collider. You can change the script to a simpler thing like below, where the name of the clicked object will be returned - this may help you to understand what's exactly going on:

function Update () {
	if (Input.GetMouseButton(0)) {
		var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		var hit: RaycastHit;
		
		if (Physics.Raycast(ray, hit)) {
			print("Hit "+hit.transform.name);
		} else {
			print("Hit nothing");
		}
	}
}
@Chesley suggested OnMouseDown, which may be a better solution in your case: when you must do always the same thing independently of the object clicked, use Raycast in a camera script; when you need each object to have a different reaction when clicked, use OnMouseDown in the object's script:

function OnMouseDown(){
  print(name+" was clicked!");
}
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 MaleficVision · Aug 20, 2012 at 01:23 AM 0
Share

This did the trick. Thank you! :D Oh and the reason that my model can't be detected as HIT when it is clicked because I used a Box Collider ins$$anonymous$$d of a $$anonymous$$esh Collider.

Can I have a follow up question?

How can you make a 3D model glow on hover of the mouse? So that players may know that that object can be clicked or it will let them know it's an essential part of the game?

I've tried this one: function On$$anonymous$$ouseOver () { renderer.material.color -= Color(0.1, 0, 0); }

but it doesn't make the model glow nor does it do anything to the model. but when I tried this script to a cube gameobject it doesn't glow but changes it's color. I've read that I need to manipulate something in the ALPHA Channel of the model?

But how exactly can I do that?

avatar image
0

Answer by Chesley · Aug 19, 2012 at 03:06 PM

can't you use like:

 function OnMouseDown() {
       what ever happens when you click here
 }

sorry made an awnser, i should have made it a comment like this.

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 aldonaletto · Aug 19, 2012 at 03:43 PM 0
Share

I think this deserves to be an answer!

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

11 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

screen resolution in android 1 Answer

I can't see light effects in Android AVD 0 Answers

Can somebody help me out with a jumpscare? (Like from the game "Slender") 1 Answer

Application.OpenURL not working 1 Answer


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