Search Unity

Flattening Grass OnTriggerEnter

Discussion in 'Scripting' started by Hyubusa, Jul 4, 2015.

  1. Hyubusa

    Hyubusa

    Joined:
    Jun 18, 2015
    Posts:
    5
    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):

    upload_2015-7-4_1-23-21.png

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GrassSway : MonoBehaviour {
    5.     private Transform obj;
    6.     private bool animA = false;
    7.     private bool animB = false;
    8.     private int childCnt;
    9.     private Vector3[] oChildRotate;
    10.  
    11.     void Start(){
    12.         obj = GetComponent<Transform> ();
    13.         childCnt = obj.childCount;
    14.         oChildRotate = new Vector3[childCnt];
    15.  
    16.         for(int i = 0; i < childCnt; i++){
    17.             oChildRotate[i] = obj.GetChild(i).transform.eulerAngles;
    18.         }
    19.  
    20.     }
    21.  
    22.     void FixedUpdate(){
    23.         if (animA) {
    24.             AnimateAway ();
    25.         } else if (animB) {
    26.             AnimateBack ();
    27.         }
    28.  
    29.  
    30.  
    31.     }
    32.  
    33.     void AnimateAway(){
    34.         for (int i = 0; i < childCnt; i++) {
    35.             //rotate each blade "flat"
    36.         }
    37.     }
    38.  
    39.     void AnimateBack(){
    40.         for (int i = 0; i < childCnt; i++) {
    41.             //rotate each blad back to oChildRotate
    42.         }
    43.     }
    44.  
    45.     void OnTriggerEnter(Collider other) {
    46.         animA = true;
    47.         animB = false;
    48.     }
    49.  
    50.     void OnTriggerExit(Collider other){
    51.         animA = false;
    52.         animB = true;
    53.     }
    54. }
    55.  
     
    Last edited: Jul 4, 2015
  2. Pavlon

    Pavlon

    Joined:
    Apr 15, 2015
    Posts:
    191
    i think the best way is to do this in a 3d programm and create 4 animations

    1.idle
    2.bending down
    3.on the ground
    4.popping up

    then play the needed animation
     
    Hyubusa likes this.
  3. Hyubusa

    Hyubusa

    Joined:
    Jun 18, 2015
    Posts:
    5
    You are prob right ... I am so used to doing everything from the program end I didn't even think about using the animator. Appreciate the solution, I will look into it now.
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201