- Home /
Lookat mouse, without raycasting?
Is there a way to make the player look at the mouse without using raycasting? For instance, if our player is walking along a narrow platform in a tds, it wont look at the mouse unless the mouse is over the platform?
Would there be a way around this? If you're giving examples could you please give them in C#, much appreciated.
Carl
additional info
Apologies I meant rotate the player! i tried to have a go at using the raycasting but i couldnt get it working,
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(hit.point - transform.position),
rotateSpeed * Time.deltaTime);
Your question is confusing. Are you talking about having the camera look at the mouse position only if it is over a platform? Or are you talking about making some "Player" Game object face the mouse position, only if it is over a platform?...
Take some time with your question to make it clear.
Is there a reason that you don't want to raycast other than it isn't working for narrow platforms? If not, my solution should work....
I don't even know how you understand what he's asking. I guess it "should work" assu$$anonymous$$g what you're assu$$anonymous$$g is assumed correctly.
as I understand it, he's using raycasting for his mouse-look script in a top-down shooter (tds). The problem is that, for narrow platforms, there's no geometry (on either side of the platform) for the raycast to hit, therefore the mouse-look will stop working.
For my answer, I recommend that he detaches the mouselook from being dependant on the level geometry and, ins$$anonymous$$d, has it looking for hits on a plane that is made to be a child of the camera (so it moves with the scene) and avoid the problem.
"as I understand it"... a.k.a. "What I'm assu$$anonymous$$g is..."
Haha.. no progress towards understanding. I love your answer. I'd like to take it to dinner and stuff. But it's all hinged on an ambiguous question.
Answer by Chris D · Jun 21, 2011 at 04:23 PM
I'd say, just create a plane (invisible, behind all your other objects, parented to your camera so it moves with the player character) and raycast to that. There won't really be much code difference associated with that change and you'll always be guaranteed to hit something.
Ok, I'm just going to toss out some functions that I think you should check out:
Also, these questions are for similar games - see if there's something you can adapt to yours:
Answer by Waz · Jun 21, 2011 at 11:20 PM
You dont need a real plane nor any raycasting at all. Just use Camera.ScreenToWorldPoint, noting that x,y is mouse and z is distance from camera to an imaginary plane where you want the player to look. If this plane is not fixed, calculate the distance from the players current z distance.