Updating the Computer Player action turn by turn in Unity - unity3d

Currently i am developing the board game. There are four players, one is the actual human player and other remaining three is computer player or simply a bot.
When it is the turn of human player to throw the dice. He can throw and update the score and other thing.
But when it is the computer player turn i want everything to be updated such as throwing the dice updating score slowly so that player can see it.
What i am doing now is normal and after a human player turn, the computer player turn is updated in millisecond.
How can i do it so that it will be updated slowly and player can see it.

This is a very general question and without some code it's difficult to answer but there are two main options you can look into.
1) Convert your AI method into a coroutine and then add some "WaitForSeconds" in between.
IEnumerator YourMethod(){
// Do stuff
yield return new WaitForSeconds(time);
// Do stuff
yield return new WaitForSeconds(time);
// Do stuff
yield return new WaitForSeconds(time);
2) Break your Method into several methods and chain calls to one another at the end with Invoke
void YourMethod(){
// Do stufff
Invoke("YourMethod2", time);
void YourMethod2(){
// Do stuff
Invoke("YourMethod3", time);

Related

Make player die if they fall off the platform

So, iam making a 3d endless runner game with unity. I want the game to stop(just like when the player hits an obstacle) when my character falls of the platform. Keep in mind that when starting the game the character's ***y position is -1.4 ***
I tried this code but it didnt work:
if ( transform.position.y < -1.5)
{
//printing game over-stopping the game
SC_PlayerMove.instance.gameOver = true;
}
I tried your code, and it worked for me when I placed it inside the update function. If you add a Debug.Log inside the if statement, check that it prints when the condition occurs. If it does, then it might be an issue with how you're handling game over.

How to get info about completion of one of my animations in Unity?

I have monster named Fungant in my 2D platform game.
It can hide as mushroom and rise to his normal form.
I try to handle it in code, but I don't know how to get information about finished animation (without it, animation of rising and hiding always was skipped).
I think the point there is to get info about complete of the one of two animations - Rise and Hide.
There is current code:
if (
fungantAnimator.GetCurrentAnimatorStateInfo(0).IsName("Fungant Rise")
)
{
fungantAnimator.SetBool("isRising", false);
isRisen = true;
fungantRigidbody.velocity = new Vector2(
walkSpeed * Mathf.Sign(
player.transform.position.x - transform.position.x),
fungantRigidbody.velocity.y
);
}
if (fungantAnimator.GetCurrentAnimatorStateInfo(0).IsName("Fungant Hide"))
{
fungantAnimator.SetBool("isHiding", false);
isRisen = false;
}
I try this two ways:
StateMachineBehaviour
I would like to get StateMachineBehaviour, but how to get it?
No idea how to process this further.
AnimationEvents
Tried to do with animation event but every tutorial have list of functions to choose (looks easy), but in my Unity I can write Function, Float, Int, String or select object (what I should do?).
I decided to write test() function with Debug only inside, and create AnimationEvents with written "test()" in function field, but nothing happens.
Same as above, no more ideas how to process this further.
I personally would use animation events for this. Just add an event to the animation and then the event plays a function after the animation transition is finished.
For more information on how to use animation events you can click here.
This post may also help you to add an event when the animation is finished.
I hope this answer helps you to solve this problem.

Is there any function in Unity 2d , when I die the the game over screen comes only after death animation finishes

Any way to show Game over screen only after the death animation of player is played text
Check in update if animation is playing or not. if animaion.IsPlaying() return false means your animation is finish and then you show your game over screen.
in your player script (or whatever script tells your die animation to play), go below the line where the die animation plays, then write this line of code: StartCoroutine(ExampleCoroutine());. Make sure you replace the 'ExampleCoroutine' part with the name of the IEnumerator method (more on that in the next sentence). Next, go below the function where you typed StartCoroutine(ExampleCoroutine());. and make an IEnumerator method. Inside it type yield return new WaitForSeconds(1f);. and replace the 1 with how long it takes for the die animation to finish. Lastly, below that line, activate your game over screen. Hope this helped a bit!
If you want to attach code functions to animations, you can use animation events to call functions at very specific times during (or at the end of) animations.
If you prefer, you can also use events to transmit animation status from inside your animator controller using state machine behaviours.
You can use a variable playeranimationfinised and use an IEnumerator with WaitUntil like this
using System;
using UnityEngine;
public IEnumerator gameover()
{
//function to check animation finished
Func<bool> animationfinished = () => playeranimationover;
//wait until playeranimationover is true
yield return new WaitUntil(animationfinished);
//show gameoverscreen
}

How would I detect if my player is touching a game object?

Okay so basically im working on this test project that relies heavily on movement and momentum to complete levels, with a bit of parkour. I need a collider for two reasons, 1. The player touches an object at the end and gets put back into the menu. 2. If the player falls out of the map they die.
I tried a bit messing around with Colliders and at the Docs and while usually id figure it out ive been stumped for 20 minutes looking at Unity's docs and a few questions from here.
public GameObject objectCollider;
public GameObject anotherCollider;
void OnCollisionEnter(Collision collision)
{
if (CollisionDetection.IsTouching(objectCollider.gameObject, anotherCollider.gameObject))
{
Destroy(plr);
}
}
This is what I got so far. I get an error from here and if I switch it to if collider object == the other it errors out.
Basically what I want (If you perfer to just post the answer code but comments on it would still be helpful so i learn!) is for one gameobject (player (but in code its objectCollider)) to be detected if it touches the other (a cube (in code its anotherCollider)) and to execute code (for example Destroy(playerObject))
Thank you for any help you bring here, links, code anything!
Hopefully this is what you're looking for:
public void OnCollisionEnter(Collision collision)
{
if (collision.collider.name == "endObject")
{
//put back into the menu
}
}
Once your player enters a collision, it checks the name of whatever object it collided with. Then you can execute what code you want in the if statement.
You can use collision.collider for many other things, such as collision.collider.tag, but this should give you a start.

Adding videos to Unity3d

We are developing a game about driving awareness.
The problem is we need to show videos to the user if he makes any mistakes after completing driving. For example, if he makes two mistakes we need to show two videos at the end of the game.
Can you help with this. I don't have any idea.
#solus already gave you an answer, regarding "how to play a (pre-registered) video from your application". However, from what I've understood, you are asking about saving (and visualize) a kind of replay for the "wrong" actions, performed by the player. This is not an easy task, and I don't think that you can receive an exaustive answer, but only some advices. I will try to give you my own ones.
First of all, you should "capture" the position of the player's car, in various time periods.
As an example, you could read player's car position every 0.2 seconds, and save it into a structure (example: a List).
Then, you would implement some logic to detect the "wrong" actions (crashes, speeding...They obviously depend on your game) and save a reference to the pair ["mistake", "relevant portion of the list containg car's positions for that event"].
Now, you have all what you need to recreate a replay of the action: that is, making the car "driving alone", by reading the previously saved positions (that will act as waypoints for generating the route).
Obviously, you also have to deal with the camera's position and rotation: just leave it attached to the car (as the normal "in-game" action), or modify it during time to catch the more interesting angulations, as the AAA racing games do (this will make the overall task more difficult, of course).
Unity will import a video as a MovieTexture. It will be converted to the native Theora/Vorbis (Ogg) format. (Use ffmpeg2theora if import fails.)
Simply apply it as you would any texture. You could use a plane or a flat cube. You should adjust its localScale to the aspect ratio of your video (movie.width/(float)movie.height).
Put the attached audioclip in an AudioSource. Then call movie.Play() and audio.Play().
You could also load the video from a local file path or the web (in the correct format).
var movie = new WWW(#"file://C:\videos\myvideo.ogv").movie;
...
if(movie.isReadyToPlay)
{
renderer.material.mainTexture = movie;
audio.clip = movie.audioClip;
movie.Play();
audio.clip.Play();
}
Use MovieTexture, but do not forget to install QuickTime, you need it to import movie clip (.mov file for example).