1. 程式人生 > >Unity3D控制物體移動

Unity3D控制物體移動

在遊戲開發中,遊戲物體的移動是最基本的,如果連物體都移動不起來,那就不用說玩遊戲了。

下面記錄了自己在開發中實現的物體移動,不同專案可能有不同的物體移動實現,也有很多種不同的物體移動實現,文章會持續更新。

1.rigidbody.MovePosition()控制物體上下左右移動(簡單好用)

// Update is called once per frame
    void Update()
    {
        //控制移動
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);
    }

2.transform.Translate();上下左右移動

using UnityEngine;
using System.Collections;

public class Move : MonoBehaviour
{
    public float speed = 5;
    private Transform transform;

    // Use this for initialization
    void Start()
    {
        transform = this.GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        transform.Translate(new Vector3(h, 0, v) * speed * Time.deltaTime);
    }
}

3.點選滑鼠,物體移動到滑鼠位置(2D,transform.position)

if (isMouseDown && GameManager._instance.gameState == GameState.Runing)
        {
            if (lastMousePosition != Vector3.zero)
            {
                //Camera.main.ScreenToWorldPoint(Input.mousePosition)
                Vector3 offest = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition;//位移
                transform.position = transform.position + offest;
                checkPosition();
            }
            lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }

4.rigidbody2D.AddForce();(2D遊戲新增力左右移動物體,忍者跑酷)

void Update()
    {
        if (isSlide == false)
        {
            float h = Input.GetAxis("Horizontal");
            Vector2 velocity = rigidbody2D.velocity;


            if (h > 0.05f)
            {
                rigidbody2D.AddForce(Vector2.right * force_move);
            }
            else if (h < -0.05f)
            {
                rigidbody2D.AddForce(-Vector2.right * force_move);
            }
    }

5.點選滑鼠,物體移動到滑鼠的位置(3D,Vector3.MoveToWards()函式,BeatPlane飛機大戰)

實現程式碼:

protected Transform _transform;
protected Vector3 targetPos;//目標位置

    // Use this for initialization
    void Start()
    {
        _transform = this.transform;
        targetPos = this._transform.position;
    }

    void MoveTo()
    {
        if (Input.GetMouseButton(0))
        {
            //獲得滑鼠螢幕位置
            Vector3 mousePos = Input.mousePosition;
            //將螢幕位置轉為射線
            Ray ray = Camera.main.ScreenPointToRay(mousePos);
            //用來記錄射線碰撞記錄
            RaycastHit hitInfo;
            //產生射線
            bool isCast = Physics.Raycast(ray, out hitInfo, 1000, inputMask);
            if (isCast)
            {
                //如果射中目標,記錄射線碰撞點 
                targetPos = hitInfo.point;
            }
        }
        //使用Vector3提供的MoveTowards函式,獲得朝目標移動的位置
        Vector3 pos = Vector3.MoveTowards(this._transform.position, targetPos, speed * Time.deltaTime);
        //更新當前位置
        this._transform.position = pos;
    }

遊戲執行時,點選滑鼠,物體就會移動到滑鼠所點選的位置。

6.鍵盤WSAD控制物體的上下左右移動(這個比較複雜。。)

void Control()
    {

        //獲取滑鼠移動距離
        float rh = Input.GetAxis("Mouse X");
        float rv = Input.GetAxis("Mouse Y");

        // 旋轉攝像機
        m_camRot.x -= rv;
        m_camRot.y += rh;
        m_camTransform.eulerAngles = m_camRot;

        // 使主角的面向方向與攝像機一致
        Vector3 camrot = m_camTransform.eulerAngles;
        camrot.x = 0; camrot.z = 0;
        m_transform.eulerAngles = camrot;

        // 定義3個值控制移動
        float xm = 0, ym = 0, zm = 0;

        // 重力運動
        ym -= m_gravity * Time.deltaTime;

        //按鍵盤W向上移動
        if (Input.GetKey(KeyCode.W))
        {
            zm += m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.S))//按鍵盤S向下移動
        {
            zm -= m_movSpeed * Time.deltaTime;
        }

        if (Input.GetKey(KeyCode.A))//按鍵盤A向左移動
        {
            xm -= m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.D))//按鍵盤D向右移動
        {
            xm += m_movSpeed * Time.deltaTime;
        }<pre name="code" class="csharp">}