Created
June 27, 2017 16:32
-
-
Save juliandunn/7511e01f6e5c3d9ab89d865f1ca80660 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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