- Home /
Question by
dominoslex3 · Jun 12, 2017 at 07:31 PM ·
scripting problemcharactercontrollerscripting beginnercharacter movementendless runner
The character controller stop moving when the Speed Start increase Endless runner
this is the class for player movement
using UnityEngine; using System.Collections;
public class PlayerMotor : MonoBehaviour {
private CharacterController controller;
private Vector3 moveVectore;
private float animationDuration = 0.2f;
public float speed = 0.01f;
private float verticalVelocity = 0.0f;
public float gravity = 12.0f;
public float jumpForce = 20.0f;
public float speedH = 20f;
private Vector3 left = new Vector3(-4, 0, 0);
private Vector3 right = new Vector3(4, 0, 0);
private bool isDeath = false;
void Start()
{
controller = GetComponent<CharacterController>();
}
void Update()
{
if (isDeath)
return;
if (Time.time < animationDuration)
{
controller.Move(Vector3.forward * speed * Time.deltaTime);
return;
}
if (controller.isGrounded)
{
verticalVelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
verticalVelocity = jumpForce;
}
}
else
{
verticalVelocity -= gravity * Time.deltaTime;
}
Vector3 moveVector = Vector3.zero;
//X
if (Input.GetKeyDown("left"))
{
controller.Move(left);
}
if (Input.GetKeyDown("right"))
{
controller.Move(right);
}
//Y
moveVector.y = verticalVelocity;
//Z
// moveVector.z = Input.GetAxis("Vertical") * 5.0f;
controller.Move(Vector3.forward * speed);
controller.Move(moveVector * Time.deltaTime); // here when i delete this function the character controller is continue the moving , but the jump don't working
}
public void SetSpeed(float modifier)
{
speed = 0.5f + modifier;
}
private void OnControllerColliderHit(ControllerColliderHit hit)
{
if (hit.point.z > transform.position.z + controller.radius)
Death();
}
private void Death()
{
isDeath = true;
GetComponent<Score>().OnDeath();
}
}
and this class for Score
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Score : MonoBehaviour {
private float score = 0.0f;
public Text scoreText;
public float difficultyLevel = 1f;
private float maxDifficultyLevel = 2;
private float scoreToNextLevel = 2;
private bool isDead = false;
// Update is called once per frame
void Update ()
{
if (isDead)
return;
if (score >= scoreToNextLevel)
LevelUp();
score += Time.deltaTime;
scoreText.text = ((int)score).ToString();
}
void LevelUp()
{
if (difficultyLevel == maxDifficultyLevel)
{
return;
}
// scoreToNextLevel *= 2f;
difficultyLevel+=0.1f;
GetComponent<PlayerMotor>().SetSpeed(difficultyLevel);
}
public void OnDeath()
{
isDead = true;
}
}
Comment