- Home /
direct a raycast from point a to point b
hello everyone, i am attempting to make a space shooter game, and thought of adding solar pannels to the spaceships, that way people cant hide in a behind an object forever to prevent death. so the best way i could think of doing this is by using a colider on the light to detect when an object is in range to actualy be able to charge from the light and a raycast to be casted from the light to the spaceship`s solarpanel position do detect if there is something in the way. so my question is how do i input the direction to the raycast if i only have 2 positions?
thanks for the help everyone.
p.s. i did not include code simply because i did not think it would be nessesary to answer the question, but if its needed pls tell me and i will repost the question with code
when you said only two positions did you mean the game is 2d?
Answer by steakpinball · Dec 05, 2013 at 11:28 PM
To get a vector from one position to another simply subtract the source from the destination.
Vector3 fromPosition = source.transform.position;
Vector3 toPosition = destination.transform.position;
Vector3 direction = toPosition - fromPosition;
The same principle applies to any type of vector.
Answer by Eric5h5 · Dec 05, 2013 at 11:41 PM
Use Linecast instead of Raycast.
Would that shoot out from the current object's position to the target object?
Try this - It is just a pseudo code(Untested)
RaycastHit hit;
Vector3 fromPosition = source.transform.position;
Vector3 toPosition = destination.transform.position;
Vector3 direction = toPosition - fromPosition;
if(Physics.Raycast(source.transform.position,direction,out hit))
{
print("ray just hit the gameobject: "+hit.collider.gameObject.name);
}
hope this may help you. Nsks
Or how about just using Linecast instead of Raycast? No need to make things complicated.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123528im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Vector direction for a reticle 1 Answer
Raycast between TransformDirection and Vector3.Forward? 1 Answer
Generate a random direction within min and max angles 2 Answers
C# Raycast from Object direction (z-axis) + another Vector3 1 Answer
Declaring Vector in inspector for directional movement 1 Answer