Animation auto reset when scrolling up and down in Unity - unity3d

I have create an apps with a timeline like where the function is when scrolling up and down, the content should be animated. I want to build my apps just like this page (http://www.bbc.com/future/bespoke/20140304-how-big-is-space-interactive/ ).
I've put 2 trigger box on top and bottom of the canvas. 2 Animations were created, so when the content from below hit the trigger box when scrolling, it should be animated to large size (animator 'planettolarge'). So does when the content hit the trigger box at the top, it should be back to smaller size back (animator 'planettosmall').
enter image description here
The problem is, how to set the animation to be large size mode when the content is center on the canvas when scrolled from top. Because the animation is normal when the page is scrolled from the bottom.
Here is the simplest I have tried so far since I'm a beginner in Unity. I would appreciate so much if you drop any solution here. TQ =)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class trigger : MonoBehaviour
{
Animator anim_roket;
Animator anim_planet;
private BoxCollider2D triggerup;
private BoxCollider2D triggerdown;
void OnTriggerEnter2D(Collider2D col)
{
switch (col.tag)
{
case "triggerup":
GetComponent<Animator>().Play("planettosmall");
break;
case "triggerdown":
GetComponent<Animator>().Play("planettolarge");
break;
}
}

What i recommend is to use the animator component and triggers instead of two animations, when you reach your trigger point you fire the animation trigger and the animation starts running.
first create an empty state and set many transitions to each f your animations with a bool parameter, if any scripts sets or clears that parameter the animation will be fired or stopped, remember to reset the animation time to the beginning every time you stop any animation so it can start from the beginning again.
see this tutorials to get a better idea, I hope it helps
https://www.youtube.com/watch?v=JeZkctmoBPw
https://www.youtube.com/watch?v=s7EIp-OqVyk

Related

How to make transparent UI visible in Unity Editor?

BackGround
In Unity 2020LTS, I want to make a UI scene.
But in Game Panel, I discovered, although a animation is set at beginning (no conditions), the game will show what I see in Editor panel for a little time at first, then play the animation.
The StateMachine is Entry -> Target(Default)
I don't want show player what I see in editor, but only the first frame in animation.
I guess this is because loading level costs some time (almost 0.5 secs).
Question
So I try another way, make initial state of all objects be same as the first frame of animation.
This way work, seems just like it freeze at first frame for 0.5secs. However, I can't edit those objects visibly (Because they all are transparent in first frame).
I have tried Gizmos, but they don't work well. Besides, Gizmos makes me have to create lots of classes in C# scripts for each object, which just is component of animation and has no script.
Could there be any better way to show transparent (UI) object in editor scene only ?
Assuming you have some Game Manager script, you can add a GameObject, assigning it the UI element, and in the Start() function of the script, make it inactive, like so:
public class GameManager : MonoBehaviour
{
public GameObject menu;
void Start()
{
menu.SetActive(false);
//other statements
}
}
I'm not quite sure what you asked for but if you want to edit the UI elements that are invisible you can simply select them by using the hierarchy and edit them. I have linked an image with an example of this. Example scene with invisible panel
Image img;
// Start is called before the first frame update
void Start()
{
img = GetComponent<Image>();
img.color = new Color32(0, 0, 0, 0);
}
The code above will set the panels transparency to zero when the scene starts, make sure to use the using unity.UI namespace in order to acess the image component.

How do I make a UI button follow a 2d game object?

new to unity and have stumbled into a problem. I am unsure about how to make a UI button on a canvas follow a rigidbody game object in the 2D screen space. I want to make the button's x and y position match the x and y of the RB, so that in theory if I clicked on the RB it would activate the button. However, I am unsure how to implement such a thing into my project.
I tried using transform.position and just equating the ui button pos and RB pos together but it remained static. However, I did script the RB to move to a designated x point and only then did the button move as well despite the RB moving beforehand.
This is literally the example in the documentation for Camera.WorldToScreenPoint(). Attach this script to your button:
public class ObjectFollow : MonoBehaviour
{
public Transform Follow;
private Camera MainCamera;
// Start is called before the first frame update
void Start()
{
MainCamera = Camera.main;
}
// Update is called once per frame
void Update()
{
var screenPos = MainCamera.WorldToScreenPoint(Follow.position);
transform.position = screenPos;
}
}
I have an Idea To Implement that You can use a world space canvas and as a child of that object "that you mentioned has rigid body " now when it moves the canvas and its content will move watch this to
Understand Canvas
Now press on your object at hierarchy window Right Click then UI then Button
this will create new canvas and attach button to will look like this
hierarchy
Now Select Canvas and reset its width and height , set scale to small value 0.01 ,0.01 0.01 for example
then change its Rendering Mode to Space World and assign camera
you should insure that you have event system it should created automatically but events will never work if you don't have a one in hierarchy Window
canvas will like this
Canvas Image
Now You can Select this Button and add a function to call using onClick or you can pass it to another script to add this function dynamically like this
Add Event to UI Button From Script

best way to change 2d sprite to animation

I have a GameObject for Chest(with Collider and SpriteRenderer)
By default it is a static sprite on the screen
now i want to add animation to this game object when open is triggered (by script), I do not have Idle animation, idle animation must be the sprite itself
what is the best to add an animation(no looping, 1 time and then show the animated sprite) to sprite?
I can do it by adding the sprite array and using an coroutine to set the sprite periodically with 0.2f delay (it work fine but not sure if it is the only way which using coroutine with delays to simulate an animation by myself...).
I can do it by create 1 more GameObject with the Animator, when the box open event is triggered, I set the original GameObject to inactive and instantiate the GameObject with the animation (but it requires to create 2 prefabs for two different gameObject).
But none of them look proper way to do this. What is the best solution for this?

Player dies when hitting the floor

I am trying to make a game in unity, and I am new to unity and coding, and I have started making a game, I have made some progress on it but I am having trouble finding some of my answers on youtube and the unity forum, and sometimes when I do, I still can't get things to work. So this is what I'm trying to do.
I have a map and the player is on top of the tower, I want the player to fall and when hitting the ground, dies with it displaying game over, What could I do to make this happen and what script?
So i have this now,
// Ground.cs: Kills players that touch this.collider.
using UnityEngine;
// Attach this to the grass collider
public class Ground : MonoBehaviour {
// Called when another collider hits the grass.
// This is part of Unity!
void OnCollisionEnter(Collision c) {
// Does the other collider have the tag "Player"?
if (c.gameObject.tag == "Player") {
// Yes it does. Destroy the entire gameObject.
Destroy(c.gameObject);
}
}
}
Now, I need to it transition to a game over overlay, which asks me to restart, yes or no.
The resources are out there in regards to learning Unity3D efficiently.
Look at the Unity3D tutorials: https://unity3d.com/learn/tutorials
These tutorials are usually kept up to date in terms of not using deprecated code. Additionally, several of the tutorials will teach you how to set up events like those that you need.
In regards to your immediate question though, look into forming the logic for your game.
You're going to need allow your player gameobject to fall via either gravitational force enacted on a rigidbody or hard-coded physics being applied to the gameobject. Next you will need to determine if the player gameobject has collided with the "floor". If so you will need to trigger an event to destroy the player gameobject, then display a GUI Text that says Game Over.
Look around more at tutorials and get better acquianted with Unity. Over time, you'll learn how to make things happen. If you have more questions, feel free to ask.
Answer to you update:
If your code is functioning correctly and your destroying your gameobject correctly, then awesome! You're learning fast!
The next step could be:
Create a Canvas, create a gui panel, create a gui text
That gui text object can have your Game Over Text
Now, create a button that you will utilize as the restart button
You can have the button call a function which utilizes SceneManager.LoadScene to reload the scene. Look here for an example: http://answers.unity3d.com/questions/1109497/unity-53-how-to-load-current-level.html
Next, Disable the panel as you will not need it until your player dies.
When your player collides with the ground you will destroy the player gameobject and set the panel you created to active. (you can trigger these actions via a listener in code or via the button component in the inspector).

How to ajust screen dynmically of unity game in android device?

Hi.
I have read about scaling-GUI and image aspect ratio, and even if it's a lot of scripts and tutorials out there it's hard to find a good way to handle it for so many different devices and screens.
But for my game it doesn't need to be so complicated, I hope. Scaling the 2D GUI was a quick fix making them a % of the value from Screen.width
The attached jpg shows the only problem I need to fix. The first picture shows the scene in a some kind of normal aspect ratio where i see the whole (test) scene. Pic 1 dragging the view sideways, and important game elements will disappear out of the screen (this is what I don't want). Pic 2 on the other hand showing that Unity scale the scene but so you see the whole scene no matter how extreme widescreen I make it.
And this is really what I want; make it scale the scene sideways and not crop it out of the picture.. Any idea how I make Unity do that?
Create an empty Gameobject and attach following script so everytime you run a game your screen will adjust
using UnityEngine;
using System.Collections;
public class ScreenAdjust : MonoBehaviour {
void Start () {
Camera.main.aspect = 800f/1280f;
}
}
You should use Canvas Scaler
http://docs.unity3d.com/ScriptReference/UI.CanvasScaler.html
Here is a short tutorial:
https://www.youtube.com/watch?v=XkfhxuNr9Es