Hi all… im pretty new to Unity scripting and I am trying to do what seems like a very simple thing! i want to draw a line between the camera (3rd person) and the player. I am using
Debug.DrawRay (transform.position,Camera.main.transform.position);
btw the main camera is following my guy (like a third person shooter camera)
but the line seems to start miles away then draws to my guys feet (im happy it goes to the guys feet but it comes from what seems like nowhere ?). When I move the camera around the line does seem to move but its origin is not at the camera where I would like it to be.
Please help! im ripping what hair I have left out!
My intention is to detect if the line is hitting anything between the camera and my guy and move the camera to the position where the ray hits but this will come once I get the line from the camera to the guy! one step at a time kinda thing…
Im doing this so the camera doesn’t look through walls.
Is your camera’s transform component centered on the camera?
The line is drawing to your players feet most likely because the transform is at the players feet. The transform is the 3 arrows that you move them around with.
Make sure you don’t have any other camera’s in your scene… If so, I’d just make a public Transform called Camera and draw the ray using that.
I just tested your code and it’s working for me.
Also, make sure that the script you’re using to draw your ray isn’t also on the Camera. If it is, it’ll give some weird results.
cheers Epictickle ill give it a go!
forgot to say, I do have another camera in the scene but its turned off?.. will this make a difference?
It might not… I’d go ahead and just assign the Transform manually, though, to prevent any confusion.
My guess is that you have the DrawRay script attached to both the player and the Camera. I mean, I obviously can’t know for sure what it is, but I know for a fact i got it working when i tried lol. Let me know if it works, man! We’ll get this figured out. xP
No i dont have it attached to the 2… I have also looked through all the attached scripts for any rays that I have left in from other stuff and there is nothing there… Wierdly the end point of the ray moves around like as if it is looking at the camera (up down back and forth you know)
when i use
Debug.DrawLine (GameObject.Find(“LookHere”).transform.position, Camera.main.transform.position);
(i have a box object called lookhere)
this works just fine but I dont know if i can use this instead of ray? what is the purpose of DrawLine ?