I am building an tower defence game and I want to implement a Ally Running the opposite way
got this piece of code running but it is not working
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ally : MonoBehaviour
{
public float StartSpeed;
private float Speed;
public int AllyHealth;
private Waypoint Waypoints;
private int WayPointIndex;
public string EnemyTag = "Enemy";
// Start is called before the first frame update
void Start()
{
Speed = StartSpeed;
Debug.Log (gameObject.name + "Ally Spawned") ;
Waypoints = GameObject.FindGameObjectWithTag("WayPoints").GetComponent<Waypoint>();
}
// Update is called once per frame
void Update()
{
Travel();
CheckDestination();
CheckHealth();
}
void OnCollisionEnter (Collision target)
{
if ( target.gameObject.tag == EnemyTag )
{
Debug.Log ("Enemy Collision Detected" + target.gameObject.name );
target.gameObject.GetComponent<Enemy>().TakeDamage( AllyHealth );
}
AllyHealth = AllyHealth - target.gameObject.GetComponent<Enemy>().Health;
}
void CheckDestination()
{
if (WayPointIndex == Waypoints.AllyWayPoints.Length - 1)
{
AllyHealth = 0;
Die();
}
}
void CheckHealth()
{
if ( AllyHealth <= 0 )
{
Die();
}
}
void Travel()
{
transform.position = Vector3.MoveTowards( transform.position, Waypoints.AllyWayPoints[WayPointIndex].position, Speed * Time.deltaTime );
if ( Vector3.Distance( transform.position, Waypoints.AllyWayPoints[WayPointIndex].position ) <0.2f )
{
WayPointIndex++;
}
}
void Die()
{
if ( AllyHealth <= 0 )
{
//Debug.Log ("Died");
Destroy(this.gameObject);
return;
}
}
}
Have I made any mistakes in Collision Detection??
I saw the unity’s Official tutorial on collision detection and I am pretty sure I Did a good job and BTW the TakeDamage function reduces the Enemy Health by the Damage
My Debug.Log Which says that enemy collision detected doesn’t Go Off
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Nope none of these are needed, I made an Newbie mistake.
I read somewhere while doing this that you need either Collider or RigidBody to Detect collisions.
But it seems like you must have a Rigidbody
After Adding a RigidBody i am able to detect collisions
But I could definitely use the tips that you gave me
Thanks,
Dev
This is misleading and I wish other devs wouldn’t state it like that because it’s more the case that if you don’t add a Rigidbody then the Collider is Static i.e. non-moving (and you shouldn’t be moving it either). Static colliders don’t collide with Static colliders because why would they? They never (should) move anyway.
By adding a Rigidbody the collider is Dynamic (moving and has a collision response) or Kinematic if you select the “IsKinematic” option. Dynamic vs Static do collide. Kinematic vs Static don’t by default.
So it’s not just that you “need to add a Rigidbody” for collisions to work but that Static vs Static do not interact because it doesn’t make sense to.
Colliders don’t move, Rigidbodies do. Colliders have no position in space, Rigidbodies do. When you don’t add a Rigidbody it’s implicitly created against Static body behind the scenes for you (so Static = Not Moving). Unity does this because it’s convenient for adding Static colliders. Colliders don’t exist without a rigidbody
Therefore what you’re asking for makes no sense and I absolutely don’t understand the reason why you are avoiding adding a rigidbody yourself explicitly. It’s as if you have some notion that it’s a bad thing or maybe you perhaps don’t understand what they are. Not sure.
I actually didn’t have any idea of adding a rigidbody to my character because I planned to move and do everything with scripts and I had no idea of adding one