The question is answered, right answer was accepted.
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
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.
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
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!
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?
Yes it is. You could also ustr a Box Collider but that might seem too cheap because it overshoots with it's corners.
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?
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 :)
It may sound scary but creating a circle in blender cant really be that hard :) And i really think this is the easiest way.
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. :)
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! :)
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.
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.
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.
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.
Thank you for the video, object and ring both face the same direction according to the blue arrow
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.
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?
Follow this Question
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