کد حرکت کاراکتر به چپ و راست و بالا و پایین به صورت دو بعدی در یونیتی
using UnityEngine; using System.Collections; public class Ctrl : MonoBehaviour { public float speed = 1.5f; void Update () { if (Input.GetKey(KeyCode.LeftArrow)) { transform.position += Vector3.left * speed * Time.deltaTime; } if (Input.GetKey(KeyCode.RightArrow)) { transform.position += Vector3.right * speed * Time.deltaTime; } if (Input.GetKey(KeyCode.UpArrow)) { transform.position += Vector3.up * speed * Time.deltaTime; } if (Input.GetKey(KeyCode.DownArrow)) { transform.position += Vector3.down * speed * Time.deltaTime; } } }
اسم اسکریپت سیشارپ رو بذارید Ctrl که همنام با کلاسی هست که توی کد بالایی اومده.
منبع :
http://answers.unity3d.com/questions/667641/how-do-i-move-my-2d-object-using-arrow-keys-also-h.html
اینم یکی دیگه :
using UnityEngine; using System.Collections; public class Player : MonoBehaviour { public float Gravity = 21f; //downward force public float TerminalVelocity = 20f; //max downward speed public float JumpSpeed = 6f; public float MoveSpeed = 10f; public Vector3 MoveVector {get; set;} public float VerticalVelocity {get; set;} public CharacterController CharacterController; // Use this for initialization void Awake () { CharacterController = gameObject.GetComponent(“CharacterController”) as CharacterController; } // Update is called once per frame void Update () { checkMovement(); HandleActionInput(); processMovement(); } void checkMovement(){ //move l/r var deadZone = 0.1f; VerticalVelocity = MoveVector.y; MoveVector = Vector3.zero; if(Input.GetAxis(“Horizontal”) > deadZone || Input.GetAxis(“Horizontal”) < -deadZone){ MoveVector += new Vector3(Input.GetAxis("Horizontal"),0,0); } //jump } void HandleActionInput(){ if(Input.GetButton("Jump")){ jump(); } } void processMovement(){ //transform moveVector into world-space relative to character rotation MoveVector = transform.TransformDirection(MoveVector); //normalize moveVector if magnitude > 1 if(MoveVector.magnitude > 1){ MoveVector = Vector3.Normalize(MoveVector); } //multiply moveVector by moveSpeed MoveVector *= MoveSpeed; //reapply vertical velocity to moveVector.y MoveVector = new Vector3(MoveVector.x, VerticalVelocity, MoveVector.z); //apply gravity applyGravity(); //move character in world-space CharacterController.Move(MoveVector * Time.deltaTime); } void applyGravity(){ if(MoveVector.y > -TerminalVelocity){ MoveVector = new Vector3(MoveVector.x, (MoveVector.y – Gravity * Time.deltaTime), MoveVector.z); } if(CharacterController.isGrounded && MoveVector.y < -1){ MoveVector = new Vector3(MoveVector.x, (-1), MoveVector.z); } } public void jump(){ if(CharacterController.isGrounded){ VerticalVelocity = JumpSpeed; } } }