Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KittenRandomController : MonoBehaviour {
public float speed;
private int frameupdaterate; // only change the kitten velocity every FrameUpdateRate frames
private float framecounter = 0;
private Rigidbody rb;
private Animator myAnimator;
private Vector3 v;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
myAnimator = GetComponent<Animator> ();
frameupdaterate = Random.Range (25, 200);
v = new Vector3 (0, 0, 0);
}
void FixedUpdate() {
framecounter++;
if (framecounter >= frameupdaterate) {
v = GetRandomVelocity ();
rb.AddForce (v * Time.deltaTime * speed, ForceMode.VelocityChange);
framecounter = 0;
// Update animator parameters
Debug.Log (myAnimator.GetFloat ("xVelocity"));
Debug.Log (myAnimator.GetFloat ("zVelocity"));
myAnimator.SetBool ("moving", true);
myAnimator.SetFloat ("xVelocity", v.x);
myAnimator.SetFloat ("zVelocity", v.z);
} else {
//rb.AddForce (v * Time.deltaTime * speed, ForceMode.Force);
myAnimator.SetBool ("moving", false);
}
}
private Vector3 GetRandomVelocity () {
return new Vector3 (Random.Range (-12.0f, 12.0f), 0.0f, Random.Range (-12.0f, 12.0f));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment