- Home /
Can you help me sort out my script and collision?
Hey,
I've been at Google, Unity Forums, and documentation for awhile now but can't see to fix the problems that I'm having. I'm trying to make a script that when the player clicks an object (In this case a flashlight) the object lifts in the air, comes to the player, and rests on the screen in front of the player (Where the flashlight would normally be held out for use in game as if it were auto equipped.)
The biggest problem that I'm facing is that the flashlight gets stuck on walls when I'm walking around.
Here's the set up that I'm using.
1) Flashlight mesh with box collider. This collider has a physics material with 0 friction on everything. This is not set to trigger so it can collide with other colliders.
2) I have an empty game object resting beside the player where I want the flashlight to go. This collider is set to trigger.
3) My walls also have box colliders with physics materials with fictions of 0.
Here's the script attached to the flashlight.
#pragma strict
var speed = 4.0;
var FlashLightHolder:GameObject;
var PickUpFlashlight = false;
private var increment:float;
private var rotation:Quaternion;;
private var FlashLightInPlace = false;
function OnMouseDown(){
PickUpFlashlight = true;
Debug.Log("PickUpFlashlight = true;");
}
// Create a function that will parent the flashlight to the falshlight holder
// when the flashlight enters the flashlight holder.
function OnTriggerEnter(MyTrigger: Collider){
Debug.Log("collision detected between flashlight and flashlight holder.");
if (MyTrigger.gameObject.name == "FlashLightHolder"){
Debug.Log("Collision If Statement Fired.");
transform.parent = FlashLightHolder.transform;
FlashLightInPlace = true;
}
}
function Start () {
}
function Update () {
if(increment <=1 && PickUpFlashlight == true)
increment += speed/200; // This will affect the speed object travels at.
// position of flashlight = outcome of function below. The first value passed is the position
// of the flashlight (script object), the second arguement is the position of object b (game object,
// and the third arguement is the speed at which the moving object will travel.
transform.position = Vector3.Lerp(transform.position, FlashLightHolder.transform.position, increment);
//Add this block only if you want the rotation also be transitioned to objectB's rotation.
/* Declare a variable of vector (Representation of 3D vectors and points.) This vector variable
equals position of player or game object - position of flashlight (script object)
*/
var direction:Vector3 = FlashLightHolder.transform.position - transform.position;
// Set the value of rotation Quaternian to
rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, increment);
PickUpFlashlight = false;
}
A second problem that I'm having is when the flashlight gets to the collider flashlight holder that I have parented to the player it kinda snaps into place weird.. Not horrible, but not smooth enough...
Is there anyone that can please help me sort of my script and collision problems. I just can't seem to find the answers...
Thanks a million, J.
Answer by Kibsgaard · May 15, 2013 at 04:19 PM
I believe the normal solution people use is just disabling the collider on the flashlight / weapon when the player has picked it up.
Thanks for the answer :)
If I disable that collider how will it detect collision against other objects? Should I make that happen through the empty game object that will hold the flashlight in front of the player?
The thing is I've tried many different ways of not getting the flashlight stuck on walls... including not using the gameobject that holds the flashlight to the play and just parenting the flashlight beside the player with a box collider...
I just can't seem to keep any collider from going into the wall and getting stuck..
is there a certain way to go about this?
Thanks again :)
I'm just curious. Couldn't I just write code that tells my flashlight to move away from any object it has collided with? I never even thought of trying something like that. I just wanted to rely on as much stock behavior as I could...
I figured the colliders wold work well for what they're supposed to do.
Any reason why you want it to collide when the player has it picked up? Else just disable the collider when its picked up and maybe make the players collider big enough to also include the flashlight.
"I figured the colliders wold work well for what they're supposed to do."
They do work well, if you use them correctly. How are you moving your player? transform.position? If so, you are working against the physics engine and can force colliders to penetrate. Ins$$anonymous$$d try with rigidbody.$$anonymous$$ovePosition or rigidbody.AddForce, and if you want more flexibility between the flashlight and the player try attaching it with a fixed or configurable joint.
Hope this helps, if not, please elaborate more on what exactly you want the flashlight to be able to do ^^
Thanks, I'll look into the fixed joint. I haven't touched them yet...
Well. I have a flashlight model and have it so when the player clicks the flashlight it floats to them, and positions itself in front of the first person controller... kind of like a gun would... there's no hands, only a flashlight...
I'm trying to figure out how to get the light to point around where the mouse cursor is pointing, but having a hard time doing it :)
Here's the script so far (Still a little messy)... I don't want the flashlight to be at the cursor like some code that I've seen... but rather stay where the code I have positions it... but make it point in the direction of the cursor..
#pragma strict
var depth = 1;
var speed = 4.0;
var FlashLightHolder:GameObject;
var PickUpFlashlight = false;
private var increment:float;
private var rotation:Quaternion;;
private var FlashLightInPlace = false;
function On$$anonymous$$ouseDown(){
PickUpFlashlight = true;
Debug.Log("PickUpFlashlight = true;");
}
function PickUpFlashLight(){
if(increment <=1 && PickUpFlashlight == true){
increment += speed/200;
transform.position = Vector3.Lerp(transform.position, FlashLightHolder.transform.position, increment);
var direction:Vector3 = FlashLightHolder.transform.position - transform.position;
rotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, increment);
gameObject.transform.eulerAngles = Vector3(-90, 0, -90);
}
}
function OnTriggerEnter($$anonymous$$yTrigger: Collider){
Debug.Log("collision detected between flashlight and flashlight holder.");
if ($$anonymous$$yTrigger.gameObject.name == "FlashLightHolder"){
Destroy (collider);
Debug.Log("Collision If Statement Fired.");
transform.parent = FlashLightHolder.transform;
FlashLightInPlace = true;
}
}
function Start () {
}
function Update () {
PickUpFlashLight();
}
//This section of code makes an object follow the mouse cursor.
/*var mousePos = Input.mousePosition;
var wantedPos = Camera.main.ScreenToWorldPoint (Vector3 (mousePos.x, mousePos.y, depth));
transform.position = wantedPos;*/
By the way, I abandoned the colliders and took a page from what you said before... I think I'll try drawing the flashlight on a new layer... I wanted the colliders because I thought of incorporating the flashlight into being a weapon... but I'm gonna simplify I think...