在C#中混淆向量(Unity Engine)

贾斯普雷特

我是Unity中C#编程的新手,在z轴上移动时会遇到一些问题。问题是放开向上按钮时我会继续移动。但是,当我在x轴上移动时,这很好,因为放开按钮将停止播放器。代码如下:

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
public Vector3 motion = new Vector3();
private CharacterController controller;

private bool onGround;

private float xRot, yRot;
public static float X_ROTATION = 0;
public static float Y_ROTATION = 0;
private const float lookSpeed = 2.0f;

public void Start() {
    controller = GetComponent<CharacterController>();
}

public void FixedUpdate() {
    if(Screen.lockCursor) {
        Vector3 impulse = new Vector3();
        if(Input.GetKey(KeyCode.W)) impulse.z+=1;
        if(Input.GetKey(KeyCode.A)) impulse.x-=1;
        if(Input.GetKey(KeyCode.S)) impulse.z-=1;
        if(Input.GetKey(KeyCode.D)) impulse.x+=1;

        if(impulse.sqrMagnitude > 0) {
            motion += Quaternion.Euler(new Vector3(0, xRot, 0)) * impulse.normalized * 0.05f;
        }

        if(onGround) {
            if(Input.GetKey(KeyCode.Space)) {
                motion.y += 0.2f;
            }
        }

        motion.y -= 0.015f;
        Vector3 oldMotion = motion;
        Vector3 oldPos = controller.transform.localPosition;
        controller.Move(motion);
        Vector3 newPos = controller.transform.localPosition;
        motion = newPos - oldPos;
        onGround = oldMotion.y < -0.0001f && motion.y >= -0.0001f;
        motion.x *= 0.8f; 
        motion.y *= 0.8f;
    }
}

public void Update() {
    if(Screen.lockCursor && Input.GetKeyDown(KeyCode.Escape)) {
        Screen.lockCursor = false;  
    }

    if(!Screen.lockCursor && Input.GetMouseButtonDown(0)) {
        Screen.lockCursor = true;   
    }

    if(Screen.lockCursor) {
        xRot += Input.GetAxis("Mouse X") * lookSpeed;
        yRot -= Input.GetAxis("Mouse Y") * lookSpeed;

        if(yRot < -90) yRot = -90;
        if(yRot > 90) yRot = 90;
        if(xRot < -180) xRot += 360;
        if(xRot >= 180) xRot -= 360;

        controller.transform.localRotation = Quaternion.Euler(new Vector3(yRot, xRot, 0));
    }

    Player.X_ROTATION = xRot;
}

}

技艺

在FixedUpdate()中,您对以下代码进行了以下编码:

motion.x *= 0.8f;
motion.y *= 0.8f;

还应该包含

motion.z *= 0.8f;

也许您不需要-

motion.y *= 0.8f;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google App Engine中的交易隔离

来自分类Dev

在Java App Engine中连接到PostgreSQL

来自分类Dev

Google App Engine + Python中的REST API?

来自分类Dev

Google App Engine NDB中的层次结构

来自分类Dev

App Engine中的UserService,OAuth和AJAX

来自分类Dev

从Google Compute Engine实例中删除用户

来自分类Dev

Google App Engine中的Xlsxwriter错误

来自分类Dev

Google App Engine中的XMPP支持

来自分类Dev

在Google Container Engine中自动缩放

来自分类Dev

增加Google Container Engine中的群集大小

来自分类Dev

Cloud Console中缺少App Engine

来自分类Dev

如何使用Unity Engine确定设备是手机还是平板电脑,并且可以使用C#完成?

来自分类Dev

ABCPDF中的Gecko Engine找不到标签

来自分类Dev

App Engine在Go中读取本地文件

来自分类Dev

在Google Compute Engine中按标签计费

来自分类Dev

Google App Engine中的网址大小

来自分类Dev

Google Container Engine中的MaxGCEPDVolumeCount

来自分类Dev

Google App Engine中包含的Javamail版本

来自分类Dev

App Engine Google Cloud中的部署失败

来自分类Dev

在Google App Engine中全局运行MongoDB

来自分类Dev

Google Governors Engine中的垂直自动缩放?

来自分类Dev

在Google Compute Engine中创建登台VM

来自分类Dev

在Unity Engine中的C#脚本中的CS1002和CS0116中显示错误

来自分类Dev

在Google App Engine中启动App Engine模块

来自分类Dev

Google App Engine中的RSA

来自分类Dev

在Eclipse中清理Google App Engine项目

来自分类Dev

如何使AI仅移向场景中活动的对象(Unity Engine)

来自分类Dev

如何使用Unity Engine确定设备是手机还是平板电脑,并且可以使用C#完成?

来自分类Dev

Google Container Engine中的MaxGCEPDVolumeCount

Related 相关文章

热门标签

归档