Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jabes22 · May 15, 2013 at 04:07 PM · collisionquaternioncolliders

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jabes22 · May 15, 2013 at 05:31 PM 0
Share

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 :)

avatar image Jabes22 · May 15, 2013 at 06:12 PM 0
Share

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.

avatar image Kibsgaard · May 16, 2013 at 08:01 PM 0
Share

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 ^^

avatar image Jabes22 · May 17, 2013 at 01:21 AM 0
Share

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;*/
avatar image Jabes22 · May 17, 2013 at 01:22 AM 0
Share

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...

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Colliders 2d apparently touch each other even if they should not. 2 Answers

Unity detecting collision without touching 1 Answer

Destroy Turret with machine Gun 0 Answers

How do you change the collisions of all of one sort of tile on a tilemap. Also, how do you change the collision box on tiles in your tilemap 0 Answers

Temporarily ignore collisions between player and enemies 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges