Skip to content

Instantly share code, notes, and snippets.

@juliandunn
Created June 27, 2017 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juliandunn/7511e01f6e5c3d9ab89d865f1ca80660 to your computer and use it in GitHub Desktop.
Save juliandunn/7511e01f6e5c3d9ab89d865f1ca80660 to your computer and use it in GitHub Desktop.
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