控制人物进行水平移动

14 篇文章 0 订阅

代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

///<summary>
///水平移动控制
///</summary>

public class PlayerControl : MonoBehaviour
{
    public Animator animator;
    float InputX;
    float InputY;

    private void Start()
    {
        //get animator
        animator = this.gameObject.GetComponent<Animator>();
    }

    public float Hor;
    public float Ver;
    public float Jup;
    public float x;
    public float y;
    public float RotateSpeed = 1;
    public float G;
    public float JumpSpeed;
    public float MoveSpeed;

    private void Move()
    {
        Hor = Input.GetAxis("Horizontal");
        Ver = Input.GetAxis("Vertical");

        if (Hor != 0 || Ver != 0 || Jup != 0)
        {
            Quaternion Dir = Quaternion.LookRotation(new Vector3(Hor, 0, Ver));
            this.transform.rotation = Dir;
            this.animator.SetFloat("InputX", Hor);
            //this.animator.SetFloat("_InputX", -Ver);
            //this.animator.SetFloat("InputZ", Ver);
            this.animator.SetFloat("InputZ", Ver);
            this.transform.Translate(0, 0, MoveSpeed * Time.deltaTime);
        }
    }
    private void MoveIncludeJup()
    {

        Hor = Input.GetAxis("Horizontal");
        Ver = Input.GetAxis("Vertical");
        Jup = Input.GetAxis("Jump");

        if (Hor != 0 || Ver != 0 || Jup !=0)
        {
            Quaternion Dir = Quaternion.LookRotation(new Vector3(Hor, 0, Ver));
            this.transform.rotation = Dir;
            animator.SetFloat("InputX", Hor);
            this.transform.Translate(0, Jup, MoveSpeed * Time.deltaTime);
        }
        //this.transform.Translate(0, Jup, MoveSpeed * Time.deltaTime);
        /*if (this.transform.position.y != 0)
        {
            this.transform.Translate(0, G * Time.deltaTime, 0);
        }
        else if (Input.GetKeyDown(KeyCode.Space))
        {
            MoveDir.y = JumpSpeed;
        }
        MoveDir += Vector3.down * G * Time.deltaTime;
        character.Move(MoveDir * Time.deltaTime);*/
    }

    private void RotateView()
    {
        //以Y轴旋转变动的是X的值
        x = Input.GetAxis("Mouse Y");
        //以X轴旋转变动的是Y的值
        y = Input.GetAxis("Mouse X");

        x *= RotateSpeed;
        y *= RotateSpeed;

        this.transform.Rotate(-x, 0, 0);
        this.transform.Rotate(0, -y, 0, Space.World);
    }

    private void Update()
    {
        Move();
        //RotateView();
    }
}

未附加相机、跳跃的水平人物移动控制。

1. 在人物文件夹中新建一个animator controllor,双击灰色区域新建BlendTree

2. 双击BlendTree,在左侧的Parameters(参数)点击“+”号命名两个参数
在这里插入图片描述
3.BlendType改为2D Simple Directional,下方的Parameters分别为2步骤中设置的两个参数,点击Motion栏下方的“+”号,选择Add Motion Field,添加5个,第1个为idle动画,其他4个为移动动画Pos XPos Y设置如下:在这里插入图片描述
4. 将脚本附加在人物上,运行效果可见。

P.S.代码中有不少用于测试的无效变量,没用上的都没用,可以删除。还有相机控制和跳跃控制存在问题,FilmStrom的这个视频可以帮助解决,跳跃再研究研究,更新了再发上来。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值