Question by
DinoMax0112 · Nov 30, 2020 at 02:17 PM ·
movement3dcharactercontrollerjumpplatformer
Double Jump using character controller
Hello, I am making a 2D/3D platformer. I had a working player script for 3D, so I made a CineMachine Camera. So I found some code online and added it to my script to make my player rotate with my camera and move in that direction. But now my double jump won't work. Thanks for any help you can give me,
Here is my code
using UnityEngine;
using System.Collections;
public class move3D : MonoBehaviour
{
public GameObject CameraRotate;
public Camera Camera;
// Moving fields
[SerializeField] // This will make the variable below appear in the inspector
float speed = 6;
float rotSpeed = 90.0f; // rotate at 90 degrees/second
[SerializeField]
float jumpSpeed = 8;
[SerializeField]
float gravity = 20;
Vector3 moveDirection = Vector3.zero;
CharacterController controller;
//bool isJumping; // "controller.isGrounded" can be used instead
[SerializeField]
int nrOfAlowedDJumps = 1; // New vairable
int dJumpCounter = 0; // New variable
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
var CharacterRotation = CameraRotate.transform.rotation;
CharacterRotation.x = 0;
CharacterRotation.z = 0;
transform.rotation = CharacterRotation;
transform.Rotate(0, Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime, 0);
moveDirection = Vector3.forward * Input.GetAxis("Vertical");
// convert direction from local to global space:
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButtonDown("Jump"))
{
if (controller.isGrounded)
{
moveDirection.y = jumpSpeed;
dJumpCounter = 0;
}
if (!controller.isGrounded && dJumpCounter < nrOfAlowedDJumps)
{
moveDirection.y = jumpSpeed;
dJumpCounter++;
}
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move(moveDirection * Time.deltaTime);
}
}
Mark
Comment