- Home /
I need help with my enemy collision.
My player dies when it collides with enemys but when my player sits still he takes no damage from enemys colliding with the player, here is my damage and ai script
DAMAGE SCRIPT.js
var Damage = 10;
function OnTriggerEnter (col : Collider) { var player : FPSPlayer = col.GetComponent(FPSPlayer);
if (player) {
player.ApplyDamage(Damage);
} else if (col.rigidbody) {
//Destroy(col.rigidbody.gameObject);
} else {
//Destroy(col.gameObject);
}
}
function Reset () { if (collider == null)
gameObject.AddComponent(BoxCollider); collider.isTrigger = true; }
AI Script.cs
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public Transform target; public int moveSpeed; public int rotationSpeed; public int maxDistance;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 1;
}
// Update is called once per frame
void Update () {
// Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//look at target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position) > maxDistance) {
//move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}}
Answer by The_r0nin · Jul 16, 2011 at 01:13 PM
Do both have Kinematic Rigidbodies? If not (from http://unity3d.com/support/documentation/Components/RigidbodySleeping.html):
When Rigidbodies fall to rest - a box landing on the floor - they will start sleeping. Sleeping is an optimization which allows the Physics Engine to stop processing those rigidbodies. This way you can have huge amounts of rigidbodies in your scene as long as you make sure that they normally don't move.
Rigidbody sleeping happens completely automatically. Whenever a rigidbody is slower than the sleepAngularVelocity and sleepVelocity it will start falling asleep. After a few frames of resting it will then be set to sleep. When the body is sleeping, no collision detection or simulation will be performed anymore. This saves a lot of CPU cycles.
...
Kinematic rigidbodies wake up sleeping rigidbodies. Static Colliders do not. If you have a sleeping rigidbody and you move a static collider (A collider without a Rigidbody attached) into the rigidbody or pull it from underneath the rigidbody, the sleeping rigidbody will not awake.
My best guess:
So, since you are moving the enemies by transform rather than by rigidbody, I would assume you have just a collider on them and not rigidbodies. When the character is moving, its rigidbody is awake for collisions. When he is still, his rigidbody falls asleep and no longer detects collisions from non-rigidbodies...
Your answer
Follow this Question
Related Questions
Audio play when object hit collision. 2 Answers
hide child object script - help 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Collider Error[SOLVED] 2 Answers