in my 2d unity game, i am randomly instantiating zombies that I want to face their mouth towards the center where there is it's target because the bullets come out of their mouth which need to face the center. the zombie is a prefab and I have this code on it:
transform.up = -(kids.transform.position - transform.position); it is pretty much working and there are no errors except, randomly, sometimes it becomes really skewed and squished in weird ways. here is a picture of what it looks like when it is skewed:
how can I make it not get skewed? thanks.
Probably you scaled it or one of its parent Transforms uniformly. Possible solution: create a parent which is unscaled and do the rotations there. Maybe have a script to follow some other transform, like settings its position in an update, if need be. A child Transform of the unscaled Transform, which you rotate, can then still be ununiformly scaled.
Related
I am trying to spawn obstacles on the road. To do so, I generate a road by spawning its parts, and each part itself spawns several obstacles in bounds of themselves. But some prefabs after being instantiated got strange "stretching" effects. I don't know how to explain it, so I recorded a small video link. Also, if I spawn that same object by "drag n drop" to the scene, this bug never appears.
This how I spawn obstacles:
Vector3 size = GetComponent<Renderer>().bounds.size;
Vector3 pos = new Vector3(Random.Range(-0.3f*size.x,0.3f*size.x), 30, Random.Range(-0.3f*size.z,0.3f*size.z));
Debug.Log(pos + transform.position +"");
GameObject newBottle = Instantiate(minus_prefabs[Random.Range(0, minus_prefabs.Length)], new Vector3(transform.position.x+pos.x,transform.position.y + pos.y,transform.position.z+pos.z), Quaternion.Euler(0, 0, 0));
newBottle.transform.SetParent(transform);
This happens because the parent GameObject of the model has diffrent sizes on X, Y and Z. This results in some weird stretching. Try going in your prefab and set the scale of your main GameObject to X:1 Y:1 Z:1 for example.
Also see: https://www.unity3dtips.com/how-to-fix-objects-stretching-when-rotated/#:~:text=Cause%20of%20the%20object%20stretching,0.01%2C%200.002%2C%200.004%E2%80%9D.
This also happens to me all the Time. Thankfully its easy to fix!
EDIT: When you instantiate the prefab, also make sure it has the same sizes on every axis.
As a general rule, whenever possible, don't use any Scale other than 1,1,1 unless you need to and it should be on a leaf node of the hierarchy or prefab not a parent node. It will make things go much smoother. If you need to change the size of a mesh (because the noob modeler didn't know how to follow the life-sized scaling metrics in their modeling program, or you just want it smaller or larger), you can do that in the Import settings on the FBX.
I'm having an issue where I can't walk up slopes in unity. My art is pixel so I understand the slope is made with a gap of 1 pixel to create the actual slope, but I have no idea how to fix it so I can just walk up and down them normally. Currently, going down them makes it a little bouncy and going up them is impossible unless you jump. Any help would be appreciated! Here is what the slope looks like:
Edit: Collider looks like this but I don't know how to fix it:
Sprites automatically have a polygon collider created for them when imported into the project. This polygon collider drives the tilemap polygon collider shapes.
You can modify the physics shape for a sprite to smooth it out and remove this unwanted when going up a ramp. Custom Physics Shape documentation
Another important thing to note in your specific problem: Often when a character has a "box-like" shape, they will get snagged on edges and small collider deviations. Unless your game's playstyle is based around a box-shaped entity and interactions, it's usually recommended to use a rounded collider for the moving characters (like a 2d or 3d capsule collider).
I have a laser turret in Unity3D, which I'd like to turn towards the enemies. The turret consists of a "leg" and a "head" (selected on the picture 1). The head can pan and tilt around a spherical joint.
I do the following:
Vector3 targetDir = collision.gameObject.transform.position - turretHead.transform.position;
float step = turnSpeed * Time.deltaTime;
Vector3 newDir = Vector3.RotateTowards(turretHead.transform.forward, targetDir, step, 0.0f);
turretHead.transform.rotation = Quaternion.LookRotation(newDir);
The problem is that since the pivot of the head is not aligned with the laser beam, the turret turns into the almost right direction, but it shoots above the target. (It would hit perfectly, if the laser would come out of the red axis of the pivot.)
Is there a builtin method or some trick to achieve the correct functionality other then doing the calculation myself?
Okay, here's the quick and easy way to do this. It's probably "better" to do it with proper trig, but this should give you the result you want pretty quick:
If you don't already have a transform aligned with the barrel, then create an empty GameObject and line it up (make sure it's a child of the turret so they move together). Add a reference to your script for it's transform.
Then, in your first line, calculate from the new Barrel transform instead of the turretHead transform. Leave everything else the same. This way it calculates from the turret barrel, but moves the turret head.
Now, this approach isn't perfect. If the pivot center is too offset from the barrel transform, then it would be less accurate over large moves, or when aiming at something close by, because the expected position when aiming would be different than the initial position due to the rotation pivot being elsewhere. But this can be solved with iteration, as the calculation would become more accurate the closer it is to it's desired goal.
For more than two days now I am struggling with this seemingly impossible problem: I have a composite GameObject (one parent, 10 children), each one hinged to at least another with angle limits and player-controlled motors. Moves perfectly as I wish.
Now I want to be able to flip it. After a while and many trials,rotation seems the best way in order to keep the angles (inverting localSpace does not respect them):
Vector3 rotPoint = new Vector3 (ParentGameObject.transform.position.x, myY, myZ);
ParentGameObject.transform.rotateAround (rotPoint, Vector3.up, 180.0f);
BUT, if the parent rotates as asked (180 degrees around its Y axis), every child flips but not around the same axis, rather around their own rotation center. This gives ridiculous results as hinges try to get back to the desired position. How could I fix this?
Many many thanks in advance...
In the end I figured out the problem. My problem had no valuable solution.
We can rotate the transform around the Y axis even in 2D, but NOT the rigidbody2D, which in the same object will invariably cause serious problems.
This is maybe one remaining inconsistency into Unity5's implementation of 2D.
I had to flip using localScale inversion and programmatically updated positions and rotations.
It sounds like the child GameObjects are rotating around their own pivots instead of the parents. I haven't got Unity installed at the moment to test this, but if you add an empty GameObject as a child of your parent (and with the same pivot point), then make all your other GameObjects a child of this, it should work.
You should not move or rotate a rigidbody if you don't have to. That will mess up the underlying physics calculations and physics engines won't be happy about it. Instead, you have other ways to interact with them
Apply force, torque
Apply acceleration, angular acceleration
Change speed, angular speed
If you have to move/rotate the object, you should also update positions, rotations, speeds, angular speeds of all of your hinged objects. That would not be easy. Give some more details if you unable to solve.
I'm trying to make a respawn system for a game in unity that starts the character back on the last platform they were on.
As it is currently, it keeps track of which platform they were last grounded on with onCollisionEnter and detects if onCollisionExit touches an out of bounds area.
I need to find the position of a face on the mesh with the y axis (assuming the best way is to do a raycast on the global y axis from the center of the platform) and add the height of the character/2 to determine where to respawn the character.
I'm very new to unity and c#, so I've never done a raycast before and I'm not sure if it's possible to raycast from inside an object to find it's mesh in a given direction, or if there is a better/more efficient way.
Thanks in advance :)
"if it's possible to raycast from inside an object to find it's mesh in a given direction"
You can place a empty gameobject at the center of your mesh (make it child of your mesh ) then pass the position of this empty gameobject to raycast origin.
I usually make re-spawn system with triggers. If you explain little bit more what actually you want to do. I'll try to guide you in that particular direction.