Question by
gjproducer · Apr 19 at 01:30 PM ·
scripting problem
Jump is inconsistently very high
Hello All,
I am working on an FPS character jump. For some reason one of the many jumps sends the character very high. Sometimes it is the first one, and sometimes it is one after I successfully jump and move around a few times. I am still very much a noob so any help is appreciated!
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float speed = 12.5f;
// adding gravity
public Vector3 velocity;
public float gravityModifier;
public CharacterController myController;
public Transform myCameraHead;
public float mouseSensitivity = 100f;
private float cameraVerticalRotation;
public GameObject bullet;
public Transform firePosition;
public GameObject muzzleFlash, bulletHole;
// jumping
public float jumpHeight = 10f;
private bool readyToJump;
public Transform ground;
public LayerMask groundLayer;
public float groundDistance = 0.5f;
void Update()
{
PlayerMovement();
CameraMovement();
Jump();
Shoot();
}
void Jump()
{
readyToJump = Physics.OverlapSphere(ground.position, groundDistance, groundLayer).Length > 0;
if(Input.GetButtonDown("Jump") && readyToJump)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * Physics.gravity.y) * Time.deltaTime;
}
myController.Move(velocity);
}
private void Shoot()
{
if(Input.GetMouseButtonDown(0))
{
RaycastHit hit;
if(Physics.Raycast(myCameraHead.position, myCameraHead.forward, out hit, 100f))
{
if(Vector3.Distance(myCameraHead.position, hit.point) > 2f)
{
firePosition.LookAt(hit.point);
}
if(hit.collider.CompareTag("Enemy"))
Destroy(hit.collider.gameObject);
}
else
{
firePosition.LookAt(myCameraHead.position + (myCameraHead.forward * 50f));
}
Instantiate(muzzleFlash, firePosition.position, firePosition.rotation, firePosition);
Instantiate(bullet, firePosition.position, firePosition.rotation);
}
}
private void CameraMovement()
{
float mouseX = Input.GetAxisRaw("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxisRaw("Mouse Y") * mouseSensitivity * Time.deltaTime;
cameraVerticalRotation -= mouseY;
cameraVerticalRotation = Mathf.Clamp(cameraVerticalRotation, -90f, 90f);
transform.Rotate(Vector3.up * mouseX);
myCameraHead.localRotation = Quaternion.Euler(cameraVerticalRotation, 0f, 0f);
}
void PlayerMovement()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 movement = x * transform.right + z * transform.forward;
movement = movement * speed * Time.deltaTime;
myController.Move(movement);
velocity.y += Physics.gravity.y * Mathf.Pow(Time.deltaTime, 2) * gravityModifier;
if(myController.isGrounded)
velocity.y = Physics.gravity.y * Time.deltaTime;
myController.Move(velocity);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613055541im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why are my instantiated projectiles not firing? 0 Answers
New problem. 1 Answer
Unable to add script component to GameObject. 0 Answers
Movement Flipped in Game View? 0 Answers
calling a value between 2 scripts 0 Answers