Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Dec 13, 2015 at 11:45 PM by Le-Pampelmuse for the following reason:

The question is answered, right answer was accepted.

avatar image
0
Question by Zythus · Dec 09, 2015 at 11:34 PM · unity 5colliderspherecollider

Changing depth of a SphereCollider without changing its size

I've been trying to change depth of a SphereCollider, so when the player goes through a checkpoint, it grabs it at the checkpoint position, not before it. Noone of the solutions from the web helped me yet and it's getting kind of frustrating. That's my code:

     void OnTriggerEnter (Collider other)
     {
         if (other.transform.tag == "Checkpoint")
         {
             SphereCollider sc = other.GetComponent<SphereCollider>();
             sc.transform.position = new Vector3(0, 0, 50);
             manager.AddCheckpoint();
             Destroy(other.gameObject);
         }
         if (other.transform.tag == "Goal")
         {
             Time.timeScale = 0f;
             manager.CompleteLevel();
         }
     }

So, what i was working with is basically this

 SphereCollider sc = other.GetComponent<SphereCollider>();
 sc.transform.position = new Vector3(0, 0, 50);

I tried to put some transform position in there, cause without it I get an error saying ,,cannot convert type Vector3 to Collider".

Any help would be appreciated

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 meat5000 ♦ · Dec 09, 2015 at 10:52 AM 0
Share

Try

 other.gameObject.GetComponent<SphereCollider>();
avatar image Zythus meat5000 ♦ · Dec 09, 2015 at 11:17 AM 0
Share

Did that, unfortunately doesn't change Collider's dimensions.

$$anonymous$$aybe I should put it somewhere else, that piece of code is in the OnTriggerEnter iteration, in a Player.cs that corresponds to the Game$$anonymous$$anager.

All I want is to make the Collider flat like a ring, not like a Sphere. I tried to use CircleCollider 2D, but it only triggers on it's borders, not when you go through inside of it.

avatar image FirePlantGames meat5000 ♦ · Dec 10, 2015 at 05:15 AM 0
Share

All I want is to make the Collider flat like a ring, not like a Sphere.

Why?

All I want is to make the Collider flat like a ring, not like a Sphere. I tried to use CircleCollider 2D, but it only triggers on it's borders, not when you go through inside of it.

2D physic Colliders are only used to collide against other 2d colliders

avatar image Le-Pampelmuse · Dec 10, 2015 at 12:12 AM 2
Share

If you want the checkpoint to only fire as soon as the player's transform enters it and not when the player's collider touches the checkpoint trigger, then just check

 Example code:
 if(Vector3.Distance (checkpoint.position, player.position) < checkpoint.radius)
 {}

However what seems to be totally different is your reply:

Did that, unfortunately doesn't change Collider's dimensions.

You can't change the dimensions of builtin colliders other than using the preset variables given in the inspector. SphereCollider would only have SphereCollider.radius.

All I want is to make the Collider flat like a ring

In this case just make a circle mesh inside any 3d program and use a mesh collider.

Hope this helps!

avatar image Zythus Le-Pampelmuse · Dec 10, 2015 at 02:31 PM 0
Share

Ok, I didn't explain it simply enough, sorry for that.

I've got checkpoints as rings, empty inside. $$anonymous$$ade the rings in the Blender, thrown them into Unity. Now, Player is supposed to get through the ring, and when he does it should increase Checkpoint count. Since SphereCollider is not flat like a ring, that ring is collected before Player passes through it. I want the ring to be collected at the same time as Player passes through it.

Would love it to be a simple fix, but as far as I can tell making a circle mesh inside Blender and using it as a mesh collider is my best option?

avatar image Le-Pampelmuse Zythus · Dec 10, 2015 at 03:48 PM 1
Share

Yes it is. You could also ustr a Box Collider but that might seem too cheap because it overshoots with it's corners.

Show more comments

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by JedBeryll · Dec 10, 2015 at 02:49 PM

"Would love it to be a simple fix, but as far as I can tell making a circle mesh inside Blender and using it as a mesh collider is my best option?" Yes. I think it's a pretty simple fix don't you?

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 Zythus · Dec 10, 2015 at 02:58 PM 0
Share

Well, sounds a bit scary since I used Blender in my life once. I meant simple fix, as in done in Unity, or by coding.

Thanks :)

avatar image JedBeryll Zythus · Dec 10, 2015 at 03:08 PM 0
Share

It may sound scary but creating a circle in blender cant really be that hard :) And i really think this is the easiest way.

avatar image Le-Pampelmuse Zythus · Dec 11, 2015 at 10:58 PM 3
Share

Check out Blender Primitives. Look at Cylinder.

Set the radius and height to match your ring mesh and the height.

