- Home /
Funny error. collision just hit 12 times
Hey, Guys!
Well... that's odd, but I'll try to explain. I have a robot with a box collider in his sword. And I have a Script attached to my player that checks the collision with the enemy's sword, And so far, it's barely ok. Because I have 2 issues.
First. When the robot's sword hits the player, the collision checks twice or more, but I'm gonna let this way, maybe I just expand the player health, though.
Second. The robot saw me, run at me and start to attack. every hit will collide and drop my health, but after the 12nd hit, the collision doesn't works anymore. He's just swinging his sword, and the collisions backs to work when I move the player.
Very odd, huh? Is that happened before? Well, I'm posting my scripting so far.
The Enemy AI script:
var waypoint : Transform[]; // The amount of Waypoint you want
var patrolSpeed : float = 3; // The walking speed between Waypoints
var loop : boolean = true; // Do you want to keep repeating the Waypoints
var player : Transform; // Referance to the Player
var dampingLook = 6.0; // How slowly to turn
var pauseDuration : float = 0; // How long to pause at a Waypoint
var attackRange = 10; // Range to start the attack
var attackSpeed = 5.0; // Speed to attack
var attackLook = 10.0; // How fast to turn when attacking
var AtkSpeed = 1;
private var distanceToPlayer : int; // Distance from the enemy to the Player
private var curTime : float;
private var currentWaypoint : int = 0;
private var character : CharacterController;
private var gravity : float = 2.0;
private var attacking : boolean = false;
private var isattacking : boolean = false;
function Awake(){
}
function Start(){
character = GetComponent(CharacterController);
animation["attack"].speed = AtkSpeed;
animation["attack"].layer = 1;
}
function Update(){
distanceToPlayer = Vector3.Distance(player.position, transform.position); // Distance between the enemy and the Player
if(distanceToPlayer < attackRange){
attacking = true;
attack();
}else{
attacking = false;
}
if(currentWaypoint < waypoint.length && !attacking){
patrol();
}else{
if(loop && !attacking){
currentWaypoint=0;
}
}
}
function patrol(){
var target : Vector3 = waypoint[currentWaypoint].position;
target.y = transform.position.y; // Keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 0.5){
if (curTime == 0) {
curTime = Time.time; // Pause over the Waypoint
animation.Play("idle");
}
if ((Time.time - curTime) >= pauseDuration){
currentWaypoint++;
curTime = 0;
}
}else{
// Look at and dampen the rotation
var rotation = Quaternion.LookRotation(target - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingLook);
character.Move(moveDirection.normalized * patrolSpeed * Time.deltaTime);
animation.Play("walk");
}
}
function attack(){
// Attack the Player
if (distanceToPlayer <= 1) {
animation.Play("attack");
}
else {
animation.Stop("attack");
}
// Rotate to face the Player
var lookPos = player.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * attackLook);
// Move towards the Player
var target : Vector3 = player.position;
var moveDirection : Vector3 = target - transform.position;
moveDirection.y = 0; // Stop the character running up off the floor to match the Players Y axis
moveDirection.y -= gravity; // Add gravity so the he always stays on the ground
character.Move(moveDirection.normalized * attackSpeed * Time.deltaTime);
animation.Play("run");
}
And that's is the script that checks the collision the player with the sword
var PlayerLife = 3;
var player : Transform;
private var m_isHit : boolean = false;
//function OnControllerColliderHit(hit: ControllerColliderHit){
function OnTriggerExit (other : Collider) {
if(other.gameObject.tag == "attackPoint"){
PlayerLife -= 1;
}
}
//}
function Update () {
print ("Health :" +PlayerLife);
}
Answer by justinl · Sep 19, 2012 at 05:24 AM
Maybe your distanceToPlayer variable becomes larger than 1.
That's make sense... should I raise distance in the if statment?
Give it a shot. I'd Debug.log(distanceToPlayer ); on your update so you can see what's going on there to isolate if that variable is changing and causing your script to misbehave