player glitching through floor when turning
so i found that my player goes forward okay but when i try and turn he starts to glitch and then eventually falls through the terrain i have tried re-importing the camera and disabling the components on the player one by one but it didn't seem to help here is a video of whats happening https://streamable.com/jfn63
here is all my code please tell me if you require more info
player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Player : MonoBehaviour {
public Slider HealthBar;
public float CurrentHealth { get; set; }
public float MaxHealth { get; set; }
public float Speed = 6.0f;
public float jumpSpeed = 8.0f;
public float rotateSpeed = 5.0f;
float gravity = 9.8f;
Vector3 moveDirection = Vector3.zero;
Animator anim;
CharacterController cc;
static int idleState = Animator.StringToHash("Base Layer.Idle");
static int moveState = Animator.StringToHash("Base Layer.Walk");
AnimatorStateInfo currentBaseState;
// Use this for initialization
void Start () {
MaxHealth = 20f;
//resets players health on load
CurrentHealth = MaxHealth;
HealthBar.value = CalculateHealth();
anim = GetComponent<Animator>();
cc = GetComponent<CharacterController>();
if (!anim)
{
Debug.Log("No Animator Found.");
}
if (!cc)
{
Debug.Log("No CharacterController Found.");
}
}
// Update is called once per frame
void Update () {
anim.speed = Speed;
//Debug.Log(tag);
//Debug.Log(Speed);
if (cc.isGrounded)
{
float moveForward = Input.GetAxis("Vertical");
moveDirection = new Vector3(0, 0, moveForward);
transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= Speed;
if (Input.GetButton("Jump"))
{
moveDirection.y = jumpSpeed;
anim.SetTrigger("Jump");
}
anim.SetFloat("Speed", moveForward);
}
moveDirection.y -= gravity * Time.deltaTime;
cc.Move(moveDirection * Time.deltaTime);
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
//if (currentBaseState.fullPathHash == moveState)
//{
// if (Input.GetButtonDown("Fire1"))
// {
// anim.SetTrigger("Punch");
// }
//}
//if (currentBaseState.fullPathHash == idleState)
//{
// if (Input.GetButtonDown("Fire1"))
// {
// anim.SetTrigger("Punch");
// // Instantiate Something
// }
//}
if (Input.GetButtonDown("Jump"))
{
anim.SetTrigger("Jump");
}
while(Input.GetKeyDown(KeyCode.LeftShift))
{
Speed = 10;
}
if (Input.GetKeyDown(KeyCode.E))
{
anim.SetTrigger("Dead");
}
}
void OnCollisionEnter(Collision collision)
{
Debug.Log(name);
if (collision.gameObject.name == "Finish")
{
Debug.Log("you finished the game");
SceneManager.LoadScene(1, LoadSceneMode.Additive);
}
if(collision.gameObject.name == "Lava")
{
Speed = 5;
DealDamage(2);
}
while (collision.gameObject.tag == "Ice")
{
Speed = 20;
}
while (collision.gameObject.tag == "Water")
{
Speed = 2;
}
}
void DealDamage(float DamageValue)
{
CurrentHealth -= DamageValue;
HealthBar.value = CalculateHealth();
if(CurrentHealth == 0)
{
Die();
}
}
float CalculateHealth()
{
return CurrentHealth / MaxHealth;
}
void Die()
{
CurrentHealth = 0;
anim.SetTrigger("Dead");
}
}
GameManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
static GameManager _instance;
// Use this for initialization
void Start () {
if (instance)
DestroyImmediate(gameObject);
else
{
DontDestroyOnLoad(gameObject);
instance = this;
}
//SceneManager.LoadScene(0, LoadSceneMode.Additive);
}
public static GameManager instance
{
get { return _instance; }
set { _instance = value; }
}
// Update is called once per frame
void Update () {
}
public void StartGame()
{
SceneManager.LoadScene("main");
}
public void Quit()
{
Application.Quit();
Debug.Log("This is where i Would Quit");
}
public void LoadNewScene(string Scene)
{
SceneManager.LoadScene(Scene);
}
public void MainMenu()
{
SceneManager.LoadScene(0);
}
}
Answer by Cornelis-de-Jager · Nov 19, 2018 at 09:08 PM
I would disable gravity when touching the ground. =
if (cc.isGrounded)
{
// Do things
} else {
// Move it move downward
moveDirection.y -= gravity * Time.deltaTime;
}
i added the else statement and it still seems to have the same problem
Your answer
Follow this Question
Related Questions
How do I make a surface move a player forward? 1 Answer
I'm having issues with collision and Physics 1 Answer
Player moves indefinitely after collision 0 Answers
How to make my player move forward when on an object 1 Answer
How can you make a level fly in and fall out as the player progresses? 0 Answers