- Home /
Launching a Projectile to a Raycast
Hey guys, I did ask this question before but have since done some coding and done a few things need from my last question.
This time all I need is for a projectile to launch from one point to the point where the ray cast is colliding. Is this possible?
If possible I need someone to explain to me (in laymen terms as my code is noob level) or a script in java that has what I need.
Regards,
CastleForce (Trent)
Answer by Hybris · Mar 17, 2012 at 10:57 AM
Hello Trent,
Hmmmmm, if you want to spawn from the point you hit you could do this:
var power = 10;
var projectile : Rigidbody;
var launch = Instantiate(projectile, hit.point, //rotation and stuff);
//then apply force.
projectile.rigidbody.AddForce(Vector3.up * power);
//or as I prefer:
projectile.transform.TransformDirection(Vector3.up * power);
//wait is this right? I don't know...... I don't know the code to my preference exactly....
What is does is it spawns a object at the point the raycast hit and it adds force to the projectile or moves it.
And if you want a object to move to the raycasthit you could do this(never used this before):
var projectile : Rigidbody;
var spawn : Transform;
projectile.transform.position = Vector3.MoveTowards(spawn.position, hit.point, /*maxDistanceDelta =*/ 0);
The maxDistanceDelta is the max distance from the target. so if it's 0 it moves directly to it(if I'm right), but if it's in the negative if moves away, If the code doesn't work please notify me. I have never used this code so I don't know if it will work. And for some clarifaction:
Remember don't just use the scripts read them then adjust them and/or use them.
Good luck Trent,
Hybris
Your answer
Follow this Question
Related Questions
GetComponent() problems 2 Answers
Question about FPS and Raycasting Javascript 2 Answers
Firing a projectile along with raycast? 1 Answer
Raycast help..... 2 Answers