unity射击游戏:超萌射手(1)使用EasyTouch3.0控制主角

来源:互联网 发布:中国电信网络重构 编辑:程序博客网 时间:2024/05/17 22:25

前言


本文由作者@zx一路飞奔出品,转载请注明出处

文章地址:http://blog.csdn.net/u014735301/article/details/42705443

作者微博:http://weibo.com/u/1847349851


人物创建


(1)给人物加上碰撞器和刚体组件,并固定刚体不能移动和旋转的方向




(2)添加动画状态机,添加参数 Move Dead来控制动画切换



(3)测试:在人物上挂一个move脚本,同时在摄像机上挂一个follow脚本跟随主角



using UnityEngine;using System.Collections;public class PlayerMove : MonoBehaviour {    public float speed = 1;    private Animator anim;// Use this for initializationvoid Start () {        anim = this.GetComponent<Animator>();        }// Update is called once per framevoid Update () {        //控制移动        float h = Input.GetAxis("Horizontal");        float v = Input.GetAxis("Vertical");        //transform.Translate(new Vector3(h, 0, v) * speed*Time.deltaTime);        rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * speed * Time.deltaTime);        //控制动画        if (h != 0 || v != 0) {            anim.SetBool("Move", true);        } else {            anim.SetBool("Move", false);        }}}

using UnityEngine;using System.Collections;public class FollowTarget : MonoBehaviour {    public float smoothing = 3;    private Transform player;// Use this for initializationvoid Start () {        player = GameObject.FindGameObjectWithTag(Tags.player).transform;}// Update is called once per framevoid FixedUpdate () {        Vector3 targetPos = player.position + new Vector3(0, 1.3f, -1.3f);        //在smoothing * Time.deltaTime时间 动画位置移动从from开始到to结束。        transform.position = Vector3.Lerp(transform.position, targetPos, smoothing * Time.deltaTime);}}

这样人物就可以在场景内移动了


EasyTouch3.0插件


导入资源包,添加一个Joystick的实例



Hierarchy中会出现三个东东



属性:

Joystick name :摇杆名称

Enable joystick :是否显示摇杆

Activated : 和enable类似

Show debug area :勾选后会显示一个矩形的边缘区域

Use fixed update :是否使用fixed update,默认是update ,如果人物移动方法是用rigidbody.MovePosition的话,请勾选上,否则会出现移动时的卡顿

Use GUI Layout :是否使用GUI layout



属性:控制摇杆的位置的大小

Dynamic joystick :默认是不勾选的,如果勾选后,摇杆在屏幕上不显示,当有点击时会显示

Anchor :位置锚点

offset :x,y位置设置

Area radius :区域半径

Touch radius :中心圆心的半径

Restrict to area :限制在区域内

Reset finger exit :手指离开区域后重置位置

Dead zone radius :拖动范围在该半径内,没效果



属性:

Interaction type :交互的方法 Event Notification是通过事件控制移动

Broadcast messages :如果你使用js编写脚本,就必须要勾

Enable X axis 

Speed :控制移动速度

Inverse axis :反转控制方向

Smooth return :勾选后和Vector3.Lerp的效果类似

Enable inertia :勾选后有个惯性的效果,即停止操作摇杆时人物不会马上停止运




属性:控制摇杆的纹理,自己DIY一下。看看效果~


 


摇杆控制脚本


using UnityEngine;using System.Collections;public class PlayerMove : MonoBehaviour{    public float speed = 1;    private Animator anim;    void OnEnable()    {        //移动摇杆        EasyJoystick.On_JoystickMove += On_JoystickMove;        //停止移动摇杆        EasyJoystick.On_JoystickMoveEnd += On_JoystickMoveEnd;        //EasyButton.On_ButtonPress += On_ButtonPress;        //EasyButton.On_ButtonUp += On_ButtonUp;    }    // Use this for initialization    void Start()    {        anim = this.GetComponent<Animator>();       }    void On_JoystickMove(MovingJoystick move)    {        float angle = move.Axis2Angle(true);        //旋转Y轴,改变人物朝向        transform.rotation = Quaternion.Euler(new Vector3(0, angle, 0));        //移动物体        rigidbody.MovePosition(transform.position + new Vector3(move.joystickAxis.x, 0, move.joystickAxis.y) *speed* Time.deltaTime);        //播放动画        anim.SetBool("Move", true);    }    void On_JoystickMoveEnd(MovingJoystick move)    {        anim.SetBool("Move", false);    }}


之后就可以通过摇杆来控制




总结


欲知后事,请听下回分解!~~~~~~~




0 0
原创粉丝点击