Hi guys.
In my game player changes to ragdoll when it falls from high places.
My problem is that ingame enemies use player as a “waypoint” when he is close enough to them. So when my “player” falls he is destroyed and replaced by ragdoll, and i’m receiving an error from enemie’s “ai” script :
So i’ve got no idea how to solve it. PLease help…
p.s. AI script:
var waypoint : Transform[];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
var player : Transform;
var botAgro : AudioClip;
var botAgro2 : AudioClip;
function Avake(){
waypoint[0] = transform;
}
function Update () {
if(currentWaypoint < waypoint.length) {
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target -transform.position;
moveDirection.y = 0;
var velocity = rigidbody.velocity;
var distFromPlayer : Vector3 = player.position - transform.position;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
else if(distFromPlayer.magnitude < 20){
velocity = Vector3.zero;
target = player.position;
velocity = (player.position - transform.position).normalized * speed;
if((player.position - waypoint[currentWaypoint].position).magnitude >20){
target = waypoint[currentWaypoint].position;
velocity = moveDirection.normalized * speed;
}
if(target == player.position){
audio.PlayOneShot (botAgro, 0.5);
}
if (!audio.isPlaying)
{
audio.clip = botAgro2;
audio.Play();
}
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity=Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
Either you make a third object/script, containing the Transform of the player (AI script look at that script), and when the player falls, it changes that Transform variable into the new Transform…
Or you make a function in the AI script, to allow changing the target. When the player falls, SendMessage to all AI scripts, and define the new Transform.
I’ve tried to do it this way:
in “player damage” script i’ve created a boolean var so when the player is destroyed it becomes true. And from ai script i’m reading this var and if it’s true player = playerRag(ragdoll).
Here’s the code
var waypoint : Transform[];
var speed : float = 20;
private var currentWaypoint : int;
var loop : boolean = true;
var player : Transform;
var playerRag : Transform; //ragdoll
var botAgro : AudioClip;
var botAgro2 : AudioClip;
var death : CharacterDamage; // player damage script where the "death boolean" is stored.
function Avake(){
waypoint[0] = transform;
}
// SO this is a piece of code to change targets
function Update () {
if(death.bobDead) {
player == playerRag;
}
if(currentWaypoint < waypoint.length) {
var target : Vector3 = waypoint[currentWaypoint].position;
var moveDirection : Vector3 = target -transform.position;
moveDirection.y = 0;
var velocity = rigidbody.velocity;
var distFromPlayer : Vector3 = player.position - transform.position;
if(moveDirection.magnitude < 1){
currentWaypoint++;
}
else if(distFromPlayer.magnitude < 20){
velocity = Vector3.zero;
target = player.position;
velocity = (player.position - transform.position).normalized * speed;
if((player.position - waypoint[currentWaypoint].position).magnitude >20){
target = waypoint[currentWaypoint].position;
velocity = moveDirection.normalized * speed;
}
if(target == player.position){
audio.PlayOneShot (botAgro, 0.5);
}
if (!audio.isPlaying)
{
audio.clip = botAgro2;
audio.Play();
}
}
else{
velocity = moveDirection.normalized * speed;
}
}
else{
if(loop){
currentWaypoint=0;
}
else{
velocity=Vector3.zero;
}
}
rigidbody.velocity = velocity;
transform.LookAt(target);
}
But it didn’t help i’m still receiving the same error…
well i’m not sure if i got You right… In the inspector i’ve assigned playerRagdoll pref to the playerRag var for the ai script. Also i’ve tried to assign any object in the scene to playerRag but the result is the same…
I’m not too hot on JS, though, so I could be wrong.
Also, assuming you don’t need the AI to track the player’s position when they are in rag doll state (for example, you killed player and will respawn at another location) then you can just check to see if player is null and return if that is the case…