- Home /
Cant Jump ?c# please
so i am making a multiplayer game in photon and when i try and jump i just stay on the ground.
here is my code
using UnityEngine;
using System.Collections;
public class ProjectJumpCharacterController : MonoBehaviour {
//booleans
public bool issprinting = false;
public bool ShootingEnabled = false;
public bool SprintingEnabled = false;
public bool StaminaEnabled = false;
//floats
//b
public float bulletImpulse = 100f;
//g
public float gravity = 20;
//j
public float jumpHeight = 12;
//L
public float lowgravity = 5;
//m
public float mouseSensitivity = 6.0f;
public float movespeed = 100.0f;
//n
public float normalspeed = 250.0f;
//s
public float sprintspeed = 500.0f;
//u
public float upDownRange = 60.0f;
//v
float verticalRotation = 0;
//KeyCodes
public KeyCode sprint = KeyCode.LeftShift;
//GameObjects
public GameObject bullet_prefab;
public GameObject shootfrom;
//Vectors
private Vector3 amountToMove;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Low-Gravity")
{
gravity = lowgravity * Time.deltaTime;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag == "Low-Gravity")
{
gravity = 25 * Time.deltaTime;
}
}
void Update()
{
//Walking
float Fspeed = Input.GetAxis("Vertical") * movespeed * Time.deltaTime;
float Sspeed = Input.GetAxis("Horizontal") * movespeed * Time.deltaTime;
//Declaring our Character Controller and assigning it a name
CharacterController cc = transform.GetComponent<CharacterController> ();
//Rotation
//Rotating the camera
float rotLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
transform.Rotate (0, rotLeftRight, 0);
verticalRotation -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
verticalRotation = Mathf.Clamp (verticalRotation, -upDownRange, upDownRange);
Camera.main.transform.localRotation = Quaternion.Euler (verticalRotation, 0, 0);
//Sprinting
//If the player presses the sprint key it adjusts the move speed of the player
//if stamina is enabled it requires stamina to be > 0 and it depletes stamina when sprinting
if (Input.GetKey (sprint) && SprintingEnabled == true)
{
issprinting = true;
}
if (Input.GetKeyUp (sprint))
{
issprinting = false;
}
if (issprinting == true && SprintingEnabled == true)
{
movespeed = sprintspeed;
}
if (issprinting == false )
{
movespeed = normalspeed;
}
//Jumping
if (cc.isGrounded)
{
amountToMove.y = 0;
if (Input.GetButtonDown ("Jump"))
{
amountToMove.y = jumpHeight *Time.deltaTime;
}
}
if ((cc.collisionFlags & CollisionFlags.Above) != 0)
{
if(amountToMove.y > 0)
{
amountToMove.y = -gravity;
}
}
//getting the cc to move
amountToMove.x = Sspeed;
amountToMove.z = Fspeed;
amountToMove.y -= gravity;
amountToMove = transform.rotation * amountToMove;
cc.Move (amountToMove);
//Shooting
if (Input.GetButtonDown ("Fire1") && ShootingEnabled == true)
{
GameObject thebullet = (GameObject)PhotonNetwork.Instantiate ("bullet 1", shootfrom.transform.position, shootfrom.transform.rotation, 0);
thebullet.rigidbody.AddForce (shootfrom.transform.forward * bulletImpulse, ForceMode.Impulse);
}
}
if i remove time.deltatime on the jumping part i go really high really fast and then back down really fast it just pops up and down
i need to fix this fast! please help me
$$anonymous$$aybe you're missing Start() where you set gravity to 20 * Time.deltaTime. That only seem to happen once you trigger.
k i did it
You know what they say: If you want a job done right, do it yourself -_-
Answer by siaran · Feb 20, 2015 at 01:36 PM
I think the problem is here:
//getting the cc to move
amountToMove.x = Sspeed;
amountToMove.z = Fspeed;
amountToMove.y -= gravity;
amountToMove = transform.rotation * amountToMove;
cc.Move (amountToMove);
You set the amountToMove.y to -gravity just before you call move, regardless of if the player has pressed Jump.
Answer by hexagonius · Feb 20, 2015 at 01:53 PM
In line 107 your setting y to a small amount of Jumpheight. Remove * Time.deltaTime here.
In line 122 you're substracting the full gravity value. add * Time.deltaTime here.
but now the jumping just makes me pop up and down really fast