您的位置:首页 > 移动开发 > Unity3D

Unity3D控制物体移动

2016-05-23 11:49 435 查看
在游戏开发中,游戏物体的移动是最基本的,如果连物体都移动不起来,那就不用说玩游戏了。

下面记录了自己在开发中实现的物体移动,不同项目可能有不同的物体移动实现,也有很多种不同的物体移动实现,文章会持续更新。

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">}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: