- Home /
FPS Controller and Explosion?
I'm using the detonator framework and have an fps controller that can't be affected by the explosion. I have written this script so far so that the player can be moved:
var targetObj : Transform;
var prefabContainer:Transform;
var proximity : float = 8;
function Update() {
var dist = Vector3.Distance(targetObj.transform.position, transform.position);
if (dist < proximity) {
var instanceContain = Instantiate(prefabContainer, targetObj.transform.position, targetObj.transform.rotation);
targetObj.transform.parent = instanceContain;
}
}
But unity doesn't allow prefab objects to be parented. How do I set targetObj to be a specific item in the hierarchy and not a prefab?
Answer by deltamish · Oct 28, 2012 at 03:18 PM
Hai found your problem
use
targetObj.transform.parent = instanceContain.transform;
I get this error:
Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption. UnityEngine.Transform:set_parent(Transform) PlayerExplosion:Update() (at Assets/PlayerExplosion.js:10)
Hmm what is instanceContain and targetObj
why do you want to parent them to add explosion
I'm trying to parent the charactercontroller (targetObj) into a container with a rigidbody (instanceContain) so that it is affected by the explosion.
Answer by deltamish · Nov 01, 2012 at 11:38 AM
Hi, sorry for the late reply I was quite busy You can 1.Create a empty gameobject and child the player obj.Remove Charactercontroller .Add rigidbody ,Use rigidbody.velocity instead of CharacterController.Move to move the player
OR
havent tried this yet Add this to your player prefab
var exppos:Vector3; var self:Transform; var explode:boolean = false; var minexp:float = 5.0; var char:CharacterController; var expspeed:float = 12;
function Explosion(pos:Vector3){ self = pos; explode = true; }
function update(){ if(explode){ if(Vector3.Distance(self.position,exppos) < minexp){//checking second time that if player's pos is less than minimu explosion distance var direction = self.position - exppos;
char.Move(direction.normalized expspeed Time.deltaTime);
}else{explode = false;} } // your movement code }
In your explosion script add this
var pos:Vector3;
function Update(){
var objs:Collider[] =
Physics.OverlapSphere(transform.position,9);
for(var:exp in objs){
if(exp.transform.tag =="Player"){
exp.transform.SendMessage("Explosion",pos,SendMessageOption.DontRequireReceiver)
;}
}
}
Rate it if you like it
Your answer
Follow this Question
Related Questions
Detect explosion Character Controller 4 Answers
Changing FPSController values via C#? 1 Answer
Instantiating 1 Answer
calling a CS module from within Javascript 0 Answers
Setting Scroll View Width GUILayout 1 Answer