- Home /
How to create gun sprinting animation by script.
How can I create gun sprinting animation just by script. Please give me script or give me some idea. Any answers to this question is appreciate here.
What special requirement needs you to create the animation by script? Because it might be the case that just tweaking some parameters of animation at runtime could yield in the result you want.
Answer by Skaster87 · Nov 17, 2017 at 06:54 AM
Hi, I extracted all this from the standard third person controller unity provides, the animationSpeedMultiplier variable will alter how fast your animations cycle.s play.
using UnityEngine;
using UnityEngine.AI;
public class Character : MonoBehaviour
{
[Header("Animator")]
[SerializeField]
RuntimeAnimatorController animatorController;
[SerializeField] AnimatorOverrideController animatorOverrideController;
[SerializeField] Avatar characterAvatar;
[SerializeField] [Range(.1f, 1f)] float animatorForwardCap = 1f;
[Header("Nav Mesh Agent")]
[SerializeField]
float navMeshAgentSteeringSpeed = 1.0f;
[SerializeField] float navMeshAgentStoppingDistance = 1.3f;
NavMeshAgent navMeshAgent;
Animator animator;
Rigidbody ridigBody;
public AnimatorOverrideController GetOverrideController()
{
return animatorOverrideController;
}
void Move(Vector3 movement)
{
SetForwardAndTurn(movement);
ApplyExtraTurnRotation();
UpdateAnimator();
}
void SetForwardAndTurn(Vector3 movement)
{
// convert the world relative moveInput vector into a local-relative
// turn amount and forward amount required to head in the desired direction
if (movement.magnitude > moveThreshold)
{
movement.Normalize();
}
var localMove = transform.InverseTransformDirection(movement);
turnAmount = Mathf.Atan2(localMove.x, localMove.z);
forwardAmount = localMove.z;
}
void UpdateAnimator()
{
animator.SetFloat("Forward", forwardAmount * animatorForwardCap, 0.1f, Time.deltaTime);
animator.SetFloat("Turn", turnAmount, 0.1f, Time.deltaTime);
animator.speed = animationSpeedMultiplier;
}
void ApplyExtraTurnRotation()
{
// help the character turn faster (this is in addition to root rotation in the animation)
float turnSpeed = Mathf.Lerp(stationaryTurnSpeed, movingTurnSpeed, forwardAmount);
transform.Rotate(0, turnAmount * turnSpeed * Time.deltaTime, 0);
}
void OnAnimatorMove()
{
// we implement this function to override the default root motion.
// this allows us to modify the positional speed before it's applied.
if (Time.deltaTime > 0)
{
Vector3 velocity = (animator.deltaPosition * moveSpeedMultiplier) / Time.deltaTime;
// we preserve the existing y part of the current velocity.
velocity.y = ridigBody.velocity.y;
ridigBody.velocity = velocity;
}
}
But you can import the whole thing by going here ->
But
@Skaster87 Thanks for the script but can you tell me how would I implement this code in my gun so that it will sprint. I hope u understand my question.
@AshwinTheGammer What code currently controls your movement? Can you provide an example and I may be able to modify it for you.