Unity trying to scale UI when mouse is over - unity3d

I'm trying to scale a UI when mouse is over the UI but in my code, when mouse is over a UI, all the UI elements are scaling and I only to scale the UI that my mouse is over. Here's my code:
public void StartScale(){
foreach (GameObject button in buttons){
LeanTween.scale(button, new Vector2(scaleAmount, scaleAmount), scaleSpeed);
}
}
I have an array of game objects that I want to scale. I want them to scale only when my mouse is over but I can't make it work.

There are two things at play here. One is how you populate your buttons list, no information was provided for us to work with. The other is the transform hierarchy. If you scale an object, anything that is a child will also scale as all transformations are relative to the parent.
In this case my suspicion is that you've accidentally added an object too far up your hierarchy tree to the list, or have made certian GameObjects children of the object you intended to scale.

Related

Move a GameObject by its child objects

I wanted to create some evolution like "game". I make some bones, muscles, etc. When I hit play the creature moved but when I've checked the distance it stayed 0. What I've discovered was that all individual child elements moved but the containing GameObject was stil at the same position.
What is the (correct) way to make changes of a child object affect the parent object? Like moving legs to move the person.
The question seemed clear in my head but after reading your questions, remarks and comments I needed elaborate more:
I understand the basic idea of walking animations and such (i think). But here I'm trying to move a creature by itself. At first purely random movement and hopefully later on via a neural network.
In the sample screenshot below you see a creature, the spheres are colored for its weight and the beams between the spheres are the bone/muscle. And my creature limbs do move by pulsating the bone/muscles... but the position of the parent object stay the same. The magenta line is the position of the parent. I would like that the position of the parent would follow the center of its elements.
Your question is a bit unclear to what exactly it is you are after. However, if you want the child-parent behaviour become reveresed in a way that child becomes parent and parent becomes child just for function A() , you can simply swap the child and parents relation in A() itself,
As an example,
void A()
{
child.transform.SetParent(parent.transform.parent);
parent.transform.SetParent(child.transform);
... // your A function
parent.transform.SetParent(child.transform.parent);
child.transform.SetParent(parent.transfotm);
}

Unity all objects have same position

I have a 3d building model in my Unity project. It has many children like doors, walls etc. The problem is, all of the children points to same position in the Unity world (24.97, -2.08, 19.35). Their transforms show this position. And this position is far away from their actual one. How can i fix this?
I tried freeing all children from parent but this didn't change anything.
I want them to show their real position, which appears with move tool when we click upon them.
Here is the image link
It seems that this is simply their pivot point exported "wrongly" from the 3D editor your model was made with.
This won't change until you export it correctly from a 3D editor (Blender, Maya, etc).
Unity is not made for 3D mesh modeling and therefore in Unity itself you can't change the pivot points.
There is a very simple fix
Add a new empty GameObject
In the Inspector go to the Transform component, click on the context menu and hit Reset (you also simply set it to position 0,0,0 rotation 0,0,0 and scale 1,1,1) assuming the pivot should be at 0,0,0
Now drag and drop all objects into the empty GameObject
=> You have now one parent object with correct pivot.
By wrapping it in a parent object the childrens pivots don't matter anymore. You can simply do all translation, rotation and scaling on the parent object with the correct pivot and don't have to care about that "wrong" position at all.

Multiple Colliders on Complex Object

