C# Unity3d兔子跳跃控制器。当我'时,我的速度降低;我扫射很快
我在unity3d中为兔子跳跃创建了一个FPS运动脚本,其中包含了来自的参考,但我有一些问题。当我快速扫射时,我的速度会降低。 我想创造一些像 这是我的代码:C# Unity3d兔子跳跃控制器。当我'时,我的速度降低;我扫射很快,c#,unity3d,controller,frame-rate,C#,Unity3d,Controller,Frame Rate,我在unity3d中为兔子跳跃创建了一个FPS运动脚本,其中包含了来自的参考,但我有一些问题。当我快速扫射时,我的速度会降低。 我想创造一些像 这是我的代码: using System.Collections; using System.Collections.Generic; using UnityEngine; public class Movement : MonoBehaviour { //Strafe Variables public float gravity =
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour {
//Strafe Variables
public float gravity = 3f;
public float ground_accelerate = 50f;
public float max_velocity_ground = 4f;
public float air_accelerate = 150f;
public float max_velocity_air = 2f;
public float friction = 8;
bool onGround;
public float jump_force = 5f;
private Vector3 lastFrameVelocity = Vector3.zero;
public Camera camObj;
Rigidbody rb;
Collider coll;
void Start () {
rb = GetComponent<Rigidbody> ();
coll = GetComponent<Collider> ();
}
void Update () {
Vector2 input = new Vector2 (Input.GetAxis ("Horizontal"), Input.GetAxis ("Vertical"));
Vector3 tempVelocity = CalculateFriction(rb.velocity);
tempVelocity += CalculateMovement (input, tempVelocity);
rb.velocity = tempVelocity;
lastFrameVelocity = rb.velocity;
rb.velocity += new Vector3(0f,-gravity,0f) * Time.deltaTime;
}
public Vector3 CalculateFriction(Vector3 currentVelocity)
{
onGround = Grounded();
float speed = currentVelocity.magnitude;
//Code from https://flafla2.github.io/2015/02/14/bunnyhop.html
if (!onGround || Input.GetButton("Jump") || speed == 0f)
return currentVelocity;
float drop = speed * friction * Time.deltaTime;
return currentVelocity * (Mathf.Max(speed - drop, 0f) / speed);
}
//Do movement input here
public Vector3 CalculateMovement(Vector2 input, Vector3 velocity)
{
onGround = Grounded();
//Different acceleration values for ground and air
float curAccel = ground_accelerate;
if (!onGround)
curAccel = air_accelerate;
//Ground speed
float curMaxSpeed = max_velocity_ground;
//Air speed
if (!onGround)
curMaxSpeed = max_velocity_air;
//Get rotation input and make it a vector
Vector3 camRotation = new Vector3(0f, camObj.transform.rotation.eulerAngles.y, 0f);
Vector3 inputVelocity = Quaternion.Euler(camRotation) *
new Vector3(input.x * air_accelerate, 0f, input.y * air_accelerate);
//Ignore vertical component of rotated input
Vector3 alignedInputVelocity = new Vector3(inputVelocity.x, 0f, inputVelocity.z) * Time.deltaTime;
//Get current velocity
Vector3 currentVelocity = new Vector3(velocity.x, 0f, velocity.z);
//How close the current speed to max velocity is (1 = not moving, 0 = at/over max speed)
float max = Mathf.Max(0f, 1 - (currentVelocity.magnitude / curMaxSpeed));
//How perpendicular the input to the current velocity is (0 = 90°)
float velocityDot = Vector3.Dot(currentVelocity, alignedInputVelocity);
//Scale the input to the max speed
Vector3 modifiedVelocity = alignedInputVelocity*max;
//The more perpendicular the input is, the more the input velocity will be applied
Vector3 correctVelocity = Vector3.Lerp(alignedInputVelocity, modifiedVelocity, velocityDot);
//Apply jump
correctVelocity += GetJumpVelocity(velocity.y);
//Return
return correctVelocity;
}
private Vector3 GetJumpVelocity(float yVelocity)
{
Vector3 jumpVelocity = Vector3.zero;
//Calculate jump
if ( Input.GetButton("Jump") && yVelocity < jump_force && Grounded())
{
jumpVelocity = new Vector3(0f, jump_force - yVelocity, 0f);
}
return jumpVelocity;
}
bool Grounded(){
return Physics.Raycast (transform.position, Vector3.down, coll.bounds.extents.y + 0.1f);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级运动:单一行为{
//扫射变量
公共浮子重力=3f;
公共浮地加速度=50f;
公共浮子最大速度地面=4f;
公共浮子空气加速=150f;
公共浮子最大流速空气=2f;
公共浮动摩擦=8;
布隆根;
公共浮子跳跃力=5f;
私有向量3 lastFrameVelocity=向量3.0;
公共摄像机;
刚体rb;
对撞机;
无效开始(){
rb=GetComponent();
coll=GetComponent();
}
无效更新(){
Vector2输入=新的Vector2(input.GetAxis(“水平”)、input.GetAxis(“垂直”);
Vector3 tempVelocity=计算折射率(rb.速度);
tempVelocity+=计算移动(输入,tempVelocity);
rb.速度=速度;
lastFrameVelocity=rb.velocity;
rb.速度+=新矢量3(0f,-重力,0f)*时间增量;
}
公共矢量3计算偏差(矢量3流速)
{
onGround=接地();
浮动速度=当前速度。幅值;
//代码来自https://flafla2.github.io/2015/02/14/bunnyhop.html
如果(!onGround | | Input.GetButton(“跳转”)| |速度==0f)
回流速度;
浮动落差=速度*摩擦力*时间.deltaTime;
返回电流速度*(数学最大值(速度下降,0f)/速度);
}
//在这里做运动输入
公共向量3计算移动(向量2输入,向量3速度)
{
onGround=接地();
//地面和空中的不同加速度值
浮球加速=地面加速;
如果(!onGround)
curAccel=空气加速;
//地面速度
float curMaxSpeed=最大速度地面;
//风速
如果(!onGround)
curMaxSpeed=最大速度空气;
//获取旋转输入并使其成为向量
Vector3 camRotation=新的Vector3(0f,camObj.transform.rotation.eulerAngles.y,0f);
矢量3输入速度=四元数.欧拉(凸轮旋转)*
新矢量3(输入.x*空气加速,0f,输入.y*空气加速);
//忽略旋转输入的垂直分量
Vector3 alignedInputVelocity=新的Vector3(inputVelocity.x,0f,inputVelocity.z)*Time.deltaTime;
//获得流速
Vector3 currentVelocity=新的Vector3(速度x,0f,速度z);
//当前速度与最大速度的接近程度(1=不移动,0=处于/超过最大速度)
浮点最大值=数学最大值(0f,1-(currentVelocity.magnitude/curMaxSpeed));
//输入与流速的垂直程度(0=90°)
float-velocityDot=Vector3.Dot(currentVelocity,alignedInputVelocity);
//将输入缩放到最大速度
Vector3 modifiedVelocity=对齐的计算速度*最大值;
//输入越垂直,应用的输入速度就越多
Vector3 correctVelocity=Vector3.Lerp(对齐的计算速度、修改的速度、速度点);
//应用跳转
correctVelocity+=GetJumpVelocity(速度y);
//返回
返回速度;
}
专用矢量3 GetJumpVelocity(浮动yVelocity)
{
Vector3跳跃速度=Vector3.0;
//计算跳跃
if(Input.GetButton(“Jump”)&&yVelocity