- Home /
Get an Object to Face/Move to RayCast Hit.point?
Hey! I was wondering how to get an object to move where the raycast ends or hits a collider. What I want to do is instantiate a bullet that always goes where the player shoots (so to check where that is, using RayCast). Anybody know how to do that? A script example would be much appreciated!
Can you post your shooting code? The Raycast should be part of the shooting script. You would get the movement direction or end position from this raycast. Note if you have gravity turned on for the projectile, the position hit will not be the same as the raycast.
I just want it to move in the direction of the ray cast hit and yes I am aware of that. I just use
if(Physics.Raycast(FirePoint.position + Spread, FirePoint.forward, out Hit, Range))
so its pretty straightforward raycasting. Yes this was what I was intended on doing I was just not sure how.
Answer by apomarinov1 · Nov 30, 2013 at 05:03 PM
This could be the bullet script:
Vector3 endPoint = Vector3.zero;
float speed = 50f;
void SetEndPoint(Vector3 p)
{
endPoint = p;
}
void Update()
{
if(endPoint != Vector3.zero)
transform.position = Vector3.MoveTowards(transform.position, endPoint, Time.deltaTime * speed);
}
Where you create the bullet you get a reference to it and send it the SetEndPoint function:
Transform bullet = Instantiate(...);
bullet.SendMessage("SetEndPoint", hit.point);
Furthure more if you are shooting at objects that might get destroyed, you could also send that object's reference to the bullet too, by adding a variable and a function for it:
Transform enemy;
void SetEnemy(Transform e)
{
enemy = e;
}
Then you should add a direction variable for the enemy direction, so if the enemy is dead before the bullet hits him, it continues in the same direction:
Vector3 enemyDirection;
void Start()
{
enemyDirection = (enemy.position - transform.position).normalized;
}
And you would add to the Update:
void Update()
{
if(endPoint != Vector3.zero)
transform.position = Vector3.MoveTowards(transform.position, endPoint, Time.deltaTime * speed);
if(enemy)
transform.position = Vector3.MoveTowards(transform.position, enemy.position, Time.deltaTime * speed);
else
transform.position += enemyDirection * Time.deltaTime * speed;
}
Interesting suggestions, thanks for your input! Although I have to say I do not like the idea of setting an "endpoint" for the bullet. Couldn't I just tell the bullet to keep moving forward in case there is no end point anymore (because an object or enemy/player moved out of the way)?
if(endPoint == Vector3.zero)
{
transform.Translate(Vector3.forward * projectileSpeed * Time.deltaTime);
}
Other then that I really like your solution to this thanks for your idea!
Well yeah, that is what I did with the enemy variable there. But this is more like a self pointing missle. If I got you right, firing the bullet in a direction will be sufficient, so you just instantiate it, set it's end point just so you can calculate the direction to that point. And then use the direction to move the bullet, if it hits something, cool, if the thing moves away the bullet continues. I dis that in a TD game demo, but there were many misses that the player would've thought were hits, so I made the bullets go to the enemies with the missle approach.
Okidoki I will try it out :) Just another question though...wouldn't the bullet then go towards the enemy and not the actual hit point? :P
Yes that is correct. The hit.point is in world coordinates so even if the bullet goes toward that point, if the target moves the hit.point will not point to the first hit. I don't know if that would work, but to always move to a certain point on a moving object I guess you could try transfor$$anonymous$$g the hit.point in the local space of the target at the beginning and then when you need that hit point transform it in world coordinates that way you'll always get the new world position of the hit point to where the bullet would go.
For me its not working right now but I believe this is because of the bullet itself. I'm going to have to do some more research. But thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613123122im_/https://answers.unity.com/themes/thub/images/avi.jpg)