Its really easy and you have to do nothing but type in numbers for how detailed ("smooth") the mesh should be.

You can do that inside your ring file so you have it automatically imported with the ring. Or make a new file with just the cylinder.

When imported and dragged into the scene, simply select the cylinder mesh and add component "$$anonymous$$esh Collider". You would also need to check "Is Trigger" like any other collider type.

It's the simplest, fastest and most guaranteed approach for this kind of task, that's why I recommended it in the first place.

A friendly advice:
Don't get caught up on solving non-coding problems with scripts.
That's over complicating things and stops your normal workflow. :)

avatar image Zythus Le-Pampelmuse · Dec 13, 2015 at 06:40 PM 0
Share

Today I had some time for that, sat down, created the cylinder, changed size and stuff, tried a couple of versions, with mesh collider here and there and finally got it to work.

It consists of 2 parts, ring which has a mesh collider of cylinder, and of cylinder itself, though with mesh renderer off.

Thank you all for help! :)

Show more comments
avatar image
0

Answer by tomhog · Dec 11, 2015 at 01:58 PM

Hi,

So what you want to do is when you get a trigger event, test how far the other object is from a plane running through the center of the the sphere. That way you know the collision is within the circle and then you know it's also close to or intersecting it's center cross section. Below is some simple code you could attach to the Checkpoint sphere with a sphere collider attached with isTrigger set to true

 using UnityEngine;
 using System.Collections;
 
 public class Checkpoint : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     void OnTriggerEnter (Collider other)
     {
         CheckForCollisionSimple(other);
     }
 
     void OnTriggerStay (Collider other)
     {
         CheckForCollisionSimple(other);
     }
 
     bool CheckForCollisionSimple(Collider other)
     {
         // simple method measures distance from a plane running through the center of the checkpoint facing it's forward axis, to the center of the other object
         float distanceToTrigger = 0.1f;
         Plane crossSectionPlane = new Plane(this.gameObject.transform.forward, this.gameObject.transform.position);
         if(crossSectionPlane.GetDistanceToPoint(other.transform.position) <= distanceToTrigger){
             Debug.Log("The other object is within tolerance");
             return true;
         }
         return false;
     }
 }

Once another object enters it calls CheckForCollisionSimple to see if it's close to the plane, the important part is to also implement OnTriggerStay as that is call continuously while the other collider is inside the trigger. Which you need as is might not pass through the center plane the instant it enters the trigger.

An exercise I leave for you is calculating when the first part of the other object reaches the center plane, at the moment we test the transform.position of the other object, but that might not be the tip of the players nose for example.

PS Remember the circle/ring you care about is facing the spheres forward/positive z axis as setup in the Plane constructor.

Comment
Add comment · Show 8 · 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 Zythus · Dec 11, 2015 at 07:06 PM 0
Share

I tried different setups with different radius with your script on, and with different distanceToTrigger and no matter what number I put in there, be it 0.1, 100 or -100, the checkpoint is always collected just when my object hits the radius. Am I doing something wrong here? I've got no clue, script looks simple, yet I have no idea what is going wrong here.

avatar image tomhog Zythus · Dec 11, 2015 at 07:09 PM 0
Share

What way is the sphere facing, remember this is a plane created at the center of the sphere with it normal facing the sphere transforms z axis. If you approach it from another axis the object will already be in the plane.

avatar image Zythus tomhog · Dec 11, 2015 at 07:15 PM 0
Share

I have an object which is moving forward, Z axis is increasing over time, from 130 up to about 180, that is where ring is collected, ring is in the air at Z axis 250, it's vertically positioned, so the object can go through a couple of these rings in a row.

$$anonymous$$ind you ring has a radius of 15, scale 2, 2, 3.

Show more comments
avatar image Zythus · Dec 11, 2015 at 07:49 PM 0
Share

Thank you for the video, object and ring both face the same direction according to the blue arrow

avatar image tomhog Zythus · Dec 11, 2015 at 07:57 PM 0
Share

Just imagine the zaxis/blue arrow runs through the hole in the ring.

The other object can face any direction as only it's position is used.

avatar image Zythus tomhog · Dec 11, 2015 at 08:44 PM 0
Share

Ok I can imagine it, and my setup has the same z axis as yours. I am going to try a bit more tomorrow.

But if you change sphere collider radius does it still work for you? Cause I can see it being small, like 0.5, so it's smaller than your sphere, and maybe that's why it catches it so late in the video?

Show more comments

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

i have a model with capsule collider on it and when i shoot it want to dead but it not working 0 Answers

Get Random transform point inside collider 0 Answers

CollisionEnter and Collision Exit won't work 2 Answers

Duplicate Remains After Destroying Object 1 Answer

Prefab is not the same as the object? 0 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