,How to make the charater rotate 180 with motion and play the idle in the good direction at the end of the motion
Hi,
I try to use a motion ( player fast walk return : https://www.mixamo.com/#/?page=1&query=turn+180&type=Motion%2CMotionPack ) and play the idle motion after.
But my issue is : When the motion "turn 180" is played, the player is still on the rotation (0,0 ,0) at the end, the idle motion is not played on the good direction after turn 180.
How can I make the direction at the end of the motion turn 180, match with the real direction of the player ?
The code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ControllerPerso : MonoBehaviour
{
public float speed = 4;
float rotSpeed = 100;
float rot = 0f;
float gravity = 8;
public bool Sand = false;
Vector3 moveDir = Vector3.zero;
CharacterController controller;
Animator anim;
void Start()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
void Update()
{
Movement();
GetInput();
}
void Movement()
{
if (controller.isGrounded)
{
if (Input.GetKey(KeyCode.Z) && Sand == false)
{
if (anim.GetBool("attacking") == true)
{
return;
}
else if (anim.GetBool("attacking") == false )
{
anim.SetBool("running", true);
anim.SetInteger("condition", 1);
moveDir = new Vector3(0, 0, 1);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
}
if (Input.GetKey(KeyCode.Z) && Sand == true)
{
if (anim.GetBool("attacking") == true)
{
return;
}
else if (anim.GetBool("attacking") == false )
{
anim.SetBool("running", false);
anim.SetInteger("condition", 3);
anim.SetBool("walksand", true);
anim.SetInteger("condition", 3);
moveDir = new Vector3(0, 0, 1);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
}
if (Input.GetKey(KeyCode.S) && Sand == false) //debut return
{
if (anim.GetBool("attacking") == true)
{
return;
}
else if (anim.GetBool("attacking") == false)
{
//anim.SetBool("running", true);
anim.SetInteger("condition", 4);
}
}
if (Input.GetKey(KeyCode.S) && Sand == true)
{
if (anim.GetBool("attacking") == true)
{
return;
}
else if (anim.GetBool("attacking") == false)
{
moveDir = new Vector3(0, 0, 0);
moveDir *= speed;
moveDir = transform.TransformDirection(moveDir);
}
}
if (Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.Z))
{
Debug.Log("debut release key");
anim.SetBool("running", false);
anim.SetBool("walksand", false);
anim.SetInteger("condition", 0);
moveDir = new Vector3(0, 0, 0);
Debug.Log("fin release key");
}
rot += Input.GetAxis("Horizontal") * rotSpeed * Time.deltaTime;
transform.eulerAngles = new Vector3(0, rot, 0);
moveDir.y -= gravity * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime);
}
}
void GetInput()
{
if (controller.isGrounded)
{
if (Input.GetMouseButtonDown(0))
{
if (anim.GetBool("running") == true)
{
anim.SetBool("running", false);
anim.SetInteger("condition", 0);
}
if (anim.GetBool("running") == false)
{
Attacking();
}
}
}
}
void Attacking()
{
StartCoroutine(AttackRoutine());
}
IEnumerator AttackRoutine()
{
anim.SetBool("attacking", true);
anim.SetInteger("condition", 2);
yield return new WaitForSeconds(1);
anim.SetInteger("condition", 0);
anim.SetBool("attacking", false);
}
}
The animator :
https://zupimages.net/viewer.php?id=20/27/a9i5.png
Please I need some help on this issue
Your answer
Follow this Question
Related Questions
Creating animations using first person controller 1 Answer
How to make arms aim at target 0 Answers
Player teleports when animation finishes 0 Answers
Animator Layers performance (No motion) 0 Answers