Hello, I am trying to remake an FPS Movement Controller script for a flying script. So that the gravity will have no impact at all and the player will be able to go up and down.
I created a camera, attached it in front of the player controller, but the script doesn’t work as it should: if I press “A” - it goes to the right, if I press “D” - it also goes to the right. Same for the “W” and “S”.
Could you please suggest what is wrong with the script?
using UnityEngine;
using System.Collections;
public class watermovement : MonoBehaviour {
// This component is only enabled for "my player" (i.e. the character belonging to the local client machine).
// Remote players figures will be moved by a NetworkCharacter, which is also responsible for sending "my player's"
//location to other computers.
public float waterspeed = 10f; // The speed at which I run
public float waterjumpSpeed = 6f; // How much power we put into our jump. Change this to jump higher.
// Booking variables
Vector3 direction = Vector3.zero; // forward/back & left/right
float verticalVelocity = 0; // up/down
CharacterController cc;
Animator anim;
public Camera thecam;
// Use this for initialization
void Start () {
cc = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
// WASD forward/back & left/right movement is stored in "direction"
// direction = transform.rotation * new Vector3( Input.GetAxis("Horizontal") ,0, Input.GetAxis("Vertical") );
// direction = thecam.transform.forward;
// if (Input.GetAxis("Vertical") != 0)
// {
// // transform.Translate(Vector3.forward * waterspeed * Input.GetAxis("Vertical"));
// transform.Translate(thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
//
// }
if (Input.GetKey(KeyCode.W)) {
transform.Translate(thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
}
if (Input.GetKey(KeyCode.S)) {
transform.Translate(-thecam.transform.forward * waterspeed * Input.GetAxis("Vertical"));
}
if (Input.GetKey(KeyCode.A)) {
transform.Translate(Vector3.left * waterspeed * Input.GetAxis("Horizontal"));
}
if (Input.GetKey (KeyCode.D)) {
transform.Translate(Vector3.right * waterspeed * Input.GetAxis("Horizontal"));
}
// if (Input.GetAxis("Horizontal") != 0)
// {
// transform.Translate(Vector3.right * waterspeed * Input.GetAxis("Horizontal"));
// // transform.Translate(thecam.transform.right * waterspeed * Input.GetAxis("Vertical"));
// }
// This ensures that we don't move faster going diagonally
if(direction.magnitude > 1f) {
direction = direction.normalized;
}
}
// FixedUpdate is called once per physics loop
// Do all MOVEMENT and other physics stuff here.
void FixedUpdate () {
// "direction" is the desired movement direction, based on our player's input
Vector3 dist = direction * waterspeed * Time.deltaTime;
// Apply the movement to our character controller (which handles collisions for us)
cc.Move( dist );
}
}
Thanks!