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 /
avatar image
0
Question by willianjohns · Sep 12, 2017 at 11:34 AM · collisiongameobjectmeshboxcolliderfootsteps

Add collision to mesh

Hello, I have a footprints script, but I have a problem! all the created footprints saw a mesh only, okay! But I wanted to have a collider, like an inimo creates the footprints and when the player touches it it loses life for example! But when I add a boxcollider in gameobject footprints that belongs to the footprints script, the boxcollider is only to the gameobject, not in the mesh of the footprints, remembering that as it creates footprints it becomes a single mesh, but the boxcollider is only in position 0,0,0 that belongs to the gameobject. How do I assign a box collider to the mesh of the footprints so that when I collide in them something happens?

Here is the script:

Footprints

 #pragma strict
  @script RequireComponent( MeshFilter, MeshRenderer )
  
  
  public var maxFootprints : int = 256; // Maximum number of footprints total handled by one instance of the script.
  public var footprintSize : Vector2 = Vector2( 0.4, 0.8 ); // The size of the footprint. Should match the size of the footprint that it is used for. In meters.
  public var footprintSpacing : float = 0.3; // the offset for the left or right footprint. In meters.
  public var groundOffset : float = 0.02;    // The distance the footprints are places above the surface it is placed upon. In meters.
  
  public var terrainLayer : LayerMask; // the layer of the terrain, so the footprint raycast is only hitting the terrain.
  
  
  private var mesh : Mesh;
  
  private var vertices : Vector3[];
  private var normals : Vector3[];
  private var uvs : Vector2[];
  private var triangles : int[];
  
  private var footprintCount : int = 0;
  
  private var isLeft : boolean = false;
  
  
  // Initializes the array holding the footprint sections.
  function Awake()
  {
      // - Initialize Arrays -
      
      vertices = new Vector3[ maxFootprints * 4 ];
      normals = new Vector3[ maxFootprints * 4 ];
      uvs = new Vector2[ maxFootprints * 4 ];
      triangles = new int[ maxFootprints * 6 ];
      
      // - Initialize Mesh -
      
      if ( GetComponent( MeshFilter ).mesh == null )
      {
          GetComponent( MeshFilter ).mesh = new Mesh();
      }
      
      mesh = GetComponent( MeshFilter ).mesh;
      
      mesh.name = "Footprints_Mesh";
  }
  
  
  // Function called by the Player when adding a footprint. 
  // Adds the information needed to create the mesh later. 
  public function AddFootprint( pos : Vector3, fwd : Vector3, rht : Vector3, footprintType : int )
  {
      // - Calculate the 4 corners -
      
      // foot offset
      var footOffset : float = footprintSpacing;
      
      if ( isLeft )
      {
          footOffset = -footprintSpacing;
      }
      
      var corners : Vector3[] = new Vector3[ 4 ];
      
      // corners = position + left/right offset + forward + right
      corners[ 0 ] = pos + ( rht * footOffset ) + ( fwd * footprintSize.y * 0.5 ) + ( -rht * footprintSize.x * 0.5 ); // Upper Left
      corners[ 1 ] = pos + ( rht * footOffset ) + ( fwd * footprintSize.y * 0.5 ) + ( rht * footprintSize.x * 0.5 ); // Upper Right
      corners[ 2 ] = pos + ( rht * footOffset ) + ( -fwd * footprintSize.y * 0.5 ) + ( -rht * footprintSize.x * 0.5 ); // Lower Left
      corners[ 3 ] = pos + ( rht * footOffset ) + ( -fwd * footprintSize.y * 0.5 ) + ( rht * footprintSize.x * 0.5 ); // Lower Right
      
      
      // raycast to get the position and normal for each corner
      var hit : RaycastHit;
      
      for ( var i : int = 0; i < 4; i ++ )
      {
          var rayPos : Vector3 = corners[ i ];
          rayPos.y = 1000.0;
          
          if ( Physics.Raycast( rayPos, -Vector3.up, hit, 2000.0, terrainLayer ) ) // also add a layermask for the terrain
          {
              var index : int = ( footprintCount * 4 ) + i;
              
              // - Vertex -
              vertices[ index ] = hit.point + ( hit.normal * groundOffset );
              
              // - Normal -
              normals[ index ] = hit.normal;
              
          }
      }
      
      
      // - UVs -
      
      // what type of footprint is being placed
      var uvOffset : Vector2;
      
      switch( footprintType )
      {
          case 1 :
              uvOffset = Vector2( 0.5, 1.0 );
          break;
          
          case 2 :
              uvOffset = Vector2( 0.0, 0.5 );
          break;
          
          case 3 :
              uvOffset = Vector2( 0.5, 0.0 );
          break;
          
          default :
              uvOffset = Vector2( 0.0, 1.0 );
          break;
      }
      
      // is this the left foot or the right foot
      switch( isLeft )
      {
          case true :
              uvs[ ( footprintCount * 4 ) + 0 ] = Vector2( uvOffset.x + 0.5, uvOffset.y );
              uvs[ ( footprintCount * 4 ) + 1 ] = Vector2( uvOffset.x, uvOffset.y );
              uvs[ ( footprintCount * 4 ) + 2 ] = Vector2( uvOffset.x + 0.5, uvOffset.y - 0.5);
              uvs[ ( footprintCount * 4 ) + 3 ] = Vector2( uvOffset.x, uvOffset.y - 0.5 );
              
              isLeft = false;
          break;
          
          case false :
              uvs[ ( footprintCount * 4 ) + 0 ] = Vector2( uvOffset.x, uvOffset.y );
              uvs[ ( footprintCount * 4 ) + 1 ] = Vector2( uvOffset.x + 0.5, uvOffset.y );
              uvs[ ( footprintCount * 4 ) + 2 ] = Vector2( uvOffset.x, uvOffset.y - 0.5 );
              uvs[ ( footprintCount * 4 ) + 3 ] = Vector2( uvOffset.x + 0.5, uvOffset.y - 0.5);
              
              isLeft = true;
          break;
      }
      
      
      
      // - Triangles -
      
      triangles[ ( footprintCount * 6 ) + 0 ] = ( footprintCount * 4 ) + 0;
      triangles[ ( footprintCount * 6 ) + 1 ] = ( footprintCount * 4 ) + 1;
      triangles[ ( footprintCount * 6 ) + 2 ] = ( footprintCount * 4 ) + 2;
      
      triangles[ ( footprintCount * 6 ) + 3 ] = ( footprintCount * 4 ) + 2;
      triangles[ ( footprintCount * 6 ) + 4 ] = ( footprintCount * 4 ) + 1;
      triangles[ ( footprintCount * 6 ) + 5 ] = ( footprintCount * 4 ) + 3;
      
      
      // - Increment counter -
      footprintCount ++;
      
      if ( footprintCount >= maxFootprints )
      {
          footprintCount = 0;
      }
      
      // - update mesh with new info -
      ConstructMesh();
  }
  
  
  function ConstructMesh() 
  {
      mesh.Clear();
      
      mesh.vertices = vertices;
      mesh.normals = normals;
      mesh.triangles = triangles;
      mesh.uv = uvs;
  }

PlayerFootprints:

 #pragma strict
  
  public var footprints : Footprints;
  
  public var footprintSpacing : float = 2.0; // distance between each footprint
  
  private var lastPos : Vector3 = Vector3.zero;
  
  
  function Start() 
  {
      lastPos = transform.position;
      
      if ( !footprints )
      {
          footprints = GameObject.Find( "Footprints" ).GetComponent( Footprints );
      }
  }
  
  
  function Update() 
  {
      var distFromLastFootprint : float = ( lastPos - transform.position ).sqrMagnitude;
      
      if ( distFromLastFootprint > footprintSpacing * footprintSpacing )
      {
          // AddFootprint( pos : Vector3, fwd : Vector3, rht : Vector3, footprintType : int )
          //footprints.AddFootprint( transform.position, transform.forward, transform.right, 0 );
          footprints.AddFootprint( transform.position, transform.forward, transform.right, Random.Range( 0, 4 ) );
          
          lastPos = transform.position;
      }
  }
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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

How to get parent with component on collision? 0 Answers

Error CS1519 1 Answer

How can I implement box collider or mesh collider in this code? 0 Answers

Spawning only 1 gameObject 3 Answers

Child gameobject moving position after collision 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