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

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

内容简介:在前面实现了角色进行跳动,可以在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)角色跳动之后怎么样检测接触地面》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Bulletproof Web Design

Bulletproof Web Design

Dan Cederholm / New Riders Press / 28 July, 2005 / $39.99

No matter how visually appealing or packed with content a Web site is, it isn't succeeding if it's not reaching the widest possible audience. Designers who get this guide can be assured their Web site......一起来看看 《Bulletproof Web Design》 这本书的介绍吧!

html转js在线工具
html转js在线工具

html转js在线工具

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

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具