I want it so when the player moves his mouse on an object the time freezes and he can't move anymore. I tried different methods but it just doesn't freeze the time.
private void OnMouseEnter()
{
PlayRandomSound();
Time.timeScale = 0;
}
Related
I am working on a VR speedrun game and I need a timer. The timer doesn't need to be showed in the screen for the player, just on the map I made. It needs to start when the player (VR) passes a specific point and end when it reaches a different point. If anyone has an idea of how to make this work I would really appreciate it.
On the start line you could have an empty gameobject with a trigger collider on it, and in the OnTriggerEnter event you could start a Coroutine that keeps track of the time, and on the finish line you'd have another trigger collider that sets a flag and stops the timer.
Something along the lines of this should work:
using UnityEngine;
using System;
public class Player : MonoBehaviour {
private bool _isTimerStarted = false;
private float _timeElapsed = 0;
private void OnTriggerEnter(Collider other) {
if (other.gameObject.name.Equals("Start Line")) {
_isTimerStarted = true;
StartCoroutine(StartTimer());
} else if (other.gameObject.name.Equals("Finish Line") {
_isTimerStarted = false;
}
}
IEnumerator StartTimer() {
while (_isTimerStarted) {
_elapsedTime += Time.deltaTime;
yield return null;
}
yield break;
}
}
For this to work just make sure your player has a RigidBody attached or else no collision will be detected :)
If you want a "timer" as in "show the elapsed time since some event" you might take a look at Stopwatch
var sw = Stopwatch.StartNew();
...
Console.WriteLine(sw.Elapsed.ToString());
The stopwatch is primarily intended for performance measurements. But it is easy to use, so if it fits your use case you might as well make use of it, even if the resolution and accuracy is much greater than you need.
I am quite new in Unity and I have a simple question regarding Animation events.
In my code snipet I have the public variables - I noticed that I can't reference an AnimationEvent - and some simple stuff to do with them.
public GameObject completeLevelUI; // a panel
public Text endUI; // the text on the panel that show your success or fail
public AnimationEvent nextSceneEvent; // AnimatonEvent in the animation of the panel above
public void GameOver() { nextSceneEvent.time = 2; completeLevelUI.SetActive(true); endUI.text = "LEVEL\nFAILED"; }
public void GameWon() { nextSceneEvent.time = 6; completeLevelUI.SetActive(true); endUI.text = "LEVEL\nCOMPLETED"; }
public void LoadEnd() { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); }
The AnimationEvent's target method is LoadEnd().
The default firing time is 1.5 seconds and the code won't change it.
The reason why I need different fire times can't be seen in the shared code - it is not important now. I figured out some other ways to solve it, I am just curious why it isn't working.
I have tried to change the activating and the time setting code piece but it is the same.
Somehow I have to reference the AnimationEvent? Is it a problem that this script is called multiple times, even from places where there is no AnimationEvent - I didn't receive any exceptions during my examination.
Any ideas? Thanks for helping!
I guess you have an animation playing and at the end you want to trigger the new scene.
You can use different approaches.
First, you add the animation event directly to the animation clip and you set the method in the inspector. The method has to be on a component attached to the same game object as the animation.
Second, you launch the animation and wait for the end of it to call the method:
void EndProcess()
{
StartCoroutine(EndProcessSequence());
}
IEnumerator EndProcessSequence()
{
Animation anim = GetComponent<Animation>();
anim.Play("animName");
yield return new WaitWhile(()=> anim.isPlaying);
LoadNewScene();
}
Here' s a reusable version
IEnumerator EndProcessSequence(string animName, Action onComplete)
{
Animation anim = GetComponent<Animation>();
anim.Play(animName);
yield return new WaitWhile(()=> anim.isPlaying);
onComplete?.Invoke();
}
I'm just starting out please excuse vast ignorance.
I'm writing a c# script in unity as part of the essentials training. I'm doing the 3d audio module and I thought I'd try and get a little bit fancier than the scope of this particular lesson which is supposed to be having an object fly through a window in a pre-built scene and make a 3d sound as it moves.
I wanted to make the movement of the object conditional upon a player moving close to it in 3d space. I figured out how to trigger the movement of an object in a script with an if statement that changes the transform parameters of the object the script is attached to when a 'distanceFromObject' variable is < 2. It works, however the script runs in the update section of the script which runs once every frame. This means that the object's transform parameters are changed every frame as expected but of course stops doing so when the distance between the object that's moving and the player exceeds 2.
I see the mistake I've made because if the object moves away when the player gets close then it will inevitably eventually move far enough away that the distanceFromObject variable will grow bigger than 2 whereupon it stops and just hovers in place. I don't know how to fix it though.
I need the script to check the distance between the object and the player every frame so that it will trigger the instance the player gets close enough, and when they get close enough, I need the object to move away, however once it has been triggered to move, I need the object to continue moving, but the script to stop checking what the distance is anymore.
The script looks like this
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlyOff : MonoBehaviour
{
public Vector3 rotateChange;
public Vector3 positionChange;
public float distanceFromObject;
public GameObject character;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
distanceFromObject = Vector3.Distance(character.transform.position, this.gameObject.transform.position);
print (distanceFromObject);
if (distanceFromObject < 2)
{
transform.Rotate (rotateChange);
transform.position += positionChange;
}
}
}
Use flags instead of writing your logic in the if statement :
public class FlyOff : MonoBehaviour
{
// fields removed for more readability
// use a flag that's set to true/false
private bool isCloseEnough = false;
void Update()
{
distanceFromObject = Vector3.Distance(character.transform.position, this.gameObject.transform.position);
print (distanceFromObject);
// set the flag to true when player is close enough
if (distanceFromObject < 2)
{
isCloseEnough = true;
}
// even if the player gets far, the flag will remain true
if (isCloseEnough)
{
transform.Rotate (rotateChange);
transform.position += positionChange;
}
}
}
You can even apply the opposite logic to stop the object to move away when it has reach a certain distance :
if (distanceFromObject < 2)
{
isCloseEnough = true;
}
else if (distanceFromObject > SomeValue)
{
isCloseEnough = false;
}
If I understand correctly you could just add a bool flag and set it once you are close enough. Then you can start moving and skip further distance checks but keep moving forever.
private bool flyAway;
void Update()
{
if(!flyAway)
{
distanceFromObject = Vector3.Distance(character.transform.position, transform.position);
print (distanceFromObject);
if (distanceFromObject < 2)
{
flyAway = true;
}
}
else
{
transform.Rotate (rotateChange);
transform.position += positionChange;
}
}
In general: Avoid using print every frame! Even if you user doesn't see the log in a built app it is still causing overhead!
pretty new here...
I am making a program that will allow a user to control a sprite (walking on a surface/jumping/falling - the usual... pretty basic i know)
To make the sprite jump in such a way, so that the human eye can actually see a rise and fall on the form, i need to slow the process by which the program translated the sprite upwards.
I decided to use a timer, not SLEEP because i don't want the whole program to freeze.
Here's what i came up with:
private void jump()
{
global.CharacterY = global.CharacterY - 1;
framer_Tick(null, new EventArgs()); //pause program without freezing
}
private void framer_Tick(object sender, EventArgs e)
{
sprite.Location = new Point(global.CharacterX, global.CharacterY);
}
Called by this:
private void Stage_KeyDown(object sender, KeyEventArgs e)
{
if (global.counter >= 1 & e.KeyCode.ToString() == "D")
{
global.CharacterX = global.CharacterX + 1;
jump();
}
if (e.KeyCode.ToString() == "W")
{
while (global.counter < 50)
{
jump();
global.counter = global.counter + 1;
}
global.counter = 0;
}
if (e.KeyCode.ToString() == "D")
{
global.CharacterX = global.CharacterX + 1;
sprite.Location =
new Point(global.CharacterX, global.CharacterY);
}
if (e.KeyCode.ToString() == "A")
{
global.CharacterX = global.CharacterX - 1;
sprite.Location =
new Point(global.CharacterX, global.CharacterY);
}
}
Now, the timer doesn't seem to have any effect. I assumed that placing the code to translate the sprite inside the timer would make it fire once every time the timer ticked.
- Unfortunately i don't have the experience make the timer pause the program (preferably 30 times a second, at an interval of 33(ish)) -
Simply changing the location of the sprite will not do anything. You have to call Invalidate() on whatever control the sprite is being drawn on to see the effect.
Also, you don't call framer_tick to get the process started. You have to call Start and Stop methods on the timer object. When you call Start, the tick handler will start getting called. When you call Stop, it will stop.
To make all of your animation smooth and logic less problematic your tick timer should be going off all the time because you should be redrawing the screen all the time. With the screen refreshing itself you just change the location of the sprite and the animation will behave as you expect it.
Simply changing the location of the sprite will not do anything. You have to call Invalidate() on whatever control the sprite is being drawn on to see the effect. Also, you don't call framer_tick to get the process started. You have to call start/stop on the timer object. When you call start, the tick handler will start getting called. When you call stop, it will stop. But I agree with #Chris. Your tick timer should be going off all the time because you should be redrawing the screen all the time. After that you just change the location of the sprite and everything will be fine. – Paul Sasik
In my pong game I'm making I have two paddles and a ball. I'm trying to make so when the ball collides with the paddle, an effect/animation is shown. I have a spritehseet and a working animation. I use this to detect when the animation plays (please notice that I havent fixed the position of the effect yet, so it plays on 400, 300 just to see if it works)
public bool BallHitEffect()
{
if (gPaddle.gpRect.Intersects(ball.ballRect))
{
return true;
}
else { return false; }
And in my Draw:
protected override void Draw(GameTime gameTime)
{
spriteBatch.Begin();
if (BallHitEffect())
{
animatedSprite.Draw(spriteBatch, new Vector2(400, 250));
}
}
Now it appears when it collide, but only for a short millisecond, because when the ball leaves the paddle, it disappears. I'm aware that it's coded to only appear when it is colliding with the paddle, but in what way could I possibly make it only disappear only when it's animation ends?
You can easily used elapsed time and some makeshift timers to do this. I assume your spritesheet is a series of frames, and that you already have rendering set up.
You will need some variables to start out:
double frameTimePlayed; //Amount of time (out of animationTime) that the animation has been playing for
bool IsPlaying; //Self-Explanitory
int frameCount; //Frames in your sprite, I'm sure you already handle this in your animation logic, but I just threw it in here
int animationTime = 1000; //For 1 second of animation.
In your update method you need to
Check if the ball is intersecting, and set IsPlaying to true
If the animation IsPlaying, increment the frameTimePlayed by the elapsed time
If the frameTimePlayed is equal to or greater than the animationTime, stop the animation by setting IsPlaying to false
In your draw method you need to
Draw the animation, if IsPlaying
Here is some example code:
protected override void Update(GameTime gameTime)
{
//If it is not already playing and there is collision, start playing
if (!IsPlaying && BallHitEffect)
IsPlaying = true;
//Increment the frameTimePlayed by the time (in milliseconds) since the last frame
if (IsPlaying)
frameTimePlayed += gameTime.ElapsedGameTime.TotalMilliseconds;
//If playing and we have not exceeded the time limit
if (IsPlaying && frameTimePlayed < animationTime)
{
// TODO: Add update logic here, such as animation.Update()
// And increment your frames (Using division to figure out how many frames per second)
}
//If exceeded time, reset variables and stop playing
else if (IsPlaying && frameTimePlayed >= animationTime)
{
frameTimePlayed = 0;
IsPlaying = false;
// TODO: Possibly custom animation.Stop(), depending on your animation class
}
}
And for drawing, pretty easy, just check is IsPlaying and draw if that is the case.
I made sure to comment the code good, so hopefully this will help you understand and work better.
You could also use a double and use TotalSeconds instead of milliseconds if needed, and calculate elapsed time with float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;