Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

basic movement script unity

public float speed = 5;

void Update() {
    float x = Input.GetAxisRaw("Horizontal");
    float y = Input.GetAxisRaw("Vertical");     
    gameObject.transform.Translate(new Vector3(x,y,0) * speed * Time.deltaTime);
}
Comment

unity movement

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class 2dMovement : MonoBehaviour
{
  //This also works with joystick, PS this is topdown movement

 private Rigidbody2D rb;
 public float MoveSpeed = 15f;


 void Start ()
 {
   rb = GetComponent<Rigidbody2D>(); 
 }

 void Update ()
 {
    private float vertical;
    private float horizontal; 
 
    horizontal = Input.GetAxisRaw("Horizontal");
    vertical = Input.GetAxisRaw("Vertical"); 
 }

 private void FixedUpdate()
 {  
    rb.velocity = new Vector2(horizontal * MoveSpeed, vertical * MoveSpeed);
 }
}
Comment

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 an object jump in unity 
Csharp :: unity check load scene 
Csharp :: DateTime previous day c# 
Csharp :: c# repeat x times 
Csharp :: how to create an array in c# 
Csharp :: c# int to hex 
Csharp :: c# datagridview change column name 
Csharp :: despicable me 
Csharp :: ef core detach entity 
Csharp :: c# datetimepicker set weeks after today 
Csharp :: bootstrap modal 
Csharp :: how to pause physics in unity c# 
Csharp :: how to execute linux command from c# 
Csharp :: words counter c# 
Csharp :: unity clamp rotation 
Csharp :: .net mvc c# alert to client browswer window 
Csharp :: get query string parameter from string value c# 
Csharp :: c# compile into an exe 
Csharp :: c# get user appdata folder 
Csharp :: new line console c# 
Csharp :: unity normalize float 
Csharp :: how to change a string variables value c# 
Csharp :: c# void 
Csharp :: how to make an object move in unity 
Csharp :: c# append text to file 
Csharp :: discord bot in c# 
Csharp :: if button is pressed unity 
Csharp :: get all child gameObject of gameObject C# 
Csharp :: c# string remove 
Csharp :: c# add char to string 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =