- Home /
Move in direction of mouse look
Hi there....im working on a 3d 'space' type of scene and need the character (camera) to move in 3d space....i have assigned the mouse look and fps controller...my camera moves on a flat plane...i want it to move in the direction of the mouse look...i.e. when the mouse looks up and i press W the character moves up in space in the direction (not just Y axis, but diagonally)..i think it has something to do with raycast, but HOW.....is there anyone kind enough to assist me please....thanx
Answer by Meater6 · Apr 11, 2011 at 11:41 PM
Hmm, I wouldn't use the fps controller. I suggest just making a script that translates the character in the local-Z direction. So when you look up, it translates that way. Or you can use a rigidbody and set it to not be affected by gravity, and create a script that modifies the velocity in a transformed direction. This is extremely simple, so if you know any scripting it should be easy to accomplish. If you need an example use of this, comment on this answer saying so.
EDIT: Here is a question about how to make transformed directions with rigidbody.velocity: Question Here is a script section on how to make a block fly in the direction it is facing:
var speed = 10.0;
//The vector we are going to move by private var moveDirection : Vector3;
var speed = 10.0;
//The vector we are going to move by private var moveDirection : Vector3;
function FixedUpdate () { //Set moveDirection to the vertical axis (up and down keys) * speed //For smoother movement use Input.GetAxis instead of Input.GetAxisRaw moveDirection = Vector3(0,0,speed*Input.GetAxisRaw("Vertical")); //Transform the vector3 to local space moveDirection = transform.TransformDirection(moveDirection); //set the velocity, so you can move transform.rigidbody.velocity = moveDirection; }
Edit it to your needs. It pretty much is all you need to make your camera fly back and forth. For a strafing effect, look at the fps walker script for the "Horizontal" axis part (The thing that determines the left right key inputs). I hope this was helpful, and is the answer to your question.
will try this and get back to you with how it turned out...i appreciate your help...and i SUC$$anonymous$$ at scripting...loool
hi...tried looking into how to make the rigid body transform using mouse look direction...NO idea...if u can assist me please, i would be greatful
hi $$anonymous$$eater6...thanx for the code...was away from my computer for a couple of days...will try this later and get back to you and DaveA...thanx guys
if i could give u more than a vote up i would....works like magic...thanx $$anonymous$$eater6
Umm, there is the "Correct Answer" button which looks like a check mark. This tells people that it is the right answer. Plus, it gives +15 rep. Glad it works.
Answer by DaveA · Apr 11, 2011 at 11:33 PM
I think it has more to do with gravity. I modified FPSWalker, add these things:
public bool fly = false;
void FixedUpdate() { if (Input.GetKeyDown ("f3")) { fly = !fly; }
..... then the code that was there, then look for if (grounded)...
if (fly || grounded) {
... then look for Apply gravity moveDirection.y -= gravity * Time.deltaTime ....
// Apply gravity (if not flying) if (!fly) moveDirection.y -= gravity * Time.deltaTime;
i agree with setting gravity to zero. this should simulate a free fps controller.
Works for me. I put in a 'fly' mode by just ignoring gravity.
hi..thanx for the feedback, but ignoring gravity makes you not fall, but doesnt allow you to fly...i tried it previously and thats where i got stuck...thanx again though
Your answer
![](https://koobas.hobune.stream/wayback/20220613122139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
is it possible to have raycasts to cover the whole screen in one Update() time? 0 Answers
Moving player in direciton camera is facing 1 Answer
Raycast from one camera using a gameobject relative to another camera 1 Answer
Right way to set a canvas in stereo mode 0 Answers
Shoot nearest enemy with raycast 1 Answer