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
1
Question by Xedfire 1 · Mar 28, 2011 at 05:17 PM · raycastcolorselection

Help - Selection System

The game that I am making right now involves the player selecting an object in the scene , and then being able to manipulate it in many different ways. I already have a (kind of) working selection script, but it feels really bad and wrong. Basically, what happens is, when the player hits the spacebar, it selects the object that is lined up with a crosshair in the middle of the view (raycasting). The way that I show the player that the gameobject is selected is by changing it's material Color to blue instead of white. I then set a variable (selObj) to be the selected object. Is there a better way to do this? I am also having trouble with deleting the selObj too, but I think that this is because of the selection method.

Here is my current script:

var selObj : Transform; var oldObj : Transform;

 var fwd = transform.TransformDirection (Vector3.forward);
 var hit : RaycastHit;

if(Input.GetKeyUp(KeyCode.Space)) { if (Physics.Raycast (transform.position, fwd, hit)) { if(hit.transform.gameObject.tag != "NotSelectable") { oldObj.transform.renderer.material.color = Color.white;

          if(hasSelector == false)
         {
             selObject = hit.transform;
         }

         selObj = hit.transform;
         oldObj = hit.transform;
         selObj.renderer.material.color = Color.blue;


     }

}

Thanks in advance,

Xedfire

Comment
Add comment · Show 3
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 Statement · Mar 28, 2011 at 07:06 PM 0
Share

"I already have a (kind of) working selection script, but it feels really bad and wrong." - Why don't you post it here then and see what we can do about it?

avatar image Statement · Mar 28, 2011 at 07:07 PM 0
Share

"Is there a better way to do this?" - Eh, it's hard to tell without knowing what it is you want to do better. :)

avatar image Xedfire 1 · Mar 29, 2011 at 02:57 PM 0
Share

I added the script - it should work, but I had to re-order a few things

1 Reply

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

Answer by Statement · Mar 29, 2011 at 04:07 PM

You know, I'd create two scripts for this. One that handles the selection and one that handles coloring upon selection. See the two scripts below.


Selecter.js

var selected : Transform; static var unrequired = SendMessageOptions.DontRequireReceiver;

function Update() { if (Input.GetKeyUp(KeyCode.Space)) { var hit : RaycastHit; if (Physics.Raycast(transform.position, transform.forward, hit) && hit.transform.tag != "NotSelectable") { Select(hit.transform);
} } }

function Select(other : Transform) { if (selected) selected.SendMessage("OnSelected", false, unrequired); selected = other; if (selected) selected.SendMessage("OnSelected", true, unrequired); }

SelectedColor.js

OnSelected(false);

function OnSelected(selected : boolean) { renderer.material.color = selected ? Color.blue : Color.white; }


  • One advantage is that by using SendMessage we can notify each individual object that they are selected or not, so they can apply whatever logic they want when they are selected. You could even make some objects play a sound or change to a custom color of their own.

  • One disadvantage is that you'd have to put SelectedColor on each selectable item so if you have a lot of them it just becomes annoying to set up. In that case you might want to just do the material color change right in Selecter.

This solution is very like mine in Spotlight switch script? question, have a look there as well.

Comment
Add comment · Show 2 · 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 Xedfire 1 · Mar 29, 2011 at 06:57 PM 1
Share

Brilliant answer, just what I needed!

avatar image Statement · Mar 29, 2011 at 09:44 PM 0
Share

Glad to help :)

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

No one has followed this question yet.

Related Questions

Canvas UI - Get Pixel Colour from Image 0 Answers

[well commented code example] problem animating color.alpha of objects above the character 0 Answers

How to create a biological topography in Unity3D? 0 Answers

Advance when the sound is done playing 1 Answer

changing guiTexture color with raycast 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