I have a plane whose scale is set to 1,1,1. I am willing to get its start position and end position. That what is the position at start of the object in world space and what is the end position.
One easy way to do this job is to place object in start and end of the plane and get object positions but i want to do it through the floor Only.
I just tried this code but its not bringin position in world space.
Debug.Log(floor.GetComponent<MeshRenderer>().bounds.min);
Debug.Log(floor.GetComponent<MeshRenderer>().bounds.max);
Debug.Log(floor.GetComponent<MeshRenderer>().bounds.center);
Debug.Log(floor.GetComponent<MeshRenderer>().bounds.extents);
Renderer.bounds will only match size of the plane if plane transform is aligned with the world. In other world it will only work if there isn't any rotation.
Another approach to get corners of the plane would be to look into the mesh vertices directly. This would require finding vertices on the corners first. Forget that.
Create 2 new game objects and place them on the corners of your plane
Parent two corner objects to plane object
Each corners transform.position will now give you world space position regardless of position, rotation or scale of the parent plane object.
Related
Most of you may know that moving object diagonally is faster than moving object horizontally or vertically. You can watch a short youtube tutorial here. If an object is set at (0,0) then using this code.transformation.postition += new Vector3(x, y, 0f).Assume moving object on 2D plane. When looking the object position at 2D grid(cartesian plane), for value x=1, y=0, it move one position right, for value x=0, y=1, it move one position up and for value x=1, y=1 it move one right and one up. When look at the following image, the line drawing diagonally is obviously longer than others. I read about vector, the magnitude of vector, Pythagorean theorem. Assume an object move one unit every frame. Then after 1 frame passed all objects will be moved 1 unit exactly. But when calculate in magnitude[length] of vector the units are different. I mean 1 in X or Y and around 1.4 in diagonal. Aren't object moving from point to point? Does Unity move object in vector's magnitude value and set the point on X and Y plane? How do object actually move?
Unity is a frame-based engine. So Unity calculates the positions where objects are in a frame, and when it comes to calculating the next frame, the positions are calculated there as well. One could assume that the object has moved between these frames and conclude that, for example, a projectile must have hit a thin object between the two frames.
If you want to move an object consistently in any direction, first clamp the direction with normalized and then add a custom magnitude.
How can I set rotation of the placed on vertical plane object to make its bottom side pararell to floor? At the moment vertically placed objects are sticky to the wall but have random rotation. I have been trying to set object on horizontal plane and then adjust vetical objects vector forward to horizontal objects vector up. That seems to work somehow but multiple objects apart of being pararell to each other are rotated in christmas tree shape. Important thing - I want to rotate it relative to real world, not to bottom of the screen.
What I want:
What I get:
VerticallyPlacedObject.transform.forward = HorizontallyPlacedObject.transform.up;
Screenshot:
I want to change the floor's vertex normals' direction as the ball is rolling on the floor. I just need some direction on how to achieve this.
So far this is the direction that I'm heading:
Make a copy of all the vertex normals of the floor on start.
On collision get the contact point and raycast/spherecast/boxcast to get the affected vertices. (Set variable offset to control how much vertices I want to be affected by the casting)
Find normals related to the vertices.
Rotate the affected normals parrallel to the ball's closest surface point.
As ball moves away from affected's floor's vertices, slowly return the floor normals back to original direction. (Set a variable to control the movement speed of the normal's rotating back to original direction)
I just need help figuring out which type of casting to use and how to rotate the normals parallel to the ball's surface. This is for a mobile platform so performance is a must.
Thanks in advance.
Here's how you'd go about modifying the normals:
Mesh mesh = GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
Vector3[] normals = mesh.normals;
You'd want to use the vertices list to figure out which indexes to modify (presumably also needing to convert from local space to world space). You could then raycast from the worldspace coordinate to the ball's center,1 and use the raycasthit.normal to figure out what the angle to the ball is.
Some clever vector math from there to figure out the new normal for your plane:
Find the vector perpendicular between hit.normal and Vector3.Up: this vector will be parallel to the plane. If the two vectors are parallel, dump out: your normal is unchanged (or should be returned to its original value, which will be the same vector as the raycast to find the sphere).
Find the vector perpendicular to that vector and hit.normal: this vector will be your new normal.
1 Actually, you'll want to know how far down from the ball's center you should target, otherwise, you'll get the most extreme offsets as the ball moves farther away from the plane. So you want the ball's position on X and Z, but a fixed offset up from the plane for Y. This won't be too difficult to calculate.
I would try something like:
Create a texture for the normals. Each pixel is a normal of a vertex(like a grid). Calculate the correspoding coord between the 3d ball and the position of the normal in the texture and draw a ball/circle/sprite on it (like a sprite) each frame. Then you could use a compute shader to revert them slowy to the default up vector.
Plane object position to Plane GameObject position.
Basically I've created a plane based on three points. But, for testing reasons I need to know where the Plane is. So, I've got a Plane GameObject (that comes with a mesh, texture, and all the other things that come with it) in my Hierarchy. I want to move the Plane GameObject to the position and the normal to be that of the Plane object.
Here's some of the code:
plane1Go = GameObject.Find("Plane1");
Plane plane1 = new Plane(Point1,Point2,Point3);
plane1Go.gameObject.GetComponent<Mesh>().vertices= new Vector3[4]{Point1,Point2,Point3,Point4};
As you can see I use the three points, used to make the Plane object to move the Plane GameObject.
First thing, the Plane GameObject does not move and I don't know why.
Second and more importantly, I want to use the Plane object to actually be the point of reference for the Plane GameObject.
Unless it has changed, you should be modifying the Transform element of the gameobject.
GameObject.FindWithTag("PlaneTop").transform.position = new Vector3(10, 0, 0);
This maintains the mesh, but moves the object.
The reference point for the GameObject itself depends on where the local space origin of the mesh is placed when the mesh is exported. I'm not entirely sure what you are trying to achieve but it may be a case of repositioning the mesh itself, rather than the game object.
I'll suggest a two-step approach:
Position a GameObject on the plane
Make that GameObject face in a direction equal to the plane's normal
I assume that you have a mesh attached to the GameObject, perhaps a big quadrilateral of some sort.
Plane plane = new Plane(point1, point2, point3);
//technically, any point on the plane will work
//for our purposes, I'll take the average of your three inputs
Vector3 center = (point1 + point2 + point3) / 3f;
//find the plane's transform
Transform planeTransform = GameObject.Find("Plane1").transform;
//position the plane
//then make it face toward its normal
planeTransform.position = center;
planeTransform.LookAt(planeTransform.position + plane.normal);
The second part of your question is less clear:
I want to use the Plane object to actually be the point of reference for the Plane GameObject.
You could reposition planeTransform as often as needed. You could reposition it each time the plane changes, or once per frame in an Update function.
I do recommend caching the results of GameObject.Find if you're going to be calling this a lot.
I have a screen filled with circles. One circle is the player, one is the enemy, and the others are obstacles. I want the enemy to be able to calculate if there is an obstacle in the path of a straight line from the player to the enemy, so it can adjust accordingly (it is taking place in space, so it is a straight line for jumping from asteroid to asteroid).
Right now, I just turn a random direction when the AI is stuck on an asteroid.
A simple solution could be find the Vector2 between player and enemy (using Vector2.Subtract), and then divide it into smaller vectors or reduce it to a delta that you choose. This can be done normalizing the distance vector and multipling it by a fixed number.
Then simply add that delta to the enemy position and check if the new position collides with an objects (maybe using Rectangle.Contains method), and do this until you reach the player.