Unity开发详解之旋转、移动、碰撞(3/6)

来源:互联网 发布:潮汕话软件下载 编辑:程序博客网 时间:2024/06/04 00:28


在前两篇中,我们已经创建好了场景和玩家对象,下面让玩家对象动起来。

玩家对象旋转

using System.Collections;using System.Collections.Generic;using UnityEngine;public class MouseLook : MonoBehaviour {    public float rotateSpeed = 3f;    public float minimumVert = -45f;    public float maximumVert = 45f;    private float _rotationX = 0;// Use this for initializationvoid Start () {        Rigidbody body = GetComponent<Rigidbody>();        if (body != null) {            body.freezeRotation = true;        }}// Update is called once per framevoid Update () {        _rotationX -= Input.GetAxis("Mouse Y") * rotateSpeed;        _rotationX = Mathf.Clamp(_rotationX, minimumVert, maximumVert);        float delta = Input.GetAxis("Mouse X") * rotateSpeed;        float rotationY = transform.localEulerAngles.y + delta;        transform.localEulerAngles = new Vector3(_rotationX, rotationY, 0);    }}

玩家对象移动

using System.Collections;using System.Collections.Generic;using UnityEngine;public class Move : MonoBehaviour {    public float speed = 9.0f;    public float gravity = -9.8f;    private CharacterController _characterController;// Use this for initializationvoid Start () {        _characterController = GetComponent<CharacterController>();}// Update is called once per framevoid Update () {        float deltaX = Input.GetAxis("Horizontal") * speed;        float deltaZ = Input.GetAxis("Vertical") * speed;        Vector3 movement = new Vector3(deltaX, 0, deltaZ);        movement = Vector3.ClampMagnitude(movement, speed);        movement.y = gravity;        movement *= Time.deltaTime;        movement = transform.TransformDirection(movement);        _characterController.Move(movement);}}

刚体碰撞

_characterController.Move(movement);

现在玩家对象在场景中的移动已经没有问题。接下来引入子弹

游戏图示、游戏下载、源码下载http://blog.csdn.net/d276031034/article/details/56016801

0 0
原创粉丝点击