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 /
avatar image
0
Question by migaligil · Apr 15, 2020 at 11:06 AM · scripting problemterrainerror messagefootsteps

Error on script for detecting the terrain texture

Hey everyone. I'm new to Unity an I'm having problems with this script for detecting the terrain texture under the player. I''m using the script that I've found here: https://gamedevbeginner.com/terrain-footsteps-in-unity-how-to-detect-different-textures/ When I run the game I get error: T exture rectangle is out of bounds (824 + 1 > 512) UnityEngine.TerrainData:GetAlphamaps(Int32, Int32, Int32, Int32)

(From what I can tell the 824 is the posZ and the 512 is one of the map dimensions.) the error is on this line: float[,,] aMap = t.terrainData.GetAlphamaps(posX, posZ, 1, 1); the script looks like this:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CheckTerrainTexture : MonoBehaviour
 {
 
     public Transform playerTransform;
     public Terrain t;
     public float mapx = 0;
     public float mapy = 0;
     public int posX;
     public int posZ;
     public float[] textureValues;
 
     void Start()
     {
         t = Terrain.activeTerrain;
         playerTransform = gameObject.transform;
     }
 
     void Update()
     {
         // For better performance, move this out of update 
         // and only call it when you need a footstep.
         GetTerrainTexture();
      mapx= t.terrainData.alphamapWidth;
      mapy= t.terrainData.alphamapHeight;
     }
 
     public void GetTerrainTexture()
     {
         ConvertPosition();
         CheckTexture();
     }
 
     void ConvertPosition()
     {
         Vector3 terrainPosition = playerTransform.position - t.transform.position;
 
         Vector3 mapPosition = new Vector3
         (terrainPosition.x / t.terrainData.size.x, 0,
         terrainPosition.z / t.terrainData.size.z);
 
         float xCoord = mapPosition.x * t.terrainData.alphamapWidth;
         float zCoord = mapPosition.z * t.terrainData.alphamapHeight;
 
         posX = (int)xCoord;
         posZ = (int)zCoord;
     }
 
     void CheckTexture()
     {
         float[,,] aMap = t.terrainData.GetAlphamaps(posX, posZ, 1, 1);
         textureValues[0] = aMap[0, 0, 0];
         textureValues[1] = aMap[0, 0, 1];
         textureValues[2] = aMap[0, 0, 2];
         textureValues[3] = aMap[0, 0, 3];
     }
 }

Can someone please help me fix this problem?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Pitic · May 12, 2020 at 03:17 PM

Heres my code, i fixed it, the problem you have is that you are not on the terrain, therefore it cant see any terrain texture below you, heres my code i have fixed the issues and added sounds :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CheckTerrainTexture : MonoBehaviour {
 
 public Transform playerTransform;
 public Terrain t;
 
 public int posX;
 public int posZ;
 public float[] textureValues;
 
 public AudioSource source;
 
 public AudioClip[] stoneClips;
 public AudioClip[] dirtClips;
 public AudioClip[] sandClips;
 public AudioClip[] grassClips;
 
 AudioClip previousClip;
 
 void Start () 
   {
     t = Terrain.activeTerrain;
     playerTransform = gameObject.transform;
   }
 
 void Update()
   {
     // For better performance, move this out of update 
     // and only call it when you need a footstep.
     GetTerrainTexture();
   }
 
 public void GetTerrainTexture()
   {
     ConvertPosition(playerTransform.position);
     CheckTexture();
   }
 
 void ConvertPosition(Vector3 playerPosition)
   {
     Vector3 terrainPosition = playerPosition - t.transform.position;
 
     Vector3 mapPosition = new Vector3
     (terrainPosition.x / t.terrainData.size.x, 0,
     terrainPosition.z / t.terrainData.size.z);
 
     float xCoord = mapPosition.x * t.terrainData.alphamapWidth;
     float zCoord = mapPosition.z * t.terrainData.alphamapHeight;
 
     posX = (int)xCoord;
     posZ = (int)zCoord;
   }
 
 void CheckTexture()
   {
     float[,,] aMap = t.terrainData.GetAlphamaps (posX, posZ, 1, 1);
     textureValues[0] = aMap[0,0,0];
     textureValues[1] = aMap[0,0,1];
     textureValues[2] = aMap[0,0,2];
     textureValues[3] = aMap[0,0,3];
   }
   public void PlayFootstep()
 {   
   GetTerrainTexture();
 
   if (textureValues[0] > 0)
   {
     source.PlayOneShot(GetClip(stoneClips), textureValues[0]);
   }
   if (textureValues[1] > 0)
   {
     source.PlayOneShot(GetClip(dirtClips), textureValues[1]);
   }
   if (textureValues[2] > 0)
   {
     source.PlayOneShot(GetClip(dirtClips), textureValues[2]);
   }
   if (textureValues[3] > 0)
   {
     source.PlayOneShot(GetClip(dirtClips), textureValues[3]);
   }
 }
 
 AudioClip GetClip(AudioClip[] clipArray)
   {
     int attempts = 3;
     AudioClip selectedClip = 
     clipArray[Random.Range(0, clipArray.Length - 1)];
 
     while (selectedClip == previousClip && attempts > 0)
       {
         selectedClip = 
         clipArray[Random.Range(0, clipArray.Length - 1)];
         
         attempts--;
       }
 
     previousClip = selectedClip;
     return selectedClip;
 
   }
 }

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

Answer by migaligil · May 13, 2020 at 08:23 AM

Thank you very much! It works :)

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

246 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 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

Random: NullReferenceException: Object reference not set to an instance of an object 2 Answers

"CheckDisalowAllocation" Error using ShaderGraph 0 Answers

Can't access rigidbody commands 'Rigidbody' does not contain a definition for... 2 Answers

Cannot paint textures onto terrain, errors are raised 0 Answers

Can scripts be attached to speedtrees? 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