Hello everyone, there is such a script:
public class Hero : MonoBehaviour { public CharacterController hero; public float speed = 10; public float gravity = 5F; private Vector3 moveDirection; GameObject[] tree = GameObject.FindGameObjectsWithTag("tree"); private AnimationClip NewAnimationn; // Use this for initialization void Start() { hero = gameObject.GetComponent<CharacterController>(); } // Update is called once per frame void Update() { Vector3 moveDirection = Vector3.zero; if (hero.isGrounded) { moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; // раскоментировать для прыжка // if (Input.GetButton("Jump")) // moveDirection.y = jumpSpeed; } moveDirection.y -= gravity * Time.deltaTime; hero.Move(moveDirection * Time.deltaTime); if (Input.GetKey(KeyCode.A)) { for (int i=0;i<tree.Length;i++) { var p= tree[i].GetComponent<Animation>(); p.AddClip( NewAnimationn,"TimeLapse"); p.Play(); } } } There is a prefab tree on the stage, and not one, I added the tag "tree" to all prefabs - and on the line
GameObject[] tree = GameObject.FindGameObjectsWithTag("tree"); - I search for objects by tag and stuff them into an array, then when I press the D button (I and the character move to the side) and I reboot this array - that is, I apply to each object with such a tag and its component animation and assign all this to a field named p, so that later on it call the AddClipp () method - with which to add to each prefab of the tree the animation I need, which I should pass as an argument to AddClipp (). How can I send a link to
then the general animation? Why ask? Because I do not know! After all, the project also created a file.
And as I understand it, the animation file itself, and the first is the so-called animator. Explain what's what. And the last - Unity exception
- when you press the A key. In the studio, this is the string for (int i=0;i<tree.Length;i++) - that is, the beginning of the cycle
Animationand there is anAnimator- these are slightly different things. Therefore, at a minimum, you need to figure out what component there is in the tree and what method is hanging on it, depending on which you call ........ so what do you actually have there? - Alexey ShimanskyGameObject.FindGameObjectsWithTag("tree");need to do in theStartorAwakemethod, because in the editor mode, he knows nothing about the objects of the scene .... so at the time of the start there will be nothing - Alexey Shimanskypublic GameObject[] tree;...... and in the method already initializevoid Start() { tree = GameObject.FindGameObjectsWithTag("tree"); }void Start() { tree = GameObject.FindGameObjectsWithTag("tree"); }- Alexey Shimansky