Hallo, ich habe ein problem, und zwar möchte ich das mein 3d Charakter auf welchen man von schräg hinten schaut springt, wenn man Leertaste drückt, dies macht er auch, jedoch nachdem man einmal Leertaste gedrückt hat hört er nicht mahr auf zu springen. Vielen Dank für die hilfe schon mal im Vorraus!
Code(des Charakters):
using UnityEngine;
using System.Collections;
public class NewBehaviouerScript1 : MonoBehaviour
{
private const float gravityMultiplier = 2;
public float walkingSpeed = 5;
public float jumpingSpeed = 10;
private bool shouldJump;
private bool previouslyGrounded;
private bool hasJumped;
private Camera targetCamera;
private Vector2 inputVector;
private Vector3 movingDirection = Vector3.zero;
private CollisionFlags collisionFlags;
private float timeSinceNotGrounded = 0;
private void Awake()
{
Application.targetFrameRate = 60;
targetCamera = GetComponentInChildren<Camera>();
}
private void Update()
{
if (shouldJump && IsGrounded() && !hasJumped)
{
hasJumped = true;
shouldJump = false;
movingDirection.y = jumpingSpeed;
}
if (Input.GetKeyDown(KeyCode.Space))
{
shouldJump = true;
}
if (!previouslyGrounded && IsGrounded())
{
previouslyGrounded = true;
timeSinceNotGrounded = 0;
hasJumped = false;
}
if (previouslyGrounded && (!IsGrounded()))
{
timeSinceNotGrounded += Time.deltaTime;
if (timeSinceNotGrounded > 0.02f)
{
previouslyGrounded = false;
}
}
}
private void FixedUpdate()
{
ProcessInput();
Vector3 desiredMove = Vector3.zero;
if (inputVector.y > 0) // move forward
{
desiredMove = -transform.forward * inputVector.y;
}
else if (inputVector.y < 0) // move backward
{
desiredMove = -transform.forward * inputVector.y;
}
if (inputVector.x > 0) // rotate right
{
transform.Rotate(Vector3.up, 90 * Time.fixedDeltaTime);
}
else if (inputVector.x < 0) // rotate left
{
transform.Rotate(Vector3.up, -90 * Time.fixedDeltaTime);
}
RaycastHit hitInfo;
if (Physics.Raycast(transform.position, Vector3.down, out hitInfo, 1.01f))
{
desiredMove = Vector3.ProjectOnPlane(desiredMove, hitInfo.normal).normalized;
}
movingDirection.x = desiredMove.x * walkingSpeed;
movingDirection.z = desiredMove.z * walkingSpeed;
if (!IsGrounded())
{
movingDirection += Physics.gravity * gravityMultiplier * Time.fixedDeltaTime;
}
transform.position += movingDirection * Time.fixedDeltaTime;
}
private void ProcessInput()
{
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
inputVector = new Vector2(horizontal, vertical);
if (inputVector.sqrMagnitude > 1)
{
inputVector.Normalize();
}
}
private bool IsGrounded()
{
return Physics.Raycast(transform.position, Vector3.down, 1.01f);
}
}