1. 程式人生 > >unity 實現鍵盤控制物體移動和轉向

unity 實現鍵盤控制物體移動和轉向

unity 的character controller不太好用,碰撞什麼的不好操作,經常需要自己寫人物移動的指令碼,所以就記錄一下。


這個指令碼程式碼很簡單,實現的是按下鍵盤上下方向鍵,人物前進後退,按下鍵盤左右方向鍵,人物向左或向右轉彎。一般情況下使用一個膠囊體代表player,將指令碼掛載在其上即可。

程式碼如下:

mainTransform.position = this.transform.position + new Vector3(0f, 3.1f, 0f);

if (Input.GetKey (KeyCode.LeftArrow)) {
            transform.Rotate (Vector3.up * -rotateSpeed * Time.deltaTime);
            mainTransform.Rotate (Vector3.up * -rotateSpeed * Time.deltaTime);
        }

        if (Input.GetKey (KeyCode.RightArrow)) {
            transform.Rotate (Vector3.up * rotateSpeed * Time.deltaTime);
            mainTransform.Rotate (Vector3.up * rotateSpeed * Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.UpArrow)) {
            transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime);
        }
        if (Input.GetKey (KeyCode.DownArrow)) {
            transform.Translate (Vector3.forward * -moveSpeed * Time.deltaTime);//小車控制時,前進後退movespeed前都有負號
        }

mainTransform指的是攝像機,如何不想攝像機作為player的子物體,又想攝像機跟隨人物移動,就可以這麼寫,前面需要獲取到攝像機物體,這裡的程式碼中我沒有寫出來。

相關推薦

unity 實現鍵盤控制物體移動轉向

unity 的character controller不太好用,碰撞什麼的不好操作,經常需要自己寫人物移動的指令碼,所以就記錄一下。 這個指令碼程式碼很簡單,實現的是按下鍵盤上下方向鍵,人物前進後退,按下鍵盤左右方向鍵,人物向左或向右轉彎。一般情況下使用一個膠囊體代表pl

unity 實現滑鼠控制角色移動角色頭部的血條顯示

在很多專案中,都有這樣的場景就是滑鼠點選一個位置,滑鼠所控制的玩家就會移動到指定的位置處,還有就是在戰鬥過程中任務頭上血條的UI跟隨顯示,今天就講一下如何實現通過滑鼠控制人物移動和人物頭部血條的顯示。 一、首先是搭建簡易的3D場景 這裡使用Terrain地形元件,安裝官方

Unity鍵盤控制物體左右旋轉前後移動的c#指令碼

using System.Collections; using System.Collections.Generic; using UnityEngine; public class CubeScript : MonoBehaviour {void Start () {}v

Unity中用觸控控制物體旋轉放大

using UnityEngine; using System.Collections; using System.IO; public class ScaleAndRotate : MonoBehaviour { private Touch oldTouch1; /

js 實現鍵盤控制方塊移動

需求: 在網頁上有一個方塊,我們需要鍵盤上下左右鍵控制方塊的移動。 程式碼: <!doctype html> <html> <head> <met

Unity簡單實現第三人稱人物的移動轉向

方法 區分 ron ica img 如果 方向 tro 技術分享 上圖不重要,因為實現人物的移動用的是動畫,沒有什麽可說的,主要是下面實現人物的轉向。 比如在一個平面中,玩家按了w和d鍵則人物會面向右前方向前進,如果此時玩家按了a和s鍵則人物會面向左後方向前進,那麽

Unity —— 通過鼠標點擊控制物體移動

技術 aps this lap sep CA sha pub mon 1 //ClickMove - - 通過鼠標點擊控制物體移動 2 3 using System.Collections; 4 using System.Collections.Generic

Unity3D 鍵盤控制物體平面移動(操作相對於攝像機方向)

Intro 目標是實現目標隨攝像機方向的不同而進行不同方向移動——而且,目標不需要隨攝像機一起旋轉。 使用攝像機的四元數旋轉 void HandleKeyboardAction() { var horizontal = Input.GetAxis("Hor

利用ITWEEN實現照相機的移動轉向

第一次寫部落格請多多指教呀~~ 廢話不多說,這次實現的功能是利用ITWEEN實現照相機的移動和轉向。 首先搭建簡單場景:兩個Cube,正方體作為移動目標,長方體作為方向目標 通過以下程式碼: using System.Collections; using System.Collect

unity三大控制物體移動的元件

Transform元件: Rigidbody元件: CharacterController元件:角色控制器元件也屬於物理元件,在Physic物理屬性下,我查資料顯示角色控制器主要用於第三人稱和第一人稱遊戲主角(人形為主)的控制 使用SimpleMove控制角色移動,這裡附上程式碼方便自己記憶

js實現鍵盤控制移動div

簡單原理: 獲取鍵盤keyCode,按一定規則改變指定div的邊距實現控制指定div的移動。需注意的是要考慮div的溢位問題,要編寫函式限制移動範圍。 實現效果: 使用者通過鍵盤的指定鍵指定div在web頁面內移動。 程式碼塊 html程式碼 &

Unity控制人物移動移動動畫

using System.Collections;using System.Collections.Generic;using UnityEngine;[System .Serializable ]public class Anim//遊戲控制動畫{    public An

js鍵盤控制div移動,解決停頓問題

版本 pan 原因 idt win jin false top val 問題版本代碼如下: 1 <html> 2 <head> 3 <title>鍵盤控制div移動</title> 4 <meta charse

Unity實現通用的物體高亮閃爍的方法(輪廓或整體高亮閃爍)

  一、匯入DoTween或DoTweenPro外掛到專案中 二、編寫一個控制物體輪廓高亮閃爍的指令碼,如下圖所示: using System.Collections; using System.Collections.Generic; using UnityEngine

安卓觸控手勢事件實現圖片跟著手指移動圖片縮放

效果如下: 佈局程式碼: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

滑鼠點選按鈕控制圖片移動或者鍵盤控制圖片移動(解決問題:parseInt失效問題,用Number)

在測試用滑鼠點選按鈕,使圖片左右上下移動: 效果如下: 程式碼如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title><

WASD鍵控制物體移動

指令碼掛在物體上 using System.Collections; using System.Collections.Generic; using UnityEngine; public class write : MonoBehaviour { private Transfo

Unity3D搖桿控制物體移動及圖片

Unity3D搖桿控制物體移動 指令碼掛在移動的控制器上 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems;

unity 滑鼠拖拽物體移動

    private Camera cam;//發射射線的攝像機     private GameObject go;//射線碰撞的物體     private Vector3 screenSpace;     pri

unity用剛體做玩家移動玩家看向滑鼠點

1 //人物移動的一些基礎屬性 2 private float speed = 5; 3 private float h, v; 4 private Rigidbody rig; 5 private Camera cam; 6 private Ani