I need help with this script
So i'm making a character controller that when i left click my character stops in place and does the attack animation, so far I've had no difficulty with anything but getting him to wait wait 5 seconds before setting "speed" to 3, i still want unity to run as there will be other characters moving too, i'm new to unity and coding in general so this script may be an eyesore to everyone, sorry, would appreciate some help, thanks.
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class PlayerController : MonoBehaviour { public bool CursorLockMode { get; private set; }
static Animator anim;
public float speed = 2.0f;
public float rotationspeed = 75.0f;
public Vector2 MovementVector { get; private set; }
public bool IsInputEnabled = true;
private void Start() {
anim = GetComponent<Animator>();
}
void Update()
{
float translation = Input.GetAxis("Vertical") * speed;
float rotation = Input.GetAxis("Horizontal") * rotationspeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0, 0, translation);
transform.Rotate(0, rotation, 0);
if (Input.GetMouseButtonDown(0))
{
anim.SetBool("IsWalking", false);
anim.SetTrigger("Attack");
transform.Translate(0, 0, 0);
speed = 0.0f; //
}
if (translation != 0)
{
anim.SetBool("IsWalking", true);
}
else {
anim.SetBool("IsWalking", false);
if (Input.GetMouseButtonDown(0))
{
anim.SetBool("IsWalking", false);
anim.SetTrigger("Attack");
Comment
Best Answer
Answer by Dragate · Oct 23, 2017 at 01:06 PM
Sounds like you need a Coroutine.
Make a function like:
IEnumerator setSpeedAfter(float seconds){
yield return new WaitForSeconds(seconds);
speed = 3;
}
Replace where you have
speed = 0.0f; //
with
StartCoroutine(setSpeedAfter(5));
Your answer
