首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运动很奇怪

运动很奇怪
EN

Stack Overflow用户
提问于 2022-03-17 12:17:44
回答 1查看 51关注 0票数 0

我是跟随韦尔顿国王的教程如何制作fps,我是在第三个视频,我写了这个。

代码语言:javascript
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Com.Pat12.FPS
{
    public class Motion : MonoBehaviour
    {
        public float speed;
        public float sprintModifier;
        public float jumpForce;
        public Camera normalCam;
        public Transform groundDetector;
        public LayerMask ground;
        private Rigidbody rig;
        private float baseFOV;
        private float sprintFOVModifier = 1.5f;
     
        private void Start()
        {
            baseFOV = normalCam.fieldOfView;
            Camera.main.enabled = false;
            rig = GetComponent<Rigidbody>();
        }

        void FixedUpdate()
        {
            //Axis
            float t_hmove = Input.GetAxisRaw("Horizontal");
            float t_vmove = Input.GetAxisRaw("Vertical");
            //Controls
            bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
            bool jump = Input.GetKeyDown(KeyCode.Space);
            //States
            bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 0.1f, ground);
            bool isJumping = jump && isGrounded;
            bool isSprinting = sprint && t_vmove > 0 && !isJumping && isGrounded;
            //Jumping
            if (isJumping)
            {
                rig.AddForce(Vector3.up * jumpForce);
            }
            //Movement
            Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove);
            t_direction.Normalize();
            float t_adjustedSpeed = speed;
            if (isSprinting) t_adjustedSpeed *= sprintModifier;
            Vector3 t_targetVelocity = t_direction * t_adjustedSpeed * Time.deltaTime;
            t_targetVelocity.y = rig.velocity.y;
            rig.velocity = t_targetVelocity;
            //FOV
            if (isSprinting) 
            { 
                normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV * sprintFOVModifier, Time.deltaTime * 8f); 
            }
            else 
            { 
                normalCam.fieldOfView = Mathf.Lerp(normalCam.fieldOfView, baseFOV, Time.deltaTime * 8f); 
            }
        }
    }
}

问题是当我把我的球员转过来的时候,w键是不会前进的。它只是按地面移动,而不管它的脸如何。

EN

回答 1

Stack Overflow用户

发布于 2022-03-17 13:46:29

修好了。你必须让它成为transform.TransformDirection(t_direction )

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71512167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档