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 GFarina · Jul 15, 2010 at 01:13 AM · raycastswitch

Raycast switching object's color on and off.

What I'm looking for is a way to use this raycast script to switch back and forth. I.e. tap my object once for on and again for off. Any ideas?

var deadReplacement : GameObject; var deadReplacementPosition : Vector3; var deadReplacementRotation : Quaternion;

var hit : RaycastHit; var dieSound : AudioClip; var initialDelay = 1.0; var fadeSpeed = 1.0; var explosion : Transform; var damagesound : AudioClip; var myTransform : Transform;

function Awake() { myTransform = transform; // this objects transform }

function Update () { for (var evt : iPhoneTouch in iPhoneInput.touches) { if (evt.phase == iPhoneTouchPhase.Began) { var ray = camera.main.ScreenPointToRay(evt.position); if (Physics.Raycast(ray, hit, 10) ) { var hitObject : GameObject = hit.transform.gameObject; var hitTransform : Transform = hitObject.transform; if ( hitObject.tag == "Player" ) { if ( myTransform.position.x == hitTransform.position.x) { // matches x position if ( myTransform.position.z == hitTransform.position.z) { if ( myTransform.position.y == hitTransform.position.y) { // matches z position // so this is the object touched in the 2d space deadReplacementPosition = hit.transform.position; deadReplacementRotation = hit.transform.rotation;

renderer.material.color.b = 0.0; renderer.material.color.g = 50.0; renderer.material.color.r = 0.0;

             }           

else {

renderer.material.color.b = 0.0; renderer.material.color.g = 0.0; renderer.material.color.r = 50.0;

        }}
     }               
  }
 }

}}}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bampf · Jul 15, 2010 at 02:00 AM

Is the code not working? Does it compile?

Some suggestions.

If it's simply not doing anything, break down the problem. First figure out if the tap code is working. One simple way is to call Debug.Log in each bit of code. First after the tap, then after the raycast, and lastly when deciding on the color. See where the code actually goes. Maybe the tap works and the raycast doesn't - maybe the player is more than 10 units from the screen? Maybe the ray hits something but it's the wrong object. Etc.

If I had to guess, I'd say it's the test for the Player tag that's messing you up. My guess is you have this script on an object that isn't tagged Player. If so, either get rid of that test or check that the object that this script is on is tagged Player.

Another possibility is that the object you want to be tapped doesn't have a collider on it, so the raycast will not detect it. Or, it's too far from the screen point and you'll have to increase the raycast distance.

A more elegant design for this, instead of every object having this script on it and doing a raycast, you could run this code only once, and send a "tapped" message to whichever object was hit. Some objects would ignore the message, others would react, if they wanted.

Also, I don't see why you need to test myTransform positon is equal to hitTransform position. First of all, comparing floating point numbers is inherently weak. The slightest numerical difference breaks the test. Secondly, you are already testing the hit object's tag == Player.

Hope one of these suggestions helps.

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 GFarina · Jul 15, 2010 at 10:14 PM 0
Share

Thanks for taking the time to help me with this, Bampf. I will check into the "tapped" message. I appreciate your help!

avatar image Bampf · Jul 16, 2010 at 08:29 PM 0
Share

To be clear: what you are sending is a message of your own invention, to tell the object to do something. The message could be "Have a nice day". In your case, you'd be telling the object it got tapped... $$anonymous$$y point is, you'll still need code somewhere to detect the player's taps. It will look similar to the code you posted, but you could put it on each object and have them detect their own taps, or you could put it on a single object and have it send a message to each tapped object.

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

How to develop a script that substitute a gameobject position with my character? 0 Answers

How can I reset a function (boolean), like ON and OFF? 1 Answer

Camera switch between child back to main camera issue 2 Answers

how to turn a lamp on with a switch? 1 Answer

Seperate switches for individual lights 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