- Home /
Problem with raycast aim, need help ignoring layers/tags and pointing at the end of the ray.
I am making a space type dog fighting game similar to moonbreakers on steam. but i have been having a little trouble geting all the aims correct.
I decided that i am going to make a raycast to cursor position and move a empty object to the hit possition then have my forward guns and missle launcher aim at that, so far it works fine but as long as i am looking at something with a colider.
However, i want it so that if it is not hitting a colider then it just moves the empty game object to say the end of the ray cast. also i was wondering how i would add in anything that would make the ray ignore the "player" Layer or Tag.
here is what i have so far (My knowlege of java is poor, But i am taking the best steps i can to improve),
// Moves the object with this script to the point of the raycast hit
function Update () { // Send a ray through the cursor var ray : Ray = (Camera.main.ScreenPointToRay(Input.mousePosition)); // Do a raycast var hit : RaycastHit; if (Physics.Raycast (ray, hit)) transform.Translate((hit.point - transform.position)); }
Any help is much apreciated. Thank you.
Answer by AeonIxion · Nov 22, 2012 at 01:01 PM
For the ignore player problem: Rather than ignoring the player, you'll want the player to ignore the raycast. Click on your player object and change the layer to 'ignore raycast'.
As for moving the empty gameobject if not colliding, you'll want an else with a Translate that moves the object to ray.direction*value (where value is any value really).
else
{
transform.Translate(ray.direction*100);
}
Answer by Demon pete · Nov 23, 2012 at 10:36 AM
Ah thank you, looking at it now i don't know how i didn't figure that one out on my own, but you help has been much appreciated.
as for the ray cast ignoring player i got that one figured out eventually as i can't move it to the ignore raycast layer because i need the ship to interact with other ray cast i have going on. so far it works like a charm so i will put up the finished version or what i have tomorrow.
again thanks.
Your answer
Follow this Question
Related Questions
How make Raycast Aim to Crosshair? 1 Answer
How do I tell what NavMeshLayer a NavMeshAgent is currently on? 0 Answers
Touch and aim with a raycast line 2 Answers
Unable to set collision layer from scrip 0 Answers
Layermask (raycast) wont work... 4 Answers