I have designed a complex city with thousand of objects like roads, sideways, buidlings, trees, etc. and imported to Unity3d
I want to put colliders over them, so that when player hit them, it should face collision and should not passes though them
Though it has many objects, if i put one by one collider to each object, it will take so much time. Is there any other way i can just select all of them and put collider.
Also, if i select all object and put colliders (Mesh Collider). it is not adding to the object
Please help
Editor Scripting to the rescue. I would write a tool similar to this example. It uses a Menu Item which can be selected via Window -> Collider Tool. It then runs our custom method to find all meshes, which we want to add a collider to, then adds one and also logs which GameObjects were changed.
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
public static class ColliderTool
{
[MenuItem("Window/Collider Tool")]
public static void RunTool()
{
List<MeshFilter> meshes = findObjectsThatNeedColliders();
for (int i = 0; i < meshes.Count; i++)
{
GameObject go = meshes[i].gameObject;
// Add a collider to each mesh and record the undo step.
MeshCollider collider = Undo.AddComponent<MeshCollider>(go);
// Use the existing mesh as the collider mesh.
collider.sharedMesh = meshes[i].sharedMesh;
Debug.Log("Added a new collider on: " + go.name, go);
}
Debug.Log("Done");
}
private static List<MeshFilter> findObjectsThatNeedColliders()
{
// This list will be filled with all meshes, which require a collider.
List<MeshFilter> meshesWithoutCollider = new List<MeshFilter>();
// Get all meshes in the scene. This only returns active ones.
// Maybe we also need inactive ones, which we can get via GetRootGameObjects and GetComponent.
MeshFilter[] allMeshes = GameObject.FindObjectsOfType<MeshFilter>();
foreach(MeshFilter mesh in allMeshes)
{
if(mesh.GetComponent<Collider>() == null)
meshesWithoutCollider.Add(mesh);
}
return meshesWithoutCollider;
}
}
You might need more complex rules about which objects require a collider and which type it should be, but a simple tool can still save you a lot of time before hand tweaking everything.
You should also consider making this selection based. The Selection class can give you a list of all selected objects. Some ideas:
Add a MeshCollider to all selected objects, if one doesn't already exist.
Add a BoxCollider to selected objects and scale it to an approximate size depending on the transform size or bounding box of the MeshRenderer.
My solution is based on your question about how to add a collider to many objects in a large scene. However, this might not be the overall best solution. Maybe too many MeshColliders hurt physics performance. You will most likely want to approximate most of the colliders with boxes and spheres, but of course you can still write a tool to help you with that.

Make rigid body bounce off the screen edge

I'm currently working on an object that involves bubble-like movement. This object has a rigidbody and a sphere collider.
I use the AddForce method every 1-3 seconds to make it move continuously and slowly.
Now I'd like to know how to make the rigidbody move in the opposite direction (a.k.a bounce off) when they reach the screen edge already. I have already computed the screen edges using ViewportToWorldPoint method.
One ineffective solution that I thought of is to put empty game objects with collider at the edges but that won't work in my case since I'm building for mobile devices which have different screen resolutions/sizes.
Anyone know a good solution for this?
I'm not sure i got the idea. But i think i had the same problem when i was writing an old mobile game.
I had the same idea you did, use empty game objects with box collider on the edges, but then i thought, this isn't responsive, so i wrote this code:
public class Walls_c : MonoBehaviour {
public Transform righttop;
public Transform rightbottom;
public Transform lefttop;
public Transform leftbottom;
// Use this for initialization
void Start () {
righttop.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(1,1,0));
rightbottom.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(1,0,0));
lefttop.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(0,1,0));
leftbottom.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(0,0,0));
}
}
With this, i always get the corners of the screen. It's no fancy... but it works.
Let me now if it works.
In order to get a reflection effect, you need Material. Attach a bouncy material to the edges(gameObject) and let the physics calculate what should be the reaction of collision.
Create a Physics 2D material and set the bounceness to some appropriate value say 0.2.
Regarding your issue:
One ineffective solution that I thought of is to put empty game
objects with collider at the edges but that won't work in my case
since I'm building for mobile devices which have different screen
resolutions/sizes.
If you are working with UI component then dealing with boundaries should not be a problem (since it anchors with different resolution) but if it is not the case you can create a script CameraResizer and an enum Anchor (Left, Right, Top, Bottom) and using the same way ViewportToWorldPoint you can align your empty gameObject (boundary) to any screen size by attaching it the gameObject.
Hope it helps!

Unity3d Transform issues when trying to re-parent a Gameobject with sub-Gameobjects

I am trying to grab a Gameobject that's placed in the correct spot and re-parent it to different Gameobject transforms using code. I manage to do this by using ..transform.parent = parent.transform but the rotation and position get messed up. How can I keep it's rotation and position when re-parenting?
Thanks!
Always use gameObject.transform.SetParent(anotherGameObject), or you risk exactly the sort of phenomena you've described. See SetParent docs; there is a second parameter to consider.
The form mentioned in the question still works in many cases, but has been deprecated for a while.
Place all game object that you want to parent, to empty game object(example:"EMPTY\PARENT\EMPTY\CHILD") with scale 1:1:1(in editor)or re-scale your parent game object to 1:1:1