x


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;

       }}
    }               
 }
}

}}}

more ▼

asked Jul 15 '10 at 01:13 AM

GFarina gravatar image

GFarina
1 2 2 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jul 15 '10 at 02:00 AM

Bampf gravatar image

Bampf
5k 8 19 49

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

Jul 15 '10 at 10:14 PM GFarina

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

Jul 16 '10 at 08:29 PM Bampf
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1532
x237
x6

asked: Jul 15 '10 at 01:13 AM

Seen: 1064 times

Last Updated: Jul 15 '10 at 01:18 AM