- Home /
Question by
antonsimmerle · Mar 30 at 07:55 PM ·
movementplayer movementacceleration
Add acceleration to movement
Hey guys, I want to have a customizable acceleration, but i really don't know how i can do that.
Hope someone knows how I can do that
Here's the movement script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MenschMovement : MonoBehaviour { //Variables public CharacterController controller; public float gravity = -9.81f; public Transform groundCheck; public float groundDistance = 0.4f; public LayerMask groundMask; public float jumpHeight = 3f; public float speed;
Vector3 velocity; bool isGrounded; private bool canDoubleJump;
void Update() {
//Gravity & GroundCheck
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if(isGrounded && velocity.y < 0)
{
velocity.y = -4f;
}
//Movement
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
velocity.y += gravity * Time.deltaTime;
controller.Move(move * speed * Time.deltaTime);
controller.Move(velocity * Time.deltaTime);
//Jump / DoubleJump
if(isGrounded)
{
canDoubleJump = true;
}
if(Input.GetButtonDown("Jump"))
{
if (isGrounded) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
} else {
if (canDoubleJump == true) {
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
canDoubleJump = false;
}
}
}
} }
Comment
Your answer
Follow this Question
Related Questions
Acceleration with CC 0 Answers
How to make an object move to another object's location in a 2d game? 2 Answers
Player movement 1 Answer