游戏制作之路(20)角色跳动之后怎么样检测接触地面

栏目: 后端 · 发布时间: 5年前

内容简介:在前面实现了角色进行跳动,可以在Y轴方向,也就是向天空方向进行移动了,并且也实现加速度的方式进行移动,可见这里大家要多学习物理知识,否则速度、加速度、重力是什么都不知道,这样是开发不了游戏的。不过,这个游戏还有一个问题,当你把角色走到地板边缘时,角色一不小心,就像掉到万丈深渊,并且还在加速往下掉。看来要解决这个问题了,那么怎么样解决呢?先看查看一下问题根源,如下这行代码:yVelocity -= gravity * Time.deltaTime;在游戏每帧里都会减少,不管角色在那里,也就是说角色的不断产生

在前面实现了角色进行跳动,可以在Y轴方向,也就是向天空方向进行移动了,并且也实现加速度的方式进行移动,可见这里大家要多学习物理知识,否则速度、加速度、重力是什么都不知道,这样是开发不了游戏的。不过,这个游戏还有一个问题,当你把角色走到地板边缘时,角色一不小心,就像掉到万丈深渊,并且还在加速往下掉。看来要解决这个问题了,那么怎么样解决呢?先看查看一下问题根源,如下这行代码:

yVelocity -= gravity * Time.deltaTime;

在游戏每帧里都会减少,不管角色在那里,也就是说角色的不断产生加速向下的速度,越来越大。因此,要想办法让角色碰到地面之后,就让它复位为0,这样就不会加速向下掉下去了。那么问题又来了,怎么样才能知道角色碰到地面了呢?我们来翻查unity手册,发现Character Controller里有一个属性:isGrounded,它的定义如下:

CharacterController.isGrounded

Was the CharacterController touching the ground during the last move?

它定义为bool类型,当角色最后一次移动是否触碰到地面,如果碰到就设置为true。为了检测到角色的触碰问题,应该使用角色的移动函数move() 或者 simpleMove(),并且地面添加了碰撞 Clider。

因此,添加地面检测之后,代码如下:

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

public class BasicMovement : MonoBehaviour {

    public float speed = 10.0f;
    public float rotationSpeed = 2.0f;
    public Transform head;
    public float maxHeadRotation = 80.0f;
    public float minHeadRotation = -80.0f;
    private float currentHeadRotation = 0;

    private float yVelocity = 0;
    public float jumpSpeed = 15.0f;
    public float gravity = 30.0f;

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (GetComponent<CharacterController>().isGrounded)
        {
            yVelocity = 0;
        }

        if (Input.GetButtonDown("Jump"))
        {
            yVelocity = jumpSpeed;
        }


        Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        //gameObject.GetComponent<CharacterController>().Move(transform.TransformDirection(input * speed * Time.deltaTime));
        gameObject.GetComponent<CharacterController>().Move(transform.TransformDirection(input * speed * Time.deltaTime + yVelocity * Vector3.up * Time.deltaTime));
        yVelocity -= gravity * Time.deltaTime;

        Vector2 mouseInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
        transform.Rotate(Vector3.up, mouseInput.x * rotationSpeed);
        //Debug.Log(mouseInput.x + ":" + mouseInput.y); 
        //head.Rotate(Vector3.left, mouseInput.y * rotationSpeed);
        currentHeadRotation = Mathf.Clamp(currentHeadRotation + mouseInput.y * rotationSpeed, minHeadRotation, maxHeadRotation);
        head.localRotation = Quaternion.identity;
        head.Rotate(Vector3.left, currentHeadRotation);

    }
}

这时,再去运行这个游戏,发现跳动碰到地面之后,就会变为0,如果在边沿也是慢慢地掉下去了。比如跳上一个箱子上面,再跳下来,就比较慢了。

不过,你会发现还有一个问题,角色在空中也可以跳动,这一般不符合常理的,因此要把跳动的代码移到接地判断里面,这样就不会在空气中进行跳动了,如下:

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

public class BasicMovement : MonoBehaviour {

    public float speed = 10.0f;
    public float rotationSpeed = 2.0f;
    public Transform head;
    public float maxHeadRotation = 80.0f;
    public float minHeadRotation = -80.0f;
    private float currentHeadRotation = 0;

    private float yVelocity = 0;
    public float jumpSpeed = 15.0f;
    public float gravity = 30.0f;

    // Use this for initialization
    void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
        if (GetComponent<CharacterController>().isGrounded)
        {            
            yVelocity = 0;
            if (Input.GetButtonDown("Jump"))
            {
                yVelocity = jumpSpeed;
            }
        }

        Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        //gameObject.GetComponent<CharacterController>().Move(transform.TransformDirection(input * speed * Time.deltaTime));
        gameObject.GetComponent<CharacterController>().Move(transform.TransformDirection(input * speed * Time.deltaTime + yVelocity * Vector3.up * Time.deltaTime));
        yVelocity -= gravity * Time.deltaTime;

        Vector2 mouseInput = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
        transform.Rotate(Vector3.up, mouseInput.x * rotationSpeed);
        //Debug.Log(mouseInput.x + ":" + mouseInput.y); 
        //head.Rotate(Vector3.left, mouseInput.y * rotationSpeed);
        currentHeadRotation = Mathf.Clamp(currentHeadRotation + mouseInput.y * rotationSpeed, minHeadRotation, maxHeadRotation);
        head.localRotation = Quaternion.identity;
        head.Rotate(Vector3.left, currentHeadRotation);

    }
}

在这里判断角色落到地面之后,才允许向上跳动。到这里,就实现角色碰到地面,以及解决空中跳动的问题。

C++标准模板库从入门到精通

http://edu.csdn.net/course/detail/3324

跟老菜鸟学C++

http://edu.csdn.net/course/detail/2901

跟老菜鸟学python

http://edu.csdn.net/course/detail/2592

以上所述就是小编给大家介绍的《游戏制作之路(20)角色跳动之后怎么样检测接触地面》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

秩序之美

秩序之美

Vinh / 人民邮电 / 2011-5 / 35.00元

怎样才能设计出简洁大方而不落于俗套的超人气网站?纽约时报网站的资深设计师Khoi Vinh在这《秩序之美——网页中的网格设计》一书中将为你揭示其中的奥秘。   《秩序之美——网页中的网格设计》将源自传统平面设计、被众多平面设计大师推崇的网格设计方法应用于网页设计,向读者详细介绍了网格设计成熟而经典的设计模式,并以整个网站的设计为例,对工作流程、设计工具和方法进行了系统而全面的介绍,手把手教读......一起来看看 《秩序之美》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具