- Home /
RayCasting from an object and SmoothFollow
Main problem
I am making a space game styled like the game 'Rodina' at the moment, and it is mainly going well; however I am changing the control system to control better using raycasting, though I have run into a few problems. I have the code for a script here:
var hit : RaycastHit;
function Update () {
var ray = Camera.main.ScreenPointToRay(Vector3.forward);
if (Physics.Raycast(ray, hit)) {
Debug.DrawLine(ray.origin, ray.direction, Color.green);
}
}
This script is attached to the ship object. However, when I execute the script this happens:
The 'Debug.DrawLine' may be the problem, but as you can see, the line drawn goes almost the exact opposite way intended. Any ideas why this might be? I can post the main code if needed, as the controls for the ship also use the camera by using 'Camera.main.ScreenToWorldPoint', which could be interfering.
Other Problems
These are not really connected to the question asked, but I may as well ask them here so I dont clog up Unity Answers. The Unity script 'SmoothFollow' is good in all, but it doesn't allow proper rotations, and it seems to bug out my controls a little, as it is based on the way the camera moves with the ship. It's hard to explain, so I have pictures instead:
As you can see, the camera doesn't follow behind it correctly, the same is also true for banking upwards. The intended follow would look like the first image on the right-side, which is following at the right height behind the ship, whilst pointing up or down. So if anyone could help with a better way of going about the camera in order to accurately follow my ship that would be appreciated.
If needed I can post the main controls for the ship, as it may be interfering
Answer by robertbu · Sep 01, 2014 at 03:01 PM
You don't say what your Raycast() should be doing, so it is hard to give an accurate answer. But I can tell you that what you have now is wrong. Vector3.forward is a direction, not a position and is (0,0,1). Screen coordinates start in the lower left of the screen at (0,0), so your code is casting a ray from the lower left corner of the screen. My guess is that you want to Raycast from the center of the screen. Here are a few different ways of doint that:
Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, hit);
---
var ray = Camera.main.ViewportPointToRay(Vector2(0.5, 0.5));
Physics.Raycast(ray, hit);
var ray = Camera.main.ScreenPointToRay(Screen.width / 2.0, Screen.height / 2.0);
Physics.Raycast(ray, hit);
As for the SmoothFollow(), 1) that is a separate issue and should and 2) as someone who answers a lot of that kind of question, I don't understand the question well enough to answer. You need to give more of a description and include your code.
Thanks so much that was very helpful! And it worked perfectly. I actually needed it to come from the ship itself, but I modified the first method in order to facilitate that. Thanks!
Also as a side note, I have just submitted a question about the Unity SmoothFollow script, be sure to look at it when it is approved as I really need help. Thanks!
Your answer
Follow this Question
Related Questions
SmoothFollow Unity Script improvements? 1 Answer
Texture following cursor... 1 Answer
Smooth Follow Camera shakes violently 2 Answers
C # script error CS1729. 1 Answer