unity move character
C#
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
[SerializeField] private float speed = 5.0f;
private void Update()
{
var horizontal = Input.GetAxis("Horizontal");
var vertical = Input.GetAxis("Vertical");
transform.Translate(new Vector3(horizontal, 0, vertical) * (speed * Time.deltaTime));
}
}using UnityEngine;
using System.Collections;
// This script moves the character controller forward
// and sideways based on the arrow keys.
// It also jumps when pressing space.
// Make sure to attach a character controller to the same game object.
// It is recommended that you make only one call to Move or SimpleMove per frame.
public class ExampleClass : MonoBehaviour
{
CharacterController characterController;
public float speed = 6.0f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
private Vector3 moveDirection = Vector3.zero;
void Start()
{
characterController = GetComponent<CharacterController>();
}
void Update()
{
if (characterController.isGrounded)
{
// We are grounded, so recalculate
// move direction directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"));
moveDirection *= speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
}
}
// Apply gravity. Gravity is multiplied by deltaTime twice (once here, and once below
// when the moveDirection is multiplied by deltaTime). This is because gravity should be applied
// as an acceleration (ms^-2)
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
characterController.Move(moveDirection * Time.deltaTime);
}
} float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float speed = 5.0f;
void Update(){
transform.position = new Vector3(horizontal, 0, vertical) * speed * Time.deltaTime;
}
Also in C#:
- Title
- modificare una strinfa in c#
- Category
- C#
- Title
- c# close window
- Category
- C#
- Title
- how to add a componet to a gameobject throgh code unity
- Category
- C#
- Title
- c# change colour of console
- Category
- C#
- Title
- c# get desktop path
- Category
- C#
- Title
- define enum c#
- Category
- C#
- Title
- c# thread sleep
- Category
- C#
- Title
- c# string to memorystream
- Category
- C#
- Title
- creating a c# class
- Category
- C#
- Title
- json ignore property c#
- Category
- C#
- Title
- c# add object to array
- Category
- C#
- Title
- convert string to boolean c#
- Category
- C#
- Title
- c# convert Unix time in seconds to datetime
- Category
- C#
- Title
- convert system.byte a string c#
- Category
- C#
- Title
- find gameobject with tag
- Category
- C#
- Title
- C# .net core convert int to enum
- Category
- C#
- Title
- input get button uibutton
- Category
- C#
- Title
- c# how to compare 2 dates without time
- Category
- C#
- Title
- while loop in c#
- Category
- C#
- Title
- git set origin
- Category
- C#
- Title
- unity 2d Drag object
- Category
- C#
- Title
- how to reload app.config file at runtime in c#
- Category
- C#
- Title
- how to make a pause feautre in unity
- Category
- C#
- Title
- c# reverse a string
- Category
- C#
- Title
- unity load scene
- Category
- C#
- Title
- c# clone stream
- Category
- C#
- Title
- how to turn a string in a char list c#
- Category
- C#
- Title
- button color uwp c#
- Category
- C#
- Title
- unix time c#
- Category
- C#
- Title
- dynamic in c#
- Category
- C#
- Title
- asp net mvc 5 return view from another controller
- Category
- C#
- Title
- .net core download image from url binary file
- Category
- C#
- Title
- what data type should be for contact number in asp.net
- Category
- C#
- Title
- c sharp split by newline
- Category
- C#
- Title
- unity set material color
- Category
- C#
- Title
- C# .NET Core linq Distinct
- Category
- C#
- Title
- wpf textblock line break code behind
- Category
- C#
- Title
- c# uppercase string
- Category
- C#
- Title
- unity material offset script
- Category
- C#
- Title
- convert array object to int[] c#
- Category
- C#
- Title
- c# start process
- Category
- C#
- Title
- call Textboxfor in cs
- Category
- C#
- Title
- add row and columns to grid wpf in code
- Category
- C#
- Title
- c# format number with leading zeros
- Category
- C#
- Title
- create char array c#
- Category
- C#
- Title
- visual studio c# mark class deprecated
- Category
- C#
- Title
- attribute usage c#
- Category
- C#
- Title
- countdown script unity
- Category
- C#
- Title
- unity mobile controls
- Category
- C#
- Title
- asp.net c# set session timeout
- Category
- C#
- Title
- c# polymorphism
- Category
- C#
- Title
- c# join array
- Category
- C#
- Title
- isprime c#
- Category
- C#
- Title
- exception handling c#
- Category
- C#
- Title
- how to add a list to observablecollection in c#
- Category
- C#
- Title
- unity how to change rotation
- Category
- C#
- Title
- check connection c#
- Category
- C#
- Title
- comments unity c#
- Category
- C#
- Title
- how to exit a program in c#
- Category
- C#
- Title
- The type or namespace name 'Scrollbar' could not be found
- Category
- C#
- Title
- c# udpclient receive buffer size
- Category
- C#
- Title
- split string
- Category
- C#
- Title
- how to compare datetime in c#
- Category
- C#
- Title
- how to add a gameobject
- Category
- C#
- Title
- change textbox location C#
- Category
- C#
- Title
- how to use more than one condition in ef join
- Category
- C#
- Title
- unity random float
- Category
- C#
- Title
- c# get object property value by name
- Category
- C#
- Title
- unity detect number key
- Category
- C#
- Title
- How to get the world position of the edge of an object?
- Category
- C#
- Title
- how to update a project to cross target .net core
- Category
- C#
- Title
- unity add component
- Category
- C#
- Title
- c# select first value from list
- Category
- C#
- Title
- c# do loop
- Category
- C#
- Title
- create a file in the directory of the exe and write to it c#
- Category
- C#
- Title
- x angle oes back after 90 unity
- Category
- C#
- Title
- eventos c#
- Category
- C#
- Title
- c# properties
- Category
- C#
- Title
- unity assign button onclick
- Category
- C#
- Title
- set label position winforms
- Category
- C#
- Title
- unity rotate around axis
- Category
- C#
- Title
- C# downloadstirng download old
- Category
- C#
- Title
- eager loading c#
- Category
- C#
- Title
- c# array isn't working
- Category
- C#
- Title
- accessing form controls from another class c#
- Category
- C#
- Title
- how to join array indexes with comma in c#
- Category
- C#
- Title
- c# empty array
- Category
- C#
- Title
- asp.net model display name
- Category
- C#
- Title
- newtonsoft create dynamic object
- Category
- C#
- Title
- delegate function c#
- Category
- C#
- Title
- in unity i want to destroy a gameobject when it hits the edge of the screen
- Category
- C#
- Title
- get all child gameObject of gameObject C#
- Category
- C#
- Title
- c# tostring mmm dd yyyy
- Category
- C#
- Title
- c# check if type implements interface
- Category
- C#
- Title
- c# tab character
- Category
- C#
- Title
- unity fall damage c#
- Category
- C#
- Title
- unity how to change the text on a button
- Category
- C#
- Title
- how to track a branch on github
- Category
- C#
- Title
- why is c# say ; expected
- Category
- C#
- Title
- exception handling c# stack overflow
- Category
- C#