1
我試圖添加跳到我們的控制腳本的控制,但它只是不工作,我有點沮喪,因爲我試了很多,使其工作。我不使用剛體,而是想使用基於腳本的物理和 - 但後來 - 射線廣播(檢測地面)。它是3D的,但具有固定的角度,因爲角色是精靈(我們正在考慮嘗試類似於不餓,紙馬里奧等)的風格。而我現在要放入的東西是當角色靜止不動,然後跳躍時跳躍向上,當角色移動時角色也會移動一段距離。那麼,你得到的照片。爲此,我首先需要跳躍式的工作 - 也有重力讓角色倒退,同時也考慮到角色可能會落在與起始場地高度不同的地面上。 現在發生的情況是,角色跳躍只是一點點,就像跳躍一樣,然後無休止地跌倒 - 或者在再次按下跳躍時無限上升。那麼,我該如何解決這個問題?跳躍沒有剛體統一
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Controls3D : MonoBehaviour
{
public float player3DSpeed = 5f;
private bool IsGrounded = true;
private const float groundY = (float) 0.4;
private Vector3 Velocity = Vector3.zero;
public Vector3 gravity = new Vector3(0, -10, 0);
void Start()
{
}
void Update()
{
if (IsGrounded)
{
if (Input.GetButtonDown("Jump"))
{
IsGrounded = false;
Velocity = new Vector3(Input.GetAxis("Horizontal"), 20, Input.GetAxis("Vertical"));
}
Velocity.x = Input.GetAxis("Horizontal");
Velocity.z = Input.GetAxis("Vertical");
if (Velocity.sqrMagnitude > 1)
Velocity.Normalize();
transform.position += Velocity * player3DSpeed * Time.deltaTime;
}
else
{
Velocity += gravity * Time.deltaTime;
Vector3 position = transform.position + Velocity * Time.deltaTime;
if (position.y < groundY)
{
position.y = groundY;
IsGrounded = true;
}
transform.position = position;
}
}
}
無盡的倒下問題解決了。只需要設置Velocity.y = 0;在transform.position = position之後;在IsGrounded = true下; – Gota
如果你經過我打賭,你會看到初始速度比你想象的要小,因爲'Velocity.Normalize();',所以你的Velocity.y是-1和1之間的某個數。我認爲你的向上速度應該與其他兩個方向無關,或者,您應該每次修改速度時都要將速度標準化,而不是僅在接地時進行。也就是說,爲什麼在停滯的情況下正常化,而不是在這裏:'Velocity + = gravity * Time.deltaTime;'? – Quantic
我想我現在得到了一些東西,謝謝。 – Gota