I learning animation transition with blend tree and Game Object movement system with that animation. My friend create a 3D model and few animation in blender. I wrote scripts for movement system and camera follow. It’s working good enough as you can see on video DroneMovingDemo - YouTube. However, I have animation that roll drone right and left, in my idea, drone should call roll animation and after call idle after roll done, that when i push “A” or “D” my Game Object turn left or right with roll and after moving as idle. I created variable that save a Vector3.Dot function with vector of drone and vector of camera, that allow me detect drone turning, but I think I made crazy things in my animator
//variable initialization
public float InputX;
public float InputZ;
public Vector3 desiredMoveDirection;
public bool bloclRotationPlayer;
public float speed = 12f;
public float desiredRotationSpeed = 10f;
public CharacterController controller;
Vector3 velocity;
public float allowPlayerRotation;
public Animator anim;
public Camera cam;
public float gravity = -9.6f;
public float verticalVel;
private Vector3 moveVector;
public Transform groundCheck;
public float dist = 0.4f;
public LayerMask groundMask;
public float jumpH = 60f;
Vector3 mVelocity;
Vector3 droneRotation;
bool isGrounded;
public float movingSpeed = 20f;
public bool CheckAnimFalling = false;
public Vector3 objPos;
CameraLook CameraLook;
public float scalarMultForw;
Vector3 moveDir;
private void Start()
{
anim = GetComponent<Animator>();
cam = Camera.main;
controller = this.GetComponent<CharacterController>();
}
void Update()
{
//get some input
InputX = Input.GetAxis("Horizontal");
InputZ = Input.GetAxis("Vertical");
//save Inputs in variables
moveDir = new Vector3(InputX, 0 , InputZ);
//define obj Position
objPos = transform.forward;
//Vector3.Dot with camera vector and drone vector
scalarMultForw = Mathf.Round(Vector3.Dot(Camera.main.transform.right, objPos.normalized) * 100) / 100;
//calling method that define the magnitude
InputMagnitude();
//jump staff
if (Input.GetKey(KeyCode.Space))
{
verticalVel = Mathf.Sqrt(jumpH * -2f * gravity);
}
isGrounded = controller.isGrounded;
if (isGrounded)
{
verticalVel -= 0;
CheckAnimFalling = true;
}
else
{
verticalVel -= 2;
CheckAnimFalling = false;
}
//move controller via jump or falling
moveVector = new Vector3(0, verticalVel * Time.deltaTime, 0);
controller.Move(moveVector);
}
void PlayerMoveAndRotation()
{
//method for define drone moving vector
var camera = Camera.main;
var forward = cam.transform.forward;
var right = cam.transform.right;
forward.y = 0f;
right.y = 0f;
forward.Normalize();
right.Normalize();
desiredMoveDirection = forward * InputZ + right * InputX;
Vector3 moveDir = desiredMoveDirection.normalized * movingSpeed * Time.deltaTime;
controller.Move(moveDir * Time.deltaTime * 100f);
//rotate Drone to direction if I move it
if (bloclRotationPlayer == false)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(desiredMoveDirection),desiredRotationSpeed * Time.deltaTime * 0.2f);
}
}
void InputMagnitude()
{
//method that control blend tree in the animator
anim.SetFloat("InputZ", InputZ, 0.0f, Time.deltaTime * 2f);
anim.SetFloat("InputX", InputX, 0.0f, Time.deltaTime * 2f);
//checking if we stay or moving
speed = new Vector2(InputX, InputZ).sqrMagnitude;
if (speed > allowPlayerRotation)
{
anim.SetFloat("InputMagnitude",speed,0.0f, Time.deltaTime);
PlayerMoveAndRotation();
anim.SetFloat("TurnCheck", scalarMultForw, 0.0f, Time.deltaTime);
}
else if(speed < allowPlayerRotation)
{
anim.SetFloat("InputMagnitude", speed, 0.0f, Time.deltaTime);
}
}
I guess I’m crazy, and make it wrong, can you help and give advise how work with animation, blend tree, transition correctly. How make create transition that Drone firstly roll to side and after that move as idle animation? If you need more material, just say