- Home /
How do I allow third person character to hit a object?
I am creating a third person survival game for mobile and I am able to move my character with joystick, press button to run attack animation. The problem is how the object know it being hit.
Answer by unity_ek98vnTRplGj8Q · Jan 14, 2020 at 04:00 PM
There are a million different ways to do this but two easy ways are:
1. Whenever you play the animation, just check if there is anything in front of you (check out Physics.OverlapSphere, you can cast a sphere right in front of you when you attack and see if anything is there)
2. If you want more precise control over hit boxes for your weapon, you could attach invisible colliders as children to your weapon, so that they move with the animation. Then you can look at their OnCollisionEnter function to see when they hit something
Since I am using the second way you mention here and it works now I can chop the tree thanks. I chop tree in single time and it will destroy the tree but how to chop tree in several times before destroy the tree?
Here is the code.
public class treecollision : $$anonymous$$onoBehavior{
public GameObject explosion;
public int NumberOfHits = 3;
private int health;
void Start(){
health = NumberOfHits;
}
private void OnTriggerEnter(Collider other){
health--;
Debug.Log("Tree has " + health + " hits remaining");
if(health <= 0) DestroyTree();
}
private void DestroyTree(){
GameObject e = Instantiate(explosion) as GameObject;
e.transform.position = transform.position;
Destroy(gameObject);
this.gameObject.SetActive(false);
}
}
Your answer
Follow this Question
Related Questions
growing / feeding NEED HELP!!! 1 Answer
Is object at least partly visible? 1 Answer
Change raycasting 1 Answer
Collision at which side? 1 Answer