- Home /
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;
}}
}
}
}
}}}
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.
Thanks for taking the time to help me with this, Bampf. I will check into the "tapped" message. I appreciate your help!
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
Follow this Question
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