So, i've wanted to do a hole.io game copy just for me . I've writen the code, but when i'm trying to move the hole, the entire circle moves so fast that i can barely see. This is the scene: And this is the code: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class movement : MonoBehaviour { [Header(" Detection ")] public float detectionRadius; [Header(" Mesh ")] public MeshFilter filter; public MeshCollider meshCollider; Mesh mesh; List<int> vertIndexes; List<Vector3> offsets; float hajm = 1;//make the hole bigger and smaller void Start() { vertIndexes = new List<int>(); offsets = new List<Vector3>(); mesh = filter.mesh; for (int i = 0; i < mesh.vertices.Length; i++) { float distance = Vector3.Distance(Vector3.zero, mesh.vertices[i]); //Debug.Log(distance); if (distance < detectionRadius) { vertIndexes.Add(i); offsets.Add(mesh.vertices[i] - transform.position); } } } void Update() { Vector3[] vertices = mesh.vertices; for (int i = 0; i < vertIndexes.Count; i++) { //vertices[vertIndexes[i]].y -= Time.deltaTime; vertices[vertIndexes[i]] = transform.position + offsets[i] * hajm; } mesh.vertices = vertices; filter.mesh = mesh; meshCollider.sharedMesh = mesh; if (Input.GetKey(KeyCode.S)) hajm += 0.1f; else if (Input.GetKey(KeyCode.D)) hajm -= 0.1f; } private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, detectionRadius); } } I've created a sphere that will move in RunTime, and restore the mesh, but that didn't worked as planed. I'm just a 9th class student who likes 3d modeling and unity so please have patience. Also srry for any mistaakes. Thanks!
Welcome to to world of game development and working with Unity! A note on forum etiquette: in the future, don't post screenshots of code, copy the text of the code and place it in code tags in your post. Screenshots can be hard to read for different people on different devices, and image hosting sites usually don't host the images forever, which can break the post for people years down the line. https://forum.unity.com/threads/using-code-tags-properly.143875/ One problem I notice is in your start method, when you are calculating offsets, you are checking the distance from a mesh vertex to your transform position. A vertex from a mesh is a point *relative to that object's transform*, while your transform position is a point *relative to the world origin*. We would say "the vertex is specified in object-space" and "position is in world-space". Because those two vectors are in different spaces, computing the distance between them doesn't work; the Distance method assumes they are in the same space and calculates the distance in that space. To see that, place your game object at (0,0,0) and Debug.Log the distance on line 27, after you calculate. Play the game and note the values. Then, move the object to (10,0,0) and play again. You should see that the distances aren't the same. To fix that, you need to convert the vectors you are comparing into the same space. In your case, you want to convert into local space, since that's what the vertices are already in. Fortunately, converting transform.position into the transform's local space is easy: it's (0,0,0). By definition, the transform's position is the origin of its local space. So now, you would write float distance = Vector3.Distance(Vector3.zero, meshVerticies[i]);. This can be simplified to just float distance = meshVerticies[i].magnitude;. The magnitude of a vector is just the length of the vector, which is also a point's distance from the origin. If you do the distance check test again, starting at (0,0,0) and trying again at (10,0,0), you should see that the results are the same each time now.
Still working on this. I've change the part you saaid and other ones, and still have the same problem. I even remodeled the circle in blender, and nothing happens. The hole is moving with the circle...This seem tto be tthe biggestt problem i,ve encountered so far.
If you look at lines 34 and 49, those have the same space-mismatch problem. transform.position is in world-space, but the vertices are in local-space. I think I made a mistake. I thought that the mesh was on the same object as the movement script, but that looks like it isn't true. In that case, you still need to do a space change for the transform position, but it isn't as simple as before. Instead of transforming it into its own local space, you have to transform it into the mesh's local space (which are not the same). You can do that by using Transform.InverseTransformPoint. So, distance becomes Code (csharp): Vector3 positionInMeshSpace = filter.transform.InverseTransformPoint(transform.position); float distance = Vector3.Distance(positionInMeshSpace, meshVerticies[i]);
So, i've changed out the code, I remodeled the shape, and now the movement is nice and linear. I'm now able to use the mouse cursor input. But...the hole isn't moving. When i try to move the hole, the circle startts to move too. Here is the new code. Sorry for annoying ya, but I'm a self taught, and I try to learn as much as I can. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class movement : MonoBehaviour { [Header(" Detection ")] public float detectionRadius; [Header(" Mesh ")] public MeshFilter filter; public MeshCollider meshCollider; Mesh mesh; List<int> vertIndexes; List<Vector3> offsets; float hajm = 1; void Start() { vertIndexes = new List<int>(); offsets = new List<Vector3>(); mesh = filter.mesh; Vector3 positionInMeshSpace = filter.transform.InverseTransformPoint(transform.position); for (int i = 0; i < mesh.vertices.Length; i++) { float distance = Vector3.Distance(positionInMeshSpace, mesh.vertices[i]); //Debug.Log(distance); if (distance < detectionRadius) { vertIndexes.Add(i); offsets.Add(mesh.vertices[i] - positionInMeshSpace); } } } void Update() { Vector3[] vertices = mesh.vertices; Vector3 positionInMeshSpace = filter.transform.InverseTransformPoint(transform.position); for (int i = 0; i < vertIndexes.Count; i++) { //vertices[vertIndexes[i]].y -= Time.deltaTime; vertices[vertIndexes[i]] = positionInMeshSpace + offsets[i] * hajm; } mesh.vertices = vertices; filter.mesh = mesh; meshCollider.sharedMesh = mesh; if (Input.GetKey(KeyCode.S)) hajm += 0.1f; else if (Input.GetKey(KeyCode.D)) hajm -= 0.1f; } private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, detectionRadius); } }
Do you mean if you hold the S or D key, it doesn't do anything? Otherwise, I don't see any movement code here.
Now it does, but what i'm triyng to do is creating a plane, spawn random objects and be able to move just the hole not tthe entire plane and those object fall right thru. Here is a awfull explanation: After i move the hole the gap dissapear in that place. But my problem is the hole circle moves....thx btw for all the info you gave me!
How big is your detectionRadius? It might be that you are detecting all the vertices and then moving everything instead of just the inner ones.
Ok, can you post your movement code? I can't see a problem with this part of the code (although that doesn't mean there isn't one!)
Well: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class movement : MonoBehaviour { [Header(" Detection ")] public float detectionRadius; [Header(" Mesh ")] public MeshFilter filter; public MeshCollider meshCollider; Mesh mesh; List<int> vertIndexes; List<Vector3> offsets; float hajm = 1; void Start() { vertIndexes = new List<int>(); offsets = new List<Vector3>(); mesh = filter.mesh; Vector3 positionInMeshSpace = filter.transform.InverseTransformPoint(transform.position); for (int i = 0; i < mesh.vertices.Length; i++) { float distance = Vector3.Distance(positionInMeshSpace, mesh.vertices[i]); //Debug.Log(distance); if (distance < detectionRadius) { vertIndexes.Add(i); offsets.Add(mesh.vertices[i] - positionInMeshSpace); } } } void Update() { Vector3[] vertices = mesh.vertices; Vector3 positionInMeshSpace = filter.transform.InverseTransformPoint(transform.position); for (int i = 0; i < vertIndexes.Count; i++) { //vertices[vertIndexes[i]].y -= Time.deltaTime; vertices[vertIndexes[i]] = positionInMeshSpace + offsets[i] * hajm; } mesh.vertices = vertices; filter.mesh = mesh; meshCollider.sharedMesh = mesh; if (Input.GetKey(KeyCode.S)) hajm += 0.1f; else if (Input.GetKey(KeyCode.D)) hajm -= 0.1f; } private void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, detectionRadius); } } this is the only code in the project. I've created an empty gameobject and asigned this(that red sphere). Then created that plane(I've enabled read mesh setting) and asigned the plane to the filter and mesh collider(you can see in a picture above).I don't know what I'm doing wrong. This is what i want. a see thru hole thaat moves
Oh I see, you're using the transform's gizmos. In that case, can you tell if the pivot point of the ground plane is moving? That would mean that its transform is moving. If it isn't, then we're moving all the vertices.
when i'm moving the sphere in play mode ,the pivot point of the plane remains the same, even if i move the sphere to god knows where. Edit: when i'm moving the pivot point of the plane in play mode, the plane doesn't moves.
Ok, that means that we're moving all the vertices, despite what it might seem that detectionRadius is saying. Hmm, it may be related to the model scale of the circle. I see that the transform scale of the ground circle is 100, which makes me think that you are using Maya to make your models and that the import scale is something like 0.01. Try clicking on the circle model in your project and change its file scale to something that lets you keep the transform scale at 1. Right now, what I think is happening is that a detection radius is actually detecting things within a radius of 100.
Try setting (in the inspector) detection radius to 0.01. If that works, then what you can do is set the plane transform's scale to 1, then select the model in the project (not the scene hierarchy) and change the Scale Factor to 100.
Omg...i have no words...it worked....i ve made the scale 1 and detrad 0.09. Ur such a smart guy...but now i have other questions. Can i make a square plane and add thickness aand the hole with boolean, aaand how can i scale it? And another q: how to invert the z axis with y?
Yes, but you have to be careful how you model it. It needs to have the hole in it, and all of the triangles have to be similar to the how they are in the circle; you can't have a grid of triangles, for example. Yes, try to do it on your own first and then come back if you can't make any progress. Now that you've set the scale factor to match Unity's default scale, you should be able to scale the ground plane normally. Previously, Unity was assuming the model was tiny, now it knows the right scale. The InverseTransformPoint call we make takes care of accounting for scale. The easiest thing to do is rotate the model in your modeling program and then re-export it. You might have to try a few times because maya and unity use different coordinate systems, but start by trying to put the "top" of your model along the -y axis in maya. I always forget the right way to rotate it and have to try again, lol.