Floats are acting really weird
I've been having trouble getting the floats that I assign to my movement speed variables to result in any consistent impact on my character...
I'm not sure what's going on, the gravity that I implemented works but it falls REALLY slowly unless I multiple its speed by about 100. It goes between
falling really slowly at moveDirection.y -= gravity *60
to clipping down in chunks before falling the rest of the way slowly at gravity *70 or 80
and then by the time it's at gravity *100 it just clips straight down to the ground...
I had previously just allowed it to be at that speed where it clips so that the character would at least remain grounded (since there aren't really any drops in the scene I'm working on, I just was having trouble with .isGrounded before so I had the character raised for testing purposes) but it's making it basically impossible to implement a working jump.
I'll include the code I'm working on; there are some things in there that I haven't finished yet that are commented out just because I'm eventually going to reimport the character model once I fix its animations (add falling animation and update run cycle) and then I also left in an alternate jump I was messing with which is also commented out.
I'd appreciate any suggestions about any part of it that anyone might have
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour
{
private GameObject thePlayer;
private Animator anim;
private CharacterController controller;
Transform myTransform;
Vector3 moveDirection = Vector3.zero;
PlayerEnergy playerEnergy;
public CollisionFlags _collisionFlags;
public float gravity = 60.0f;
public float speed = 3.0f;
public float turnSpeed = 80.0f;
public float jumpHeight = 60;
public float jumpLength = 1.5f;
public float fallAfter = 0.5f;
private float _timeToGo1;
private float _timeToGo2;
public bool playerStop;
public bool playerStill;
public bool jumping;
public bool lyingDown;
private bool _hasBeenLaying;
public bool playerResting;
public float airTime = 0;
void Start()
{
Debug.Log("Initialized");
//search for player clones and remove them
GameObject[] remaining = GameObject.FindGameObjectsWithTag("GoblinPlayer");
foreach (GameObject clone in remaining)
{
if (clone.name == "GoblinCharacter(Clone)")
{
GameObject.Destroy(clone);
}
}
thePlayer = GameObject.FindGameObjectWithTag("GoblinPlayer");
anim = thePlayer.GetComponent<Animator>();
controller = thePlayer.GetComponent<CharacterController>();
myTransform = thePlayer.transform;
playerEnergy = thePlayer.GetComponent<PlayerEnergy>();
_timeToGo1 = Time.fixedTime + 5.0f;
_timeToGo2 = Time.fixedTime + 30.0f;
playerStop = true;
playerStill = true;
lyingDown = false;
_hasBeenLaying = false;
playerResting = false;
jumping = false;
}
IEnumerator WaitOneSecond()
{
yield return new WaitForSeconds(1);
}
//restore 3% of energy up to 100
void RestingEnergy()
{
playerEnergy.curEnergy = Mathf.Clamp(playerEnergy.curEnergy + 3.0f, 0.0f, (playerEnergy.maxEnergy));
}
void FixedUpdate()
{
//update once every 5 seconds
if (Time.fixedTime >= _timeToGo1)
{
Debug.Log("W is pressed: " + Input.GetKey(KeyCode.W));
Debug.Log("moveDirection is " + moveDirection);
Debug.Log("playerEnergy is " + playerEnergy.curEnergy);
Debug.Log("playerStill is " + playerStill);
Debug.Log("lyingDown is " + lyingDown);
Debug.Log("playerResting is " + playerResting);
Debug.Log("jumping is " + jumping);
//restore energy if the player is resting
if (playerResting == true)
{
RestingEnergy();
}
_timeToGo1 = Time.fixedTime + 5.0f;
}
}
void Update()
{
moveDirection = myTransform.forward * Input.GetAxis("Vertical") * speed;
float turn = Input.GetAxis("Horizontal");
if (controller.isGrounded)
{
//Debug.Log("we're on the ground");
airTime = 0;
anim.SetBool("DoesJump", false);
if (Input.GetKeyDown(KeyCode.Space) && lyingDown == false)
{
if (airTime < jumpLength)
{
moveDirection.y = (moveDirection.y - gravity) + jumpHeight;
anim.SetBool("DoesJump", true);
}
}
/*
if (jumping == false && lyingDown == false && Input.GetKeyDown(KeyCode.Space))
{
anim.SetBool("DoesJump", true);
moveDirection.y += jumpHeight;
jumping = true;
WaitOneSecond();
anim.SetBool("DoesJump", false);
moveDirection.y -= jumpHeight;
jumping = false;
}
*/
//anim.SetBool("IsFalling", false);
if (lyingDown == false && jumping == false)
{
if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.LeftShift))
{
speed = 20.0f;
anim.SetInteger("MotionTrigger", 2);
playerEnergy.energyDrainSpeed = 40;
playerStop = false;
playerStill = false;
playerResting = false;
}
else if (Input.GetKey(KeyCode.W) && !Input.GetKey(KeyCode.LeftShift) && !Input.GetKey(KeyCode.S))
{
speed = 7.0f;
anim.SetInteger("MotionTrigger", 1);
playerEnergy.energyDrainSpeed = 8;
playerStop = false;
playerStill = false;
playerResting = false;
}
else if (Input.GetKey(KeyCode.S) && !Input.GetKey(KeyCode.W))
{
speed = 5.0f;
anim.SetInteger("MotionTrigger", 4);
playerEnergy.energyDrainSpeed = 7;
playerStop = false;
playerStill = false;
playerResting = false;
}
else
{
anim.SetInteger("MotionTrigger", 0);
playerEnergy.energyDrainSpeed = 0;
playerStop = true;
}
}
//lay down when the player presses the R key and is not moving
if (playerStop == true)
{
if (lyingDown == false && Input.GetKeyDown(KeyCode.R))
{
anim.SetBool("LayingDown", true);
lyingDown = true;
}
else if (lyingDown == true && Input.GetKeyDown(KeyCode.R))
{
anim.SetBool("LayingDown", false);
lyingDown = false;
}
}
//check for lyingDown once every 30 seconds
if (Time.fixedTime >= _timeToGo2)
{
_timeToGo2 = Time.fixedTime + 30.0f;
if (lyingDown == true)
playerStill = true;
}
//has the player been lying down for 30 seconds?
if (playerStill == true && lyingDown == true)
playerResting = true;
} else
{
airTime += Time.deltaTime;
/*
if (airTime >= fallAfter)
{
anim.SetBool("IsFalling", true);
}*/
}
moveDirection.y -= gravity * Time.deltaTime;
if (lyingDown == false)
{
_collisionFlags = controller.Move(moveDirection * Time.deltaTime);
transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
}
}
}
Your answer
Follow this Question
Related Questions
Uneven Jump Heights 1 Answer
Problem with jumping 1 Answer
How to throw an object while swiping and after 0 Answers
How can I prevent my player from flying when i use the space bar? 0 Answers