I have a character controller to move my player on a surface. All the things work fine, but I can't make a perfect slide with the character controller on the surface.
When I try to slide, my player animation plays in the center of capsule collider, which is attached to the player. That's why my player can not touch floor, which means my animation play in mid air. After some time I found that my problem occurs due to using the character controller. I am using Character.Move()
to move my player.
Can anyone help me? How can I avoid the collider on the character controller, or change something to make my player slide animation perfectly align with the floor.?
I am using controller.transform.Rotate(0,0,60)
to rotate my player on floor.
This is my script:
using UnityEngine;
using Assets.Scripts;
using System.Collections;
using UnityEngine.SceneManagement;
public class ControllerMovement : MonoBehaviour {
private Vector3 moveDirection = Vector3.zero;
public float gravity = 20f;
private CharacterController controller;
public float JumpSpeed = 8.0f;
public float Speed = 0.1f;
public int swipeStep = 3;
int swipeD,pos;
bool rx ;
int i=0;
public Transform CharacterGO;
bool isInSwipeArea;
private Animator anim;
IInputDetector inputDetector = null;
// Use this for initialization
void Start()
{
moveDirection = transform.forward;
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= Speed;
UIManager.Instance.ResetScore();
UIManager.Instance.SetStatus(Constants.StatusTapToStart);
GameManager.Instance.GameState = GameState.Start;
anim = CharacterGO.GetComponentInChildren<Animator> ();
inputDetector = GetComponent<IInputDetector>();
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
switch (GameManager.Instance.GameState)
{
case GameState.Start:
if (Input.GetMouseButtonUp(0))
{
//anim.SetBool(Constants.animaationStarted, true);
var instance = GameManager.Instance;
instance.GameState = GameState.Playing;
UIManager.Instance.SetStatus(string.Empty);
}
break;
case GameState.Playing:
UIManager.Instance.IncreaseScore (0.001f);
CheckHeight ();
DetectJumpOrSwipeLeftRight ();
//apply gravity
moveDirection.y -= gravity * Time.deltaTime;
//move the player
controller.Move (moveDirection * Time.deltaTime);
break;
case GameState.Dead:
////anima.SetBool(Constants.//animaationStarted, false);
if (Input.GetMouseButtonUp(0))
{ SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
break;
default:
break;
}
}
private void CheckHeight()
{
if (transform.position.y < -10)
{
GameManager.Instance.Die();
}
}
private void DetectJumpOrSwipeLeftRight()
{
var inputDirection = inputDetector.DetectInputDirection();
if (Input.GetKeyDown (KeyCode.Space)) {
moveDirection.y = JumpSpeed;
anim.SetBool ("RunToJump", true);
} else {
anim.SetBool ("RunToJump", false);
}
if(Input.GetKey(KeyCode.A) && controller.isGrounded){
controller.transform.Translate (-0.1f,0,0);
}
if(Input.GetKey(KeyCode.D) && controller.isGrounded){
controller.transform.Translate (0.1f,0,0);
}
if (Input.GetKey (KeyCode.S) && controller.isGrounded) {
anim.SetBool ("RunToSlip", true);
controller.transform.Rotate (-40, 0, 0);
Invoke ("StopSlidding", 0.2f);
}
}
void StopSlidding(){
anim.SetBool ("RunToSlip", false);
controller.transform.Rotate (40,0,0);
}
}
anim = GetComponentInChildren<Animator> ();
if the character object is as child. This has noting to do with parent rotation. You should usually rotate parent object not trying withChracterController
. \$\endgroup\$CharacterController
component. You rotate the parent transform usually. There is movement control for character controller. \$\endgroup\$