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
1
Question by DayyanSisson · Jun 25, 2012 at 06:42 PM · meshmoveverticesboundsmesh-deformation

Mesh Deformation

I'm attempting to create a mesh deformation script. My approach to this is to access each vertice in an object and then move it a certain amount based off its distance from the colliding object and the direction its being hit from. This is what I tried (the script is on the projectile that is supposed to be deforming the mesh that its hitting):

 void OnCollisionEnter (Collision collision) {
 
         Mesh mesh = collision.transform.GetComponent<MeshFilter>().mesh;
         Vector3[] vertices = mesh.vertices;
 
         int i = 0;
         while (i < vertices.Length) {
             float distance = Vector3.Distance(vertices[i], collision.transform.position);
             if(distance > hitRadius) return;
             float vertMove = distance/hitRadius;
             Vector3 localDirection = transform.position - collision.transform.position;
             vertices[i] += (localDirection * vertMove);
             i++;
         }
         mesh.vertices = vertices;
            mesh.RecalculateBounds();
     }

The problem is that it doesn't do anything until the hitRadius is larger than the object, and then on collision, it moves the whole object. What am I doing wrong here?

EDIT

Okay so after doing some research on the forums, BigMisterB wrote this script (corrected by me):

 using UnityEngine;
 using System.Collections;
 
 public class MeshDeform : MonoBehaviour {
 
     public Vector3 relativeVelocity;
 
     void Update () {
 
         transform.Translate(relativeVelocity * Time.deltaTime);
     }
 
     void OnCollisionEnter (Collision collision) {
 
         MeshFilter mf = collision.collider.GetComponent<MeshFilter>();
         Mesh mesh = mf.mesh;
 
         Vector3[] vertices = mesh.vertices;
         Vector3 hitPoint = transform.InverseTransformPoint(collision.contacts[0].point);
         float hitRadius = relativeVelocity.magnitude;
         Vector3 hitDir = transform.InverseTransformDirection(-collision.contacts[0].normal);
 
         int i = 0;
         while (i < vertices.Length) {
             float distance = Vector3.Distance(vertices[i], hitPoint);
             Vector3 dir = (vertices[i] - hitPoint);
             if(dir.magnitude < hitRadius){
                 float amount = 1 - dir.magnitude / hitRadius;
                 Vector3 vertMove = hitDir * amount;
                 vertices[i] += vertMove;
             }
             i++;
         }
 
         mesh.vertices = vertices;
         mesh.RecalculateBounds();
     }
 }

So the script works now, but unfortunately the wrong vertices are being affected. For some reason, when I hit the object, the vertices completely opposite of the vertices that are hit are affected. Any ideas why that happens?

Comment
Add comment · Show 1
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 DayyanSisson · Jun 25, 2012 at 06:52 PM 0
Share

Oh and the projectile is a cube and the object being hit is a primitive sphere object.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Jun 25, 2012 at 08:46 PM

 if(distance > hitRadius * speed) return;

I doubt that's what you meant. Presumably "continue" rather than "return". By the way, it would be a good idea to cache collision.transform.position, also transform.position. Additionally, you could optimize somewhat by using (a-b).sqrMagnitude (and the square distance) rather than Vector3.Distance.

Comment
Add comment · Show 3 · 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 DayyanSisson · Jun 26, 2012 at 12:16 AM 0
Share

Well if the distance of the vertex is larger than the hitRadius than the vertex isn't inside the area of effect, therefore doesn't need to be effected. I have no idea why I put hitRadius * speed, so I corrected it in the code for the question. But basically if the vertex isn't in the area of effect, than there's no need to continue with the rest of the code.

avatar image Eric5h5 · Jun 26, 2012 at 12:48 AM 1
Share

@nighthawx349: Using "return" means it quits the function entirely. That's not what you want.

avatar image DayyanSisson · Jun 26, 2012 at 01:46 AM 0
Share

Oh.......that explains.....way too much.....

avatar image
1

Answer by imDanOush · Jun 21, 2015 at 03:11 PM

I'm not sure but maybe if you use "transform.TransformPoint()" instead of "transform.InverseTransformPoint()", it'd be working correctly.

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

Edit SkinnedMeshRenderer 1 Answer

Flattening a mesh by deforming it 0 Answers

How do you bend a cylindrical gameobject? 1 Answer

Getting mesh length (x,z) 1 Answer

Mesh deformation simulating a fire 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