- Home /
How to change an objects position and rotation inside a script?
Hey I'm fairly new to unity so I don't know too much about scripting. I made this simple script for wood cutting where if the health of the tree reaches 0 it will play a random sound and start falling. The problem was that I done it with an animations so every other tree I cut would fly over to the first tree an start falling. How would I script it so that the tree would start falling over? I looked around and everything look very complicated, is there a simple way to do this?
Also as you can see when you cut the tree it will drop 3 planks, how do I put those 3 lines into one line. Lastly is there a way to make then tree drop a random amount between 1-3?
Thanks, I hope you guys can help! :)
Tree script:
#pragma strict
var Health = 100;
var sounds : AudioClip[];
var woodPlanks01 : Transform;
var woodPlanks02 : Transform;
var woodPlanks03 : Transform;
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
if (audio.isPlaying) return;
audio.clip = sounds[Random.Range(0,sounds.length)];
audio.Play();
collider.enabled = false;
Instantiate(woodPlanks01, transform.position, Quaternion.identity);
Instantiate(woodPlanks02, transform.position, Quaternion.identity);
Instantiate(woodPlanks03, transform.position, Quaternion.identity);
animation.Play("TreeFalling");
yield WaitForSeconds(10);
Destroy (gameObject);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613111528im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
animation & position problem 0 Answers
Objects not always Spawning At Correct Location 2 Answers
how to ignore transform.position.y 3 Answers
How to make a script access animations from a child prefab 1 Answer