- Home /
Fps player control movement script
Hi there I'm a starter in unity and i came across a problem. Well first of all i dont code all that good, and second i can't get my movement to to follow my camera. Like if i press W i want it to go forward but if i turn the camera i want it to follow that direction. (catch my drift?) Here is my script code
public var MoveSpeed :float = 50; public var jumpSpeed : float = 55; public var MoveDirection = Vector3.zero; public var Gravity :float = 45; public var grounded : boolean = false;
function Start (){ }
function Update () { if( grounded ){ MoveDirection = new Vector3(Input.GetAxis("Horizontal"), 0 , Input.GetAxis("Vertical")); MoveDirection = transform.TransformDirection(MoveDirection); MoveDirection = MoveSpeed ; } if( grounded ) { if(Input.GetKey(KeyCode.Space)){ MoveDirection.y = jumpSpeed; } } MoveDirection.y -= Gravity Time.deltaTime; var Controller = GetComponent(CharacterController); var Flags = Controller.Move( MoveDirection * Time.deltaTime); grounded = (Flags & CollisionFlags.CollidedBelow) !=0; }
@ThaiThao and @Aldo Naletto did the above script work?
If so what exactly is the whole script? Did you just copy and paste that into the FPS controller script?
Sorry I'm a newbie and have never messed with JS before and am trying to compare between each one and seeing how things change. I'd very much appreciate if someone help me with this. $$anonymous$$y head is killing me trying to understand all this.
$$anonymous$$uch thanks :D
no i wrote that script.. but it's all fixed already ^^ it worked the first time but the movement wasn't following the camera.. but i wrote another script to fix it.. to pair the camera direction with the movement direction
Would you $$anonymous$$d sharing with me exactly what you wrote? How you implemented it and so forth? I'm not sure what I keep doing wrong but it is very frustrating.
$$anonymous$$uch Thanks.
Answer by aldonaletto · Oct 21, 2011 at 04:01 AM
You can do that with a few changes: add the variable turnSpeed and modify the beginning of the Update function:
... var turnSpeed: float = 60;
function Update(){ transform.Rotate(0, turnSpeed*Input.GetAxis("Horizontal")Time.deltaTime, 0); if( grounded ){ MoveDirection = new Vector3(0, 0, Input.GetAxis("Vertical")); MoveDirection = transform.TransformDirection(MoveDirection); MoveDirection = MoveSpeed; } ...
sorry but I've tried this and it still doing the same thing... I can move but then again the movement don't follow my camera direction...
Well, this is the Doom style move script: the "Horizontal" axis turns the character left/right, and the "Vertical" move it forth/back. It works fine IF the camera is childed to the character AND have no camera script. You can even use $$anonymous$$ouseLook.cs attached to the camera, but with only $$anonymous$$ouse Y control selected. Other camera scripts may not work, since the character doesn't know in which direction the camera is turned.
If you want to control the camera with the mouse, use your original script and attach $$anonymous$$ouseLook.cs to the character - but select only $$anonymous$$ouse X in the Inspector - and attach it also to the camera, this time with only $$anonymous$$ouse Y selected (remove other camera scripts, if any). Anyway, the camera must be a child of the character.
perhaps do you have $$anonymous$$mviewer? if you do contact me and i can send you my $$anonymous$$mviewer id and pass.. so you can have a look at what I'm doing wrong because i still can't get it to go the way i want. (email is dtpblk@gmail.com contact me what time you are available and what time area... like U$$anonymous$$ or US or something like that..)
Your answer
Follow this Question
Related Questions
Help with fps script 2 Answers
Checking in front of an object before moving 1 Answer
Need to 'un-roll' my FPS 1 Answer
Why is this player script not working 1 Answer
Camera Height-based crouch script 2 Answers