Play two animations together on Spinner - unity3d

I want to play moving and rotation animations on Spinner game object. For rotation I have used animator controller and for movement I have written code.
Here is animator controller inspector settings for rotation:
Here is the code that I have written for movement:
private void StartMoving()
{
iTween.MoveBy(gameObject, iTween.Hash("x", transformDistance, "time",
Random.Range(1.5f, 5f), "looptype", iTween.LoopType.pingPong, "easetype",
iTween.EaseType.linear, "delay", Random.Range(0f, 1f)));
}
At present only movement related action, I am showing in game play, rotation completely get stopped. If I set Apply Root Motion flag to false then rotation animation start playing and movement get stopped.
I want to play both animations together.
Here you have spinner object details:

Anytime you are combining an animation via the Animation controller and some other animation/motion via script it's best to separate those two objects (into parent child objects) as these two effects will conflict with each other.
Yes you could try some Apply root motion and other fancy stuff but it's always messy and I've found that separating them is cleaner and simpler.
In your case
Create a parent object and put your motion effect on that
Create a child object with your rotation object and animate it as you want.
Because the animation is now on the child object it wont be affected by the motion on parent.

Related

Unity trying to scale UI when mouse is over

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.

Unity-3D - Can I have several object show the same animation state?

An animated figure requires an Animator to have it show animation.
I am trying to create a batalion of soldiers walking in lockstep. Just creating 200 soldiers and moving them is easy, but each one has its own animator and is calculating the animation pose of the soldier in each frame - for each soldier.
But if they are all the same it would seem better to have one animator calculate the pose and use this shaped mesh for all of them.
Is there a way to have several gameobjects share one Animator, one Mesh, and one single copy of the resulting pose?
The screenshot attached shows that the 500 animators consume several milliseconds on 5 CPUs to do the animations...

How could I create a copy of a gameobject hierachy which mimics all positions, movements, etc of the original? Like a hologram of the original?

I am building a 3d Billiard game in VR using Unity3d.
I try to display a miniature copy of the billiard "table" (actually a cube), like it is at any given moment.
I tried instantiating the root of the billiard hierarchy, just to find out it will instantiate the original gameObject (runnings its Start() methods) which totally makes sense, just not what i am trying to do.
In practice I have a billiard root, which has all the geometry of the table, and all balls as children, those balls can interact physically.
On button press I try to create a hologram of the table, with all its balls at their position at any given time.
So If a player hits a ball and it moves in the original, it should display the same in the miniature.
I thought it might be possible to translate the ball positions (and table rotation etc) every frame to the miniature. but that seams very un optimal.
var midPoint = (leftHand.transform.position + rightHand.transform.position) / 2;
var miniature = Instantiate(gameObject, midPoint, transform.rotation);
miniature.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
this obviously does not work as i described above. It creates a completely new instance of the billiard cube. (this code was run from a script inside the billiard root)
The board itself probably doesn't move, so you can just make holograms and simulate the balls.
Since Unity 2018.2, you can create physics scenes and simulate them manually.
For that problem you would need to:
Stop physics simulations.
Physics.autoSimulation = false;
Create a new scene for independent physics simulation.
var physicsPreviewScene = SceneManager.CreateScene("Physics Preview Scene", sceneParams);
Copy the physics-relevant objects and components to the preview scene.
Many ways to do it, but basically colliders, rigidbodies, and for objects you need visuals for, renderers.
You can use caching to avoid creating these every time.
You can also, in theory, have the simulation on the main scene, but then you need to make kinematic all the stuff you don't want simulated, and there are some gotchas.
Then simulate manually.
Get the PhysicsScene from the preview scene: var physicsScene = physicsPreviewScene.GetPhysicsScene();
Simulate: physicsScene.Simulate();
So yea, short of avoiding the components unrelated to physics in the copies, you can't avoid the duplicates; only mitigate their impact with pooling and/or caching.
Another technique is to use casting (spherecasting, in this case) to make a mockup physics simulation, stepping multiple casts with velocity, acceleration (gravity for example) and Time.fixedDeltaTime, and reacting to intercepts/hits as collisions, with Vector3.Reflector something similar. But this will only give you an approximation of how physics would react, and is not going to be an actual (accurate) physics simulation like having multiple PhysicsScenes will.

