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 Daniel G · Nov 26, 2013 at 03:01 PM · c#logic

How to exclude Player from Picking himself up! C#

Hello,

I am trying to pick up objects only in my viewfrustrum, not occluded from my view and exclude the player, WHICH IS in my view ugh.. Because its a third person camera. So I am thinking that is the issue. I can only pick up objects when i comment out the line "if (obj.name == "Player") return false;" BUT THE PLAYER IS PICKED UP TOO! I DONT want that to happen...

"ObjectInteractable" simply excludes objects that do not meet the criteria for being able to be moved by the controller... Thanks!

 // returns condition for if force should affect this object.
     bool ObjectInteractable(GameObject obj) {
         if (obj.rigidbody == null) return false;
         if (obj.name == "Player") return false;
         
         // ignore objects not in the view frustum.
         if (GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(Camera.main), obj.collider.bounds) == false) {
             return false;
         }
         
         // ignore objects that are occluded from our view.
         RaycastHit hit;
         if(Physics.Linecast(transform.position, obj.transform.position, out hit)) {
             if (hit.transform != obj.transform) return false;    
         }
         
         return true;
     }
     
     void Levetate() {
         if (Time.time - m_lastPushTime < 1.0f) return;
         StageOneLevetate();
     }
     
     void StageOneLevetate() {
         Collider [] m_pullObjects = Physics.OverlapSphere(transform.position, 10.0f);
         for(int i = 0; i < m_pullObjects.Length; ++i) {
             GameObject gObject = m_pullObjects[i].transform.gameObject;
             
             if (ObjectInteractable(gObject) == false) continue;
             
             if (gObject.rigidbody.mass < 1.0f) {
                 gObject.rigidbody.velocity = Vector3.up * Random.Range(0.5f, 0.8f) * (1.0f + (m_forcePoints / 500.0f));
                 gObject.rigidbody.AddTorque(new Vector3(Random.Range(-0.02f, 0.02f), Random.Range(-0.02f, 0.02f), Random.Range(-0.02f, 0.02f)));    
             }
             else if (gObject.rigidbody.mass < 5.0f) {
                 gObject.rigidbody.velocity = Vector3.up * Random.Range(0.3f, 0.4f) * (1.0f + (m_forcePoints / 500.0f));
                 gObject.rigidbody.AddTorque(new Vector3(Random.Range(-0.01f, 0.01f), Random.Range(-0.01f, 0.01f), Random.Range(-0.01f, 0.01f)));
             }
             
         }
     }


Thanks for the help!

Comment
Add comment · Show 9
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 numberkruncher · Nov 26, 2013 at 03:06 PM 1
Share

You are more likely to get help if you give your question an actual question title! Perhaps "How to pick up objects in view frustum?"

avatar image Daniel G · Nov 26, 2013 at 03:13 PM 0
Share

Thank you sir, Good point!

avatar image Daniel G · Nov 26, 2013 at 03:51 PM 0
Share

@numberkruncher do you have any ideas?

avatar image numberkruncher · Nov 26, 2013 at 04:02 PM 2
Share

At line #30 in the source of your question (immediately above if (ObjectInteractable(gObject ... please insert the following and let me know the results since this might help to highlight the cause of your problem:

 Debug.Log(gObject.name);

Generally I would have a separate script attached to the interactable objects. For instance:

 foreach (var collider in Physics.OverlapSphere(transform.position, 10.0f)) {
     var interactable = collider.GetComponent<InteractableObject>();
     if (interactable == null)
         continue;

     interactable.Interact(m_forcePoints);
 }
avatar image PhilRousse · Nov 26, 2013 at 04:51 PM 2
Share

Is the "Player" have is skeleton as a child or any children? That would explain some of your result. Because these gameobject are picked by your sphere.

If it's possible, I would suggest that answer by @numberkruncher

 foreach (var collider in Physics.OverlapSphere(transform.position, 10.0f)) {
     var interactable = collider.GetComponent<InteractableObject>();
     if (interactable == null)
         continue;
 
     interactable.Interact(m_forcePoints);
 }

Adding an InteractableObject script is probably the best way to handle this and allow easier implementation of futur functionality

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by numberkruncher · Nov 27, 2013 at 12:27 AM

Scripts for interactable objects

Generally I would have a separate script attached to the interactable objects. For instance:

 foreach (var collider in Physics.OverlapSphere(transform.position, 10.0f)) {
     var interactable = collider.GetComponent<InteractableObject>();
     if (interactable == null)
         continue;
 
     interactable.Interact(m_forcePoints);
 }

Eliminate objects within character hierarchy

Or, to eliminate objects within the hierarchy of your character you could using something like the following:

 public bool IsPlayerObject(Transform t) {
     var playerTransform = transform;
     while (t != null) {
         if (t == playerTransform)
             return true;
         t = t.parent;
     }
     return false;
 }

And then:

 if (IsPlayerObject(m_pullObjects[i].transform))
     continue;

Other than that, I am really unsure as to how I can help...

Comment
Add comment · Show 1 · 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 Daniel G · Nov 27, 2013 at 01:38 AM 0
Share

Sorry my internet was down! Excellent answer thank you!!

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

17 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 avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Using Conditional Compilers? 1 Answer

Distribute terrain in zones 3 Answers

Unity and Leap Motion: Help with Jerkiness of input! 1 Answer

iTween Visual Editor Event Call from C# to Javascript 1 Answer


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