Hi all I wonder if someone can help me with a particular problem that I'm having. The help would be much appreciated. The scenario that I've got would be that I have two boxes. One is following the playable character, whereas the other is following the non-playable character. When the playable character collides with the box following the non-playable character it gets desstroyed. But what I want to do is once it is destroyed respawn it and make it follow the box that is following the playable character.
Here are my scripts so far:
function OnControllerColliderHit (CChit : ControllerColliderHit) { Debug.Log(CChit.gameObject.name); if(CChit.gameObject.name == "Following_NPC") {
Destroy(gameObject.Find("Following_NPC"));
}
}
var speed = 6.0; var jumpSpeed = 8.0; var gravity = 10.0; var rotateSpeed = 1.0; private var moveDirection = Vector3.zero;
function FixedUpdate() {
var controller : CharacterController = GetComponent(CharacterController);
if (controller.isGrounded) {
moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if(Input.GetButton("Jump")){
moveDirection.y = jumpSpeed;
}
}
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
// Rotate around y axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
//transform.Translate(Input.GetAxis ("Vertical") * speed, 0, 0);
//Apply gravity
moveDirection.y -= gravity*Time.deltaTime;
}
function FixedUpdate() { //Assign z axis direction to newly created variable var fwd = transform.TransformDirection(Vector3.forward);
//If not casting ray at the position of character, along the z axis and a meter forwards then if(!Physics.Raycast(transform.position, fwd, 1)) {
//Move 1 meter forwards transform.Translate(0, 0, 1*Time.deltaTime); //And rotate 10 degrees (clockwise) along the y axis transform.Rotate(0, 10*Time.deltaTime, 0);
}
}
var cam_target1 : Transform; //var cam_target2 : Transform; var camRate1; //var CChit : ControllerColliderHit; //camRate variable is the speed at which the camera travels to get to the cube once it has finished moving
//function Start() { //transform.LookAt(cam_target1); //}
function FixedUpdate() { Following_NPC.transform.position); transform.LookAt(cam_target1); camRate1 = 5 * Time.deltaTime; if(Vector3.Distance(cam_target1.position, transform.position) > 2) {
if(transform.position.x > cam_target1.position.x) {
transform.position.x = transform.position.x - camRate1;
}
else if(transform.position.x < cam_target1.position.x) {
transform.position.x = transform.position.x + camRate1;
}
if(transform.position.z > cam_target1.position.z) {
transform.position.z = transform.position.z - camRate1;
}
else if(transform.position.z < cam_target1.position.z) {
transform.position.z = transform.position.z + camRate1;
}
if(transform.position.y > cam_target1.position.y) {
transform.position.y = transform.position.y - camRate1;
}
else if(transform.position.y < cam_target1.position.y) {
transform.position.y = transform.position.y + camRate1;
}
}
}