How to save a variable of a raycast??
If I raycast an object and the hit info gives me the name of the object. Lets say "cube". How can I save this string??? saveName =hit.transform.name But If the raycast hits an other Object lets say Sphere the code above will be updated. And the saveName is named as sphere. But I want to save the string of the cube and if the raycast hits another obect I want to compare if(saveName !=hit.transform.name) then do.....
I can not use Tags vbecause Im checkingt the triangle index`s of an object. and therefore I must know when the Index is chancing.
Thanks for links and advices...
Answer by itsharshdeep · Aug 04, 2016 at 12:53 PM
Hi
I think you can do like all of the cubes should be on same layers lets say 'RayCast'. & other gameobjects will be on another later say 'IgnoreRaycast' now you will check before saving the name that the hitted object is on raycast layer or not.. if it is on 'Raycast' only then it will save name
Thanks for your response but I cannot use Layers. Because Im checking the triangle index of an object. So its not possible to layer each triangle of the object. The cube and sphere example would work with layers. I need to know when the raycast changes the index.
Is there no other way?
Cheers
mm... First of all next time you need to post this response as a comment on the respective answer ins$$anonymous$$d of the adding a new answer.. because this is response to any answer not a answer..
Secondly can you provide more depth detailed on your project .. like the provided answer is response to the given example. So please update the example. So that we can re-think.
Sorry for the little rude.. but when newbies come to you solve their problem they will feel little typical to collect the structured response.
Thanks
Thanks for your advice, I was not aware of this function to reply..
Answer by NoseKills · Aug 04, 2016 at 03:46 PM
Your question is quite vague but from what i understand you could just store the name to a variable and compare it the next time you get a hit.
private string lastHitObjectName;
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.name == lastHitObjectName) {
// you hit an object with the same name as last time
}
// store the objects name so we can compare
// it on the next hit
lastHitObjectName = coll.gameObject.name;
}
Answer by psyhova · Aug 05, 2016 at 02:30 PM
I better specify my problem.
I have a camera with a Raycast transDirection.fwd. and hits a sphere. (works only with a mesh collider attached to a primitive) And this raycast gives me a triangle index back. When I move the camera the Triangle index is changing, if the ray hits another Triangle of the mesh.
What I need, is to print out "The triangle Index has changed"
My problem is that, if I store like this: var saveTriIndex = Hit.triangleIndex; The variable itself changes too. If the raycast hits a new triangle. So I cannot compare, because the variable already has changed trough the update. So I need the old TriangleIndex. Something like deltaTriangleIndex.....
function Update () {
Debug.DrawRay(transform.position,transform.TransformDirection(0,0,1)*1000,Color.red);
var hhh :RaycastHit;
if (Physics.Raycast(transform.position,transform.TransformDirection(0,0,1),hhh,1000))
{
print ("TriIndex"+hhh.triangleIndex);
//print(HOW TO PRINT TRIANGLE INDEX HAS CHANGED????????);
}
}
Your answer

Follow this Question
Related Questions
Third person shooter need some help 0 Answers
Making a portal inspired game - Need help! 0 Answers
LineRenderer doesn't go to Hit.Point 1 Answer
Raycast Performance 3 Answers