So I have a Prefab of several blades of grass. I’d like when my Player touches the grass it flattens to the ground (rotates?), or at the very least flares outward a bit, then when my Player leaves the trigger it pops back up to the original position. I am having some problems with it. I’ve played around with Quaternion.FromToRotation along with some other functionality but can’t seem to get it to work quite right (Couldn’t get the blades to lay flat, and the return animation just make the blades spin infinitely). So I was wondering if anyone could point me in a good direction? This is what I have right now (not that it helps a whole lot):
using UnityEngine;
using System.Collections;
public class GrassSway : MonoBehaviour {
private Transform obj;
private bool animA = false;
private bool animB = false;
private int childCnt;
private Vector3[] oChildRotate;
void Start(){
obj = GetComponent<Transform> ();
childCnt = obj.childCount;
oChildRotate = new Vector3[childCnt];
for(int i = 0; i < childCnt; i++){
oChildRotate[i] = obj.GetChild(i).transform.eulerAngles;
}
}
void FixedUpdate(){
if (animA) {
AnimateAway ();
} else if (animB) {
AnimateBack ();
}
}
void AnimateAway(){
for (int i = 0; i < childCnt; i++) {
//rotate each blade "flat"
}
}
void AnimateBack(){
for (int i = 0; i < childCnt; i++) {
//rotate each blad back to oChildRotate
}
}
void OnTriggerEnter(Collider other) {
animA = true;
animB = false;
}
void OnTriggerExit(Collider other){
animA = false;
animB = true;
}
}