- Home /
I solved the problem
Raycast is constrained to the horizontal axis
SOLVED
The transform.forward is contained to the horizontal axis. it doesn't rotate vertically making my weapon shoot horizontally even when my weapon is pointing upwards. any ideas as to why this is happening? thanks!
Here's my code:
if (Physics.Raycast (arv2.transform.position,
arv2.transform.forward, out hit_var, wep_range) &&
Time.time >= time_to_fire) {
Debug.DrawRay (arv2.transform.position,
arv2.transform.forward,Color.red);
time_to_fire = Time.time + 1f / fire_rate;
shooting = true;
Debug.Log ("arv2 hit");
}
SOLUTION:
The problem had to do with my Timestep. My Timestep was set to 0.002, I switched it back to default 0.02 and the problem went away.
Go to:
edit - project settings - Time
Now edit the Timestep
Answer by BGOKMEN14 · May 13, 2018 at 04:49 AM
Hello @Fibonacci_0_1_1,
The reason that is the case is because you are using transform. forward, meaning the raycast is restricted horizontally.
When looking up, is the arv2 also rotated?
Yes, when i Look up, arv2 is rotated accordingly. While playing the game with arv2 selected in the editor window in global mode, i can clearly see the blue arrow adjusting properly. For some reason my raycast is constrained horizontally but may RARELY point exactly where I want it to point.
Hello again! I found the problem ins$$anonymous$$d of using transform.forward, you need to use "transform.TransformDirection(Vector3.forward)"
So it would look like this:
if (Physics.Raycast (arv2.transform.position,
arv2.transform.forward, out hit_var, wep_range) &&
Time.time >= time_to_fire) {
Debug.DrawRay (arv2.transform.position,
arv2.transform.TransformDirection(Vector3.forward) ,Color.red);
time_to_fire = Time.time + 1f / fire_rate;
shooting = true;
Debug.Log ("arv2 hit");
}
I hope this helps and have a great day
I tried replacing
arv2.transform.forward
with
arv2.transform.TransformDirection(Vector3.forward)
it didn't work though, thanks for your help, I appreciate it.
Follow this Question
Related Questions
Raycast returns null for no apparent reason 0 Answers
[help]RayCast check [SOLVED] 2 Answers
ScreenPointToRay ray not coming through from camera 0 Answers
Raycast not working 2 Answers
Can you figure out raycast origin position from RacyastHit? 2 Answers