What's root motion and how it works?

I'm reading Unity Animation cookbook book. And I'm stuck at "Root Motion" topic. All I can understand now is Root motion allows the GameObject to move with the motion clip without coding. and it depends on the root node. But I can't imagine/understand how ? or what're that related properties like "Bake to pose" .. what's the pose..? I searched the web to find anyone talking about it.. but no helpful tutorials there! I tried to read from unity docs about the topic but it made it worse..
https://docs.unity3d.com/Manual/RootMotion.html
Please help me with example/link/replay
After spending more time searching/watching videos/ reading from other books to understand everything. I'll put my answer here for anyone face same difficulties understanding this topic
Treadmill vs root motion: There are two types of animation, treadmill and root motion. Treadmill means that the animation stays at the origin and we use code to move that asset around. Root motion means the motion is built right into the animation and it's the animation that determines how far something moves rather than code.
Then you have to watch this video to get an idea about how it looks in Blender and later in Unity when you import the character & animation
https://www.youtube.com/watch?v=d5z9dEnE4DE
Root Transform Rotation: This option captures the rotation of the root node and
applies it to the whole game object. You can set it to Bake Into Pose to disable the
root motion rotation. With this option selected, the rotation will be treated as a
visual effect of the animation and will not be applied to the game object. You
should set it to true for every animation that shouldn't rotate the character. You
can set the Based Upon option to one of the following options:
Root Transform Position Y: This option captures the vertical movement of the
root node and applies it to the whole game object. You can set it to Bake Into
Pose to disable the root motion in the Y axis. With this option selected, the Y axis
motion will be treated as a visual effect of the animation and will not be applied
to the game object. You should set it to true for every “on ground” animation
(unless it's a jump).
Root Transform Position XZ : This option captures the horizontal (XZ)
movement of the root node and applies it to the whole game object. You can set it
to Bake Into Pose to disable the root motion in the X and Z axis. With this option
selected, horizontal motion will be treated as a visual effect of the animation and
will not be applied to the game object. You should set it to true for all stationary
animations (such as Idle).
Good animations may combine both traditional(treadmill) and root motion ways.
The Body Transform (Pose) is the mass center of the character.
The Root Transform is a projection on the Y plane of the Body Transform (Pose) and is computed at runtime.
If you want to transfer the body transform to the gameobject first you have to transfer it to the root transform because that is the one that is applied to the gameobject transform. to transfer, for example, the XZ motion of the body transform to the root transform, you need to uncheck bake into pose option.
All right except "You should set it to true for every “on ground” animation (unless it's a jump)."
More correctly will be: "You should set it to True for every animation, where you will move Character GameObject by Axis Y by code (Unity Script), also are including the Jumping animation. If you will not move the GameObject of the Character along the Y-axis (by code), but want to implement "Jump" as the movement of bones (parts of the character's skeleton) from the jump animation, if it is present in the animation that was imported, you must set it to False. Many popular the Jumping "In-place" animations doesn't include the movement of bones, which will track the full bones movement at real Character Jump (say simple - in these animations the Root Node not up on demanding height)."

Unity - Rigidbody joint inheriting only position?

I was wondering if it was possible to have a joint inherit only the position of the object it is attached to. I'd like to be able to control the rotation of it independently using a controller.
You can't subvert Unity's hierarchy directly but you can do something like:
transform.localRotation = Quaternion.Inverse (transform.parent.rotation);
Put that in a script and attach it to the child and that child will negate its parent rotation. Keep in mind that if you want to rotate that child you need to apply that rotation to localRotation after inverting the parent.
Alternatively you could write a tracking script that updates position:
Transform follow; // set to object you're following.
void Update() {
transform.position = follow.position;
}