Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Simple FPS movement script C#

Discussion in 'Scripting' started by iSleepzZz, Nov 8, 2015.

  1. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    201
    I have this:
    Code (CSharp):
    1.     void Update() {
    2.         CharacterController controller = GetComponent<CharacterController>();
    3.         if (controller.isGrounded) {
    4.  
    5.             moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    6.             moveDirection = transform.TransformDirection(moveDirection);
    7.  
    8.             moveDirection *= speed;
    9.  
    10.             if (Input.GetButton("Jump"))
    11.                 moveDirection.y = jumpSpeed;
    12.  
    13.         }
    14.         moveDirection.y -= gravity * Time.deltaTime;
    15.  
    16.         controller.Move(moveDirection * Time.deltaTime);
    17.     }
    However, I want it to be more game-style ish.
    Like when I press W, I want it to move forward at "Speed" fast.
    And I want to be able to press A (left) while in-air and actually move left.
    Anyway I can do this? Anyone have the code for this? Anything?
     
  2. Mr_Teels

    Mr_Teels

    Joined:
    Jul 14, 2015
    Posts:
    14
    there should be a standart Script integrated ...

    older Post
     
  3. iSleepzZz

    iSleepzZz

    Joined:
    Dec 23, 2012
    Posts:
    201
    If you have ever used the built-in FPS character, you would know it stinks lmfao.
    I need something more cartoon-ish game style like I said above.
     
  4. Prella

    Prella

    Joined:
    Mar 11, 2020
    Posts:
    3
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;



    public class PlayerMovement : MonoBehaviour

    {
    public CharacterController controller;

    public float speed = 12f;

    // Update is called once per frame
    void Update()
    {

    float x = Input.GetAxis("Horizontal");
    float z = Input.GetAxis("Vertical");

    Vector3 move = transform.right * x + transform.forward * z;

    controller.Move(move * speed * Time.deltaTime);




    }
     
  5. ZDTTUTA

    ZDTTUTA

    Joined:
    Mar 20, 2021
    Posts:
    7
    Rigidbody has the parameter on the AddForce method() which you can set to ForceMode.Impulse I believe. this simulates a sharp and sudden force along a given axis. I.e. jumping! and you can apply a sideways force to still get that motion mid-air.
     
  6. Putcho

    Putcho

    Joined:
    Jun 1, 2013
    Posts:
    246
    Anyway I can do this?
    yes you can

    move this code out side the if(controller.isGrounded) and put it above if(controller.isGrounded)
    Code (CSharp):
    1.  
    2. moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    3. moveDirection = transform.TransformDirection(moveDirection);
    4. moveDirection *= speed;
    do that you should be able to move mid air
     
  7. prro1212

    prro1212

    Joined:
    Sep 4, 2021
    Posts:
    1
    so where do we have to put it in?? in the start function or the update function??