Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

unity 3d movement script

//make sure to add a CharacterController to the thing that you want to move

using UnityEngine;

public class PlayerMovement : MonoBehaviour

{

    CharacterController characterController;

    public float jumpSpeed = 8.0f;

    public float gravity = 20.0f;

    public float speed = 9.0f;

    private Vector3 moveDirection = Vector3.zero;

    private void Start()

    {

        characterController = GetComponent<CharacterController>();

    }

    void Update()

    {

        var horizontal = Input.GetAxis("Horizontal");

        var vertical = Input.GetAxis("Vertical");

        transform.Translate(new Vector3(horizontal, 0, vertical) * (speed * Time.deltaTime));

        if (characterController.isGrounded)

        {

            moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));

            moveDirection *= speed;

            if (Input.GetButton("Jump"))

            {

                moveDirection.y = jumpSpeed;

            }

        }

        moveDirection.y -= gravity * Time.deltaTime;

        characterController.Move(moveDirection * Time.deltaTime);

    }

}
Comment

PREVIOUS NEXT
Code Example
Csharp :: how to make build events always run visual studio 
Csharp :: static variables 
Csharp :: c# linq query map to entity 
Csharp :: how to detect a null bool C# 
Csharp :: Helper Routine GetRect¶ Calculates the area of a scaled down page: 
Csharp :: openxml row count 
Csharp :: stuck.hypixel.net ip 
Csharp :: where are the viwes in .net core publish 
Csharp :: c# regex double of some letters only 
Csharp :: how to make a console feedback 
Csharp :: visibility bound to radio button wpf 
Csharp :: Entity Framework 4 and caching of query results 
Csharp :: material Array setter 
Csharp :: what error code i should return in asp.net core whether user name or password are incorrect 
Csharp :: world space constant size 
Csharp :: Connect secretclient to proxy 
Csharp :: hardcode dropdown cshtml 
Csharp :: C# Bitwise Left Shift 
Csharp :: music file explorer c# 
Csharp :: OBSERVER 
Csharp :: c# configurationmanager load from file 
Csharp :: c# how to output array 
Csharp :: edit pdf itextsharip 
Csharp :: c# supplier equivalent 
Csharp :: player not following slide object unity 2d 
Csharp :: design pattern for so many conditions c# 
Csharp :: c# directory entry invoke 
Csharp :: ms transform 
Csharp :: unity photon base onenable 
Csharp :: unity matchinfo 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =