How to change the distance/radius from center that the objects are instantiated - unity3d

How would it be possible to move the 8 instantiatedObjects "cubes" closer to the pillar.
public void instantiateInCircle()
{
for (int i = 0; i < amount; i++)
{
float radius = 8;
float angle = i * Mathf.PI * 2f / radius;
Vector3 newPos = transform.position + (new Vector3(Mathf.Cos(angle) * radius, spawnHeight, Mathf.Sin(angle) * radius ));
GameObject instantiatedObject = Instantiate(itemToSpawn, newPos, Quaternion.Euler(0, 0, 0));
instantiatedObject.transform.LookAt(spawnAroundThis.transform);
instantiatedObject.transform.parent = spawnAroundThis.transform;
instantiatedObject.transform.localScale = new Vector3(scale, scale, scale);
//this seems to work-ish , not sure if its good math but hey :)
//Thanks BugFinder!
instantiatedObject.transform.position = instantiatedObject.transform.position += instantiatedObject.transform.forward * distance;
}
}
Ideally these could be moved closer to the pillar

Due to the fact your code already points the object towards where you are trying to get closer, you need only move the object forward until its at the correct distance.

Decreasing the value of the radius variable should spawn them closer to the pillar.
Since you are offsetting the cube position from the pillar position by the cosine and sine components of the radius, it will place them correctly spaced around the pillar.
Also, I believe
float angle = i * Mathf.PI * 2f / radius;
Should really be
float angle = i * Mathf.PI * 2f / amount;
The radius should not affect the angle the object is spawned, but the number of objects should.

Related

How can i throw an object to a specific point with physics

I want exactly this :
The probability of going to the basket must be 100%.
Throw
I tried this :
Vector3 direction = new Vector3(pota.position.x - transform.position.x, pota.position.y - transform.position.y, pota.position.z - transform.position.z);
float vAngle = Vector3.Angle(direction, Vector3.forward);
float hAngle = Vector3.Angle(direction, Vector3.left);
float distance = Vector3.Distance(transform.position, pota.position);
float yDistance = Mathf.Abs(distance * Mathf.Cos(vAngle)) + 0.5f;
float xzDistance = (distance * Mathf.Sin(vAngle));
float xDistance = (xzDistance * Mathf.Sin(hAngle));
float zDistance = Mathf.Abs(xzDistance * Mathf.Cos(hAngle));
rb.AddForce(new Vector3(xDistance, yDistance, zDistance), ForceMode.Impulse);
Thank you for your help.
this is something VERY complicated and there is no real easy way to figure this one out
Read one of the answers in this quetion
and good luck figuring this out
https://math.stackexchange.com/questions/3486775/how-do-you-calculate-the-trajectory-of-an-arrow
(or you could just use lurp or slurp or movetowards lel)

What is the size limit of the gameobjects?

I am building an AR application with unity and Mapbox. I have a point to represents a building. I can geolocate the point through Mapbox. I want to see this object everywhere. So, I change the size of objects according to distance.
Firstly, the code is working. But, I could not see the point really far away, 5 km away.
// Update is called once per frame
void Update()
{
// Get user location
// Latitude
x = getLocation.x1.ToString();
user_lat = Convert.ToDouble(x);
user_lat_rad = Math.PI * user_lat / 180.0; // Radian
// Longitude
y = getLocation.y1.ToString();
user_lon = Convert.ToDouble(y);
user_lon_rad = Math.PI * user_lon / 180.0; // Radian
// Change POIs sizes
distances = distance(user_lat_rad, user_lon_rad);
double s = 0.3; // size of the poi
double d = 50f; // specific distance to point (reference distance)
double size = (distances * s) / d;
float size2 = Convert.ToSingle(size);
temp = transform.localScale;
temp.x = size2;
temp.y = size2;
temp.z = size2;
transform.localScale = temp;
}
public double distance(double lat2, double lon2)
{
// Haversine Formula
// Lat2,Lon2 = User Location
// Lat1,Lon1 = POI Location
double dist1 = Sqrt((Pow(Sin((lat2 - lat1) / 2), 2)) + Cos(lat2) * Cos(lat2) * (Pow(Sin((lon2 - lon1) / 2), 2)));
double distance = 2 * r * Asin(dist1);
return distance;
}
Why I couldn't see the point far away even though object size is change? Is there any limitation for this?
As said in the comments the issue most probably is a too small value for the Camera's farClipPlane.
The furthest point relative to the camera that drawing will occur.
Any object/triangle that is further away from the Camera will not be rendered.
In the Inspector it is configured on the Camera component &rightarrow; Clipping Planes &rightarrow; Far
or using code
cameraReference.farClipPlane = XYZ;

Unity: Faux Gravity - inner Capsule/ Cylinder

I'm struggling with false Gravity in a Cylinder or Capsule. Basically I thought I could take the same Code as for spherical gravity, which does not work. So I changed some lines to get a better result.
[SerializeField] float gravity = 10;
public void Attract ( Transform target )
{
Vector3 gravityUp = (target.position - new Vector3(transform.position.x, transform.position.y,target.position.z)).normalized;
Vector3 bodyDown = -target.up;
Rigidbody rb = target.GetComponent<Rigidbody>();
rb.AddForce(gravityUp * gravity);
Quaternion targetRotation = Quaternion.FromToRotation(bodyDown, gravityUp) * target.rotation;
targetRotation.x = 0;
target.rotation = Quaternion.Slerp(target.rotation, targetRotation, 30.0f * Time.deltaTime);
}
This worked OK on the first try. But the Player(target) can't rotate one the Y-Axis. Does anyone have any ideas?
OK, I tried the following.
Quaternion targetRotation = Quaternion.FromToRotation(bodyDown, gravityUp) * target.rotation;
targetRotation.x = 0;
target.rotation = Quaternion.Slerp(target.rotation, targetRotation, 30.0f * Time.deltaTime);
Now I use the Surface normal to rotate the Player.
if (Physics.Raycast(attractedBody.transform.position + attractedBody.transform.forward, -attractedBody.transform.up, out hit, distance))
{
surfaceNorm = hit.normal;
}
But without Rigidbody Contrains the Player starts rotating without any Input. So I have to use:
rb.constraints = RigidbodyConstraints.FreezeRotation;
This works.

Check if the camera is focused on a sprite

I am writing a Cardboard game in Unity and I want to check whether my camera is facing a sprite or not. I have written the following code:
private void GetSphericalCoordinates(Vector3 vector, out float tetta, out float fi)
{
float r = Mathf.Sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
tetta = Mathf.Acos(vector.z / r);
fi = Mathf.Atan2(vector.y, vector.x);
}
public bool IsLookingAtMe(Vector3 point, Quaternion angle)
{
Vector3 topLeftPoint = new Vector3(transform.position.x - transform.localScale.x / 2, transform.position.y - transform.localScale.y / 2);
Vector3 topRightPoint = new Vector3(transform.position.x + transform.localScale.x / 2, transform.position.y - transform.localScale.y / 2);
Vector3 bottomLeftPoint = new Vector3(transform.position.x - transform.localScale.x / 2, transform.position.y + transform.localScale.y / 2);
Vector3 bottomRightPoint = new Vector3(transform.position.x + transform.localScale.x / 2, transform.position.y + transform.localScale.y / 2);
float topLeftTetta, topRightTetta, bottomLeftTetta, bottomRightTetta, lookTetta;
float topLeftFi, topRightFi, bottomLeftFi, bottomRightFi, lookFi;
GetSphericalCoordinates(topLeftPoint - point, out topLeftTetta, out topLeftFi);
GetSphericalCoordinates(topRightPoint - point, out topRightTetta, out topRightFi);
GetSphericalCoordinates(bottomLeftPoint - point, out bottomLeftTetta, out bottomLeftFi);
GetSphericalCoordinates(bottomRightPoint - point, out bottomRightTetta, out bottomRightFi);
lookTetta = angle.eulerAngles.x * Mathf.Deg2Rad;
lookFi = angle.eulerAngles.y * Mathf.Deg2Rad;
if (lookTetta >= topLeftTetta && lookTetta <= bottomRightTetta)
return true;
return false;
}
In my code I find the 4 angles of my sprite, which is a square, so I have 5 points which make a pyramid. Now I need to check whether my camera is inside my pyramid or not.
The problem is, my lookTetta and lookFi never goes inside my pyramid. I just checked the Tetta only.
Please help me with it.
Using WorldToViewportPoint it should be a single statement. No need to deal with viewing cones manually.
Project the sprite world position to viewport position, then lookup if it is within bounds (ignoring z).
public bool IsMainCameraLookingAtMe()
{
Bounds viewportBounds = new Bounds.SetMinMax(Vector2.zero, Vector2.one);
return viewportBounds.Contains((Vector2)Camera.main.WorldToViewportPoint(transform.position));
}
May check each sprite corner individually to fit your needs.
I have used IntersectRay using the code below:
public bool IsLookingAtMe(Vector3 origin, Vector3 direction)
{
Bounds bounds = GetComponent<Renderer>().bounds;
Ray ray = new Ray(origin, direction);
return bounds.IntersectRay(ray);
}

unity3d rotate back forth with limited angles

I'm want to rotate an object on an axis for specific angles over period of time.
And repeat that on the reverse direction once it reached the limit (of let's say 5 degree).
I could use Quaternion.Euler() to do the rotation towards 5 degree, but how do I check whether it has reached 5 degree and start reversing the direction towards -5 degree?
so in Update() I do:
int dir = 1; // somewhere global
Quaternion r = Quaternion.Euler(0, Timer.deltaTime * dir, 0);
transform.rotation *= r;
// I want to: if the "angle is >= 5f", i want to do dir *= -1 to reverse it
if (/* angle delta is >= 5f or <= -5f */)
{
dir *= -1;
}
Thanks
If you just want to rotate back and forth, you can use a sine wave to move back and forth smoothly.
public class rotator : MonoBehaviour {
public float _Angle;
public float _Period;
private float _Time;
// Update is called once per frame
void Update () {
_Time = _Time + Time.deltaTime;
float phase = Mathf.Sin(_Time / _Period);
transform.localRotation = Quaternion.Euler( new Vector3(0, phase * _Angle, 0));
}
}