• 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 Dr. Watson · Sep 22, 2010 at 06:47 PM · collisionraycastscaling

Runtime scaling allows me to go through walls...

Hi. I'm working on a project right now and the main ability of the player is being able to grow and shrink whenever. I have no issue doing this BUT... if I grow while next to a wall my guy will go through the wall. I've tried using a raycast but I have objects in my game that have trigger colliders on them and the raycast detects the trigger. I also tried making the raycast ignore the trigger but it's attached to an object I want the player to collide with. So, if i make the raycast ignore that layer it will ignore the mesh I want the player to interact with... Please help. Thanks.

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
3
Best Answer

Answer by skovacs1 · Sep 22, 2010 at 08:02 PM

Your situation is such that you have some object (player or whatever) that can be scaled at runtime. When next to objects, this causes penetration as scaling will properly resize the collider of this object, but will not generate collisions. The only way I know of to resolve this is to perform a raycast and offset.

You seem to be confused about raycasts or when you're doing them. You have objects with triggers colliders and objects with regular colliders, but that really doesn't mean anything to your problem, so consider them all as colliders just the same. To restate your problem in simpler terms, you have a number of objects with colliders and you only want to raycast against some of them. This can be done either with layers or on an object by object case.

I'm sure somewhere in the 90+ questions tagged raycast, there's an answer to this, but I don't blame you for not finding it.

To raycast only against certain layers, you would supply a layermask. Different raycasts can use different layermasks. Different objects (even children and parents) can be on different layers. If you simply place the things you don't want to cast against in certain raycasts on different layers than those you wish to cast against in those raycasts, you'll be fine.

Say for example, I have a walls with colliders on Layer 9 (collision geo) and sound trigger colliders on Layer 10 (sound zones) and desks with colliders on layer 11 (interactive collision geo) and interactive zones for the desks on Layer 12 (interaction zones). If I want to shoot a gun , but not go through the walls or desks, I could raycast to those objects I wouldn't want to pass through, but not the interactive zones. So I would create a layer mask.

var mask : int = (1 << 9) + (1 << 11); //only layers 9 and 11 var range : float = 60; var hit : RaycastHit; var someDirection : Vector3; if(Physics.Raycast (transform.position, someDirection, hit, range, layerMask)) { //do something }

//somewhere else //raycast against everything if(Physics.Raycast(transform.position, someOtherDirection, hit)) { //do something }

If this is not quite what you want, you may consider raycasting to the objects of importance individually, but this is a bit more expensive. You'll likely want to tag your objects for this purpose and at the very least only cast against objects within a set radius.

var newSize = 10; //get the diameter of the size you'll be scaling to somehow
var hit : RaycastHit;
var radius = newSize / 2;
var sqrRadius = radius * radius;
var surfaces = GameObject.FindGameObjectsWithTag ("Collision Surface");
for(surface : GameObject in surfaces) {
    var surfaceVector : Vector3 = surface.transform.position - transform.position;
    if(surfaceVector.sqrMagnitude < sqrRadius &&
       surface.collider.Raycast(new Ray(transform.position, surfaceVector), hit, radius))
        //offset the character
}

One thing to consider is that any offset you apply will upset the results from your raycasts because you will have just moved and you would then need to cast again to ensure that your offset didn't put you through a different object. This is where you would need to design your world and player abilities to prevent problems where you have a character being scaled into the walls on opposite sides.

Something like:

function checkWalls(size : float) : Vector3 { var hit : RaycastHit; var radius = newSize / 2; var sqrRadius = radius * radius; var surfaces = GameObject.FindGameObjectsWithTag ("Collision Surface"); for(surface : GameObject in surfaces) { var surfaceVector : Vector3 = surface.transform.position - transform.position; if(surfaceVector.sqrMagnitude < sqrRadius && surface.collider.Raycast(new Ray(transform.position, surfaceVector), hit, radius)) return -surfaceVector.normalized * (radius - hit.distance); } return Vector3.zero; }

var maxScaleRebouds = 3;

function Update() { //some stuff to tell you you're scaling... var offset = checkWalls(newSize); int i = 0; for(; i < maxScaleRebounds && offset != Vector3.zero; i++) { transform.position += offset; //This may change depending how you're moving offset = checkWalls(newSize); } if(i == maxScaleRebouds && offset != Vector3.zero) Debug.Warning("Max scale rebound reached and still inside a surface. Fix it!"); }

Remember only to do your scaling raycasting when you're scaling up (and maybe down, depending on your use case).

Comment
Add comment · Show 4 · 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 Dr. Watson · Sep 22, 2010 at 08:16 PM 0
Share

I can give you a better example of what's going on if you go to www.wyldstallynsgames.com then in Development Journal watch update 2 you will see what I'm trying to achieve. I got that you can set the ignore layers but I ran into a problem with my vacuums because when you get in the suction area(trigger collider that's attached to the parent of all aspects of the vacuum) it moves you based on the vacuum's position/direction. So if I put the vacuum's layer to ignore raycasts, which has the trigger collider and the mesh I still want the player to interact with, won't the raycast ignore the mesh?

avatar image Dr. Watson · Sep 22, 2010 at 08:17 PM 0
Share

Thanks for the beefy response XD

avatar image skovacs1 · Sep 23, 2010 at 01:57 PM 0
Share

I never said anything about the ignore raycast layer. I said to use a layer mask or to raycast selectively. In my layer mask example, the vacuums would be like my Layer 12 against which I do not raycast in that example, but could easily raycast against in subsequent raycasts by using a mask that included that layer or without using a mask at all. A layer mask only affects the raycast to which you pass it. The raycast for the scaling would ignore the triggers because of the mask while any other raycast without the mask would not.

avatar image Dr. Watson · Sep 23, 2010 at 03:32 PM 0
Share

Ok thanks I'll see if that will do the trick!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Physics.SphereCast please help understand 0 Answers

How to detect which side is the raycast hitting? 2 Answers

My Raycast acts like it's hitting something when it isn't 0 Answers

Align to grid in runtime problem 1 Answer

Ignore NullReferenceException error? 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