Update child gameobject positions when rotating parent gameobject - unity3d

I'm a beginner and I'm currently trying to create a 3D Tetris. Since 1-2 Weeks im trying to rotate my Tetromino properly, but it's not working the way I want.
So this is my "T" Tetromino. It's made of 4 Cubes, which are combined with an empty gameobject like this:
When I rotate the "T" gameobject with transform.Rotate(0, 0, -90), optically every cube moves, but not really. There is a blue dot on the first Picture. When i rotate my gameobject, it rotates around this point. After the rotation, every cube gets its new transform.position expect the middle one. Optically it moves one unit, but its transform.position doesn't change.
The same with the other figures. There is always one Cube, which transform.position doesn't update. I kinda know why its position doesn't change, but i still don't know what to do.
I tried many stuff, but i failed. Summarized I want my gameobject to rotate and update its position. Is there a way to do this?
Best regards

Related

Unity bounce and move

I'm working on a game similar to golf. My situation is this:
I have colliders like martinellas, so their behavior is to bounce ball and keep the same amount of speed.
Well, since I added some abilities, one ability is that my ball transforms into a wheel and goes only in forward direction at higher speed. When it collides with martinellas, it should bounce by Y axis only (in air) and stop movement as well. I managed to do it by freezing the positions.
Looks good for now, but I have a problem: when the ball touches the ground, it collides again, because it goes to the first position of collision. It makes sense.
Now, I need help on how to only bounce in the air, and little bit backwards, so it doesn't collide again (without freezing the positions, I guess).
So, here I added a picture to understand it easier.
1st and 2nd scenario works now.
Now I need the 4th scenario.
The 3rd scenario shows how the ball is dropping by default: it drops down to position of ball in 2nd scenario, which makes collision again. I need the ball to drop down by changed direction, not much, only a little bit to evade the collision.
You can interpolate between y-axis between fixed position.
Vector3 interpolatedPosition = Vector3.Lerp(
new Vector3(transform.position.x, 0, transform.position.z),
new Vector3(transform.position.x, 5, transform.position.z), Time.deltaTime * 5);
transform.position = interpolatedPosition;

How would I keep the rotation of the look of the object but make the rotation 0 Unity

How would I be able to have Wheel look the same but make the rotation 0.
As already commented you can rotate the mesh in 3D modeling software.
A faster way would be to parent the GameObject that represents the wheel under another GameObject, which will be acting as a pivot then. Just position it at the exact position of the wheel GameObject, but with a the desired rotation.

How do I apply rotations to parent child using Rigidbody?

I have a finger object. It is just three cubes representing the finger parts.
The 2nd cube is the child of the 1st one. And the 3rd cube is the child of the 2nd one.
This is the heirarchy: Cube1 -> Cube2 -> Cube3
My goal is to apply a rotation angle to the first cube and let the other cubes do the same locally.
Example: Apply 30 degrees Z rotation to the first cube, 30 degrees Z rotation to the 2nd cube, and also the 3rd one.
This will make a finger that look like this:
(Forgive me if it doesn't look like a finger)
In every Update() frame, I will change the angle (it's just one number) and it will rotate every cube for me.
My question is:
How do I make all these cubes collide properly with other objects?
I tried putting the Rigidbody on all of them and set isKinematic=false because I want to transform them myself. But I still cannot use transform.rotation to update my rotation because it will miss the collision with a ball very easily (especially the tip of the finger because it moves faster than other parts). Continuous detection doesn't help.
So I tried using rigidbody.MoveRotation() and rigidbody.MovePosition() instead, which is a pain because they need absolute values. They worked but the animation is so jumpy when I change the angle quickly.
I'm guessing that the animation is jumpy because there are many Rigidbodies or because the physics engine cannot interpolate the position of each box properly when I use MoveRotation() and MovePosition().
I need to use MovePosition() also because when I use only child.MoveRotation(transform.parent.rotation * originalChildLocalRotation * Quaternion.Euler(0, 0, angle)), the position doesn't move relative to the parent. So I have to compute child.MovePosition(parent.TransformPoint(originalLocalPositionOfTheChild)) every frame too.

Child gameobject moving faster than parent gameobject

Character is the parent object and has camera object as its child object (FPS type).
Camera object has a script which 'throws' raycast to detect objects in front of it.
Camera object has a child object 'Crosshair', which has a script 'ShowCrosshair' attatched to it.
Character <- Camera <- Crosshair.
Expected behaviour should've been: I move the mouse up and down, camera rotates around y-axis and along with it the crosshair object moves smoothly remaining at the center of the screen.
But, whenever I move the mouse up/down, crosshair moves faster than the camera can rotate and goes out of sight.
Here's my code for Camera Rotate:
//vertical is float and is initially zero and transform is for the current gameObject i.e Camera
vertical -= Input.GetAxis("Mouse Y");
transform.localRotation = Quaternion.Euler(new Vector3(vertical, 0.0f, 0.0f));
This script is attached to Camera object.
It is desired that Crosshair move with camera that is why it's parented by camera object.
I think your unwanted behaviour happends because whenever you move, rotate or scale the parent Transform all the childs are affected, therefore when you rotate the camera, the crosshair moves with it and then you rotate the crosshair causing it to rotate a second time and going out of sight.
It look like you are rotating around the X-axis instead of the Y (vertical) in this line:
transform.localRotation = Quaternion.Euler(new Vector3(vertical, 0.0f, 0.0f));
Also you should probably multiply the affect of the input by the delta time or it will move much too quickly.
vertical -= Input.GetAxis("Mouse Y") * Time.deltaTime;
But I don't think any of that is your problem. You commented that the script is attached to Camera. Is it also attached to Crosshair? Because if so then you will be getting double rotation. If not, take a look at what scripts might be changing the rotation of Crosshair. If you are just moving the camera, you shouldn't need anything rotating Crosshair at all (because it will move with its parent).

Collision Detection of objects in certain area of camera in Unity3d

I am creating a 3rd person action game where the player is a helicopter and he can shoot other objects while moving. The problem is I am trying to find the enemy objects who are inside a circle in the center of the camera and I need to track them and shoot them.
Raycast wouldn't help as i need a thicker raycast and so I tried spherecast and capsulecast.
I have a GUI element which gives the player idea on where he can shoot.When using Spherecast or Capsulecast, It is working when the enemy is near but when the enemy is far behind I guess the spherecast becomes small while traveling along z and doesn't hit the object most times.
if (Physics.SphereCast (startPoint, 1f, transform.forward, out hit)) {
if (hit.collider.CompareTag ("Shootable") ){
Debug.Log(hit.collider.name);
Destroy(hit.collider.gameObject);
}
}
I have seen raycast from camera and so i was wondering if there is something to do like circlecast from the camera which would be appropriate for this. If not how can I proceed?
Any help is really appreciated.
If you want to detect whether enemies lie within a conical area in front of your camera, using a SphereCast or RayCast will not be able to meet your needs.
Instead, you might consider checking the angle between an enemy's relative position and your camera's forward vector, to see if it is below a particular value, and hence within the cone.
For a 60-degree field of view, and assuming you store your enemy Transform components in an array/List, your code might look like:
foreach (Transform enemy in enemies){
if (Vector3.Angle(transform.forward, enemy.position - transform.position) < 30){
Destroy(enemy.gameObject);
}
}
Hope this helps! Let me know if you have any questions. (Answer adapted from this Unity question.)