I'm currently working on a function supposed to create a fade in on a charged song...
It's barely working. The problem is that it's not respecting the duration of the fadeIn that we ask.
For example, if I mention a duration of 20sec and the other 50sec, it's doing the exact same fadeIn (which is lasting maybe 3sec...)
If you could help me, that would be great ! :)
Code:
[fichier,directory]=uigetfile('*.wav'); //cibler fichier
cd('C:\Users\elise\Documents\COURS\Scilab\ProTools'); //aller dans le dossier
[music, meta] = loadwave(fichier); //charger la musique
fqInit = meta(3); // initial frequency of the charged music
function [musicMod]=fadeIn(music,fqInit)
tpsFadeIn = input('Entrez le temps du fondu (en s) : '); //duration of fadeIn
nbEch = round(tpsFadeIn*fqInit); //number of sample concerned by fadeIn
cm = [0:nbEch-1]/nbEch; //multiplier
for k = 1 : size(music,1)-1
cm=[cm; cm];
end
cm = [cm, ones(size(music,1), size(music,2)-nbEch)];
musicMod = cm .* music; //modified music
playsnd(musicMod,fqInit);
endfunction
Related
I want to know is it necessary to set application frame rate in Unity's android game script?
What is the best frame rate for android ? 30 fps or 60 fps ?
What if we do not set it? is it affect on game performance ?
Because my game has lag in some devices and it's genre is First Person Shooter.
Could it be because I did not specify the frame rate of the game?
Because I've been optimizing my game scripts as far as I can.
And in unity's profiller I checked my scripts and i didn't see any serious problem.
But some time's I have lag when i turning around. I mean when player looks around, some times he rotate about 180 degree or more quickly when lag is happening.
Here is part of my mouse look code :
void Update()
{
the_time = Time.deltaTime;
if (Input.touches.Length > 0)
{
foreach (Touch t in Input.touches)
{
if (t.position.x > Screen.width / 2)
{
if (t.phase == TouchPhase.Began)
{
delta = t.deltaPosition;
}
if (t.phase == TouchPhase.Began || t.phase == TouchPhase.Moved)
{
delta = -t.deltaPosition;
rotX += (delta.y * sensitivityX * current_speed_offset_vertical * the_time);
rotY -= delta.x * sensitivityY * current_speed_offset * the_time;
rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
TlocalEurlar.y = rotY;
transform.eulerAngles = TlocalEurlar;
x_rot_transform_Eurlar.x = rotX;
x_rot_transform.localEulerAngles = x_rot_transform_Eurlar;
}
else if (t.phase == TouchPhase.Ended)
{
delta = t.deltaPosition;
}
}
}
}
}
Is there a problem with my script?
Can this problem be related to not setting the frame rate of the game?
I have a script in my simple platformer game which says that if my player is in the ground and "Z" is pressed, his movement in the Y axis is going to go up to 600, and if he's not in the ground, he's going to perform the jump animation.
So here's the thing, I know it plays only the first frame of the jumping animation because the code is constantly detecting that the player is in the air. I want a way to tell the code to trigger the animation only once.
I tried using a function called input_(event): but it seems that it doesn't have a is_action_just_pressed type of Input, just is_action_pressed.
I'm fairly new to Godot and don't know how to use signals. Signals might help via animation_finished(), although that function might have nothing to do with what I actually want to do in my code.
Here's my code:
extends KinematicBody2D
#Variables van aquĆ
var movimiento = Vector2();
var gravedad = 20;
var arriba = Vector2(0, -1);
var velocidadMax = 600;
var fuerza_salto = -600;
var aceleracion = 5;
var saltando = false;
func _ready(): # Esto es void Start()
pass;
func _physics_process(delta): #Esto es void Update()
movimiento.y += gravedad;
if Input.is_action_pressed("ui_right"):
$SonicSprite.flip_h = true;
$SonicSprite.play("Walk");
movimiento.x = min(movimiento.x +aceleracion, velocidadMax);
elif Input.is_action_pressed("ui_left"):
$SonicSprite.flip_h = false;
$SonicSprite.play("Walk");
movimiento.x = max(movimiento.x-aceleracion, -velocidadMax);
else:
movimiento.x = lerp(movimiento.x, 0, 0.09);
$SonicSprite.play("Idle");
if is_on_floor():
if Input.is_action_just_pressed("z"):
movimiento.y = fuerza_salto;
else:
$SonicSprite.play("Jump");
movimiento = move_and_slide(movimiento, arriba)
I had the smae problem, and it was solved for me adding the next after move_and_slide():
if velocity.y == 0:
velocity.y = 10
Apparently, if velocity is 0 after move_and_slide() it does not detect is_on_floor() anymore (bug?) so i added small velocity in direction of gravity.
About usinginput_(event), you do not need just_pressed because it only process when there is an input.. you can do somethin like this:
func _input(event):
if event.is_action_pressed("ui_up") and is_on_floor():
velocity.y = jump_speed
What i called velocity, i think in your script is called movimiento. Also, i think you are mixing gravities and velocities in movimiento. I'm sharing you my character scrpit so you can compare and see if it works better:
extends KinematicBody2D
var Bullet = preload("res://Bullet.tscn")
var speed = 200
var jump_speed = -300
var shot_speed = 100
var velocity = Vector2()
var grav = 980
var shooting = false
func _ready():
$AnimatedSprite.play()
$AnimatedSprite.connect("animation_finished",self,"on_animation_finished")
func _input(event):
if event.is_action_pressed("ui_up") and is_on_floor():
velocity.y = jump_speed
if event.is_action_pressed("shoot") and !shooting:
shooting = true
shot_speed = 20
velocity.y = -200
fire_weapon()
func _physics_process(delta):
velocity.x = 0
if Input.is_action_pressed("ui_right"):
velocity.x += speed
if Input.is_action_pressed("ui_left"):
velocity.x -= speed
if velocity.length() > 0:
if velocity.x < 0:
$AnimatedSprite.flip_v = true
$AnimatedSprite.rotation_degrees = 180
elif velocity.x > 0:
$AnimatedSprite.flip_v = false
$AnimatedSprite.rotation_degrees = 0
if shooting:
$AnimatedSprite.animation = "shot"
velocity.x = -shot_speed * cos($AnimatedSprite.rotation_degrees)
shot_speed *= 0.98
else:
if is_on_floor():
if velocity.x == 0:
$AnimatedSprite.animation = "idle"
else:
$AnimatedSprite.animation = "moving"
else:
$AnimatedSprite.animation = "jumping"
velocity.y += grav * delta
velocity = move_and_slide(velocity, Vector2(0,-1))
if velocity.y == 0:
velocity.y = 10
func on_animation_finished():
if $AnimatedSprite.animation == "shot":
shooting = false
func fire_weapon():
var bullet = Bullet.instance()
get_parent().add_child(bullet)
if $AnimatedSprite.flip_v :
bullet.position = $ShotLeft.global_position
else:
bullet.position = $ShotRight.global_position
bullet.rotation_degrees = $AnimatedSprite.rotation_degrees
bullet.linear_velocity = Vector2(1500 * cos(bullet.rotation),1500*sin(bullet.rotation))
Notice that i do not use gravity as a velocity or moviemiento; instead y multiply it by delta for getting velocity, as acceleration x time gives velocity.
I hope this helps.
This kind of problem can be solved using a Finite State Machine to manage your character controls and behaviour. You would play the Jump animation on entering the Jumping state from the Walking state. Utilizing correct design patterns early prevents spaghetti code in the future.
This is not a Godot specific solution, but definitely one worth your attention.
Let's dissect what's happening here, you fire up the code and ask your player to play the animation in one frame and then again in the other and in the next as long as the correct key is pressed or you in the correct state.
What the problem, the problem is that at every call of the function play it fires the animation again and again so it just restarts or another call runs separately and this causes unexpected behaviour.
The better way will be to manage the state of the player and animation player simultaneously and using that to perform animation calls.
enum State { IDLE, WALK, RUN, JUMP, INAIR, GROUNDED } ## This is to manage the player state
var my_state = State.IDLE ## Start the player with the idle state
As for the animation player state use the signal that you were talking about using the GUI or with code like below.
get_node("Animation Player").connect("animation_finished", this, "method_name")
## Here I assume that you have the Animation Player as the Child of the Node you the script on
And also hold a boolean variable to tell if the animation is playing or not.
if ( animation_not_playing and (case)):
animation_player.play("animation")
Turn it true or false as per your liking. Based on the animation finished signal.
In future, you might want to consider even using a simple FSM to maintain all this state data and variables.
I decided I wanted to learn how to work with the unity2D engine, and started with trying to make pong. This was going pretty good, until I found a problem I couldn't find/didn't understand an answer for on google .
Every time the player/AI hits the ball, I make the ball go a little bit faster. This works fine until the ball goes pretty fast (still playable though) and just passes through the player/AI. I solved this by making the box collider of the player/AI really long, but at really high (and unplayable) speeds it still goes through.
My solution works, but isn't that pretty, and I wonder if there is a better solution for this (make the engine check more often for collisions?).
Here's the script for the ball movement (Javascript):
#pragma strict
var StartSpeed : int;
var speedFactor : float;
function Start () {
yield WaitForSeconds(2);
StartBall();
}
function ResetBall () {
GetComponent.<Rigidbody2D>().velocity.x = 0;
GetComponent.<Rigidbody2D>().velocity.y = 0;
transform.position.x = 0;
transform.position.y = 0;
yield WaitForSeconds(0.5);
StartBall();
}
function StartBall () {
var randomDirection = Random.Range(0f,1f);
var randomAngle = Random.Range(-Mathf.PI/4, Mathf.PI/4);
if(randomDirection < 0.5f){
GetComponent.<Rigidbody2D>().velocity.x = Mathf.Cos(randomAngle) * StartSpeed;
GetComponent.<Rigidbody2D>().velocity.y = Mathf.Sin(randomAngle) * StartSpeed;
}else{
GetComponent.<Rigidbody2D>().velocity.x = - Mathf.Cos(randomAngle) * StartSpeed;
GetComponent.<Rigidbody2D>().velocity.y = Mathf.Sin(randomAngle) * StartSpeed;
}
}
function OnCollisionEnter2D (colInfo : Collision2D) {
if(colInfo.collider.tag == "Player"){
GetComponent.<Rigidbody2D>().velocity.x = speedFactor * GetComponent.<Rigidbody2D>().velocity.x;
if(colInfo.collider.GetComponent.<Rigidbody2D>().velocity.y == 0){
GetComponent.<Rigidbody2D>().velocity.y = speedFactor * GetComponent.<Rigidbody2D>().velocity.y;
}
var vel = GetComponent.<Rigidbody2D>().velocity;
Debug.Log("Speed: " + vel);
}
}
Any other comments on the script that may improve it are welcome!
EDIT: I tried the following (as Andrew suggested):
function OnCollisionEnter2D (colInfo : Collision2D) {
if(colInfo.collider.tag == "Player"){
GetComponent.<Rigidbody2D>().AddForce( Vector2 (speedFactor * GetComponent.<Rigidbody2D>().velocity.x, speedFactor * GetComponent.<Rigidbody2D>().velocity.y));
var vel = GetComponent.<Rigidbody2D>().velocity;
Debug.Log("Speed: " + vel);
}
}
This still causes the problem I had before.
Update your RigidBody settings and set Collision Detection to Continuous (it will probably be set to discrete) and your high speed collision will work fine.
You shouldn't be messing with the velocity directly, try just using AddForce() instead.
Whole physics including collision detection runs on FixedUpdate, so to actually detect any collision colliders must collide when FixedUpdate is called. Let's say one collider isn't moving (wall for example) and another is going right at it, on current call of FixedUpdate collider that is moving is just before the wall, while on the next call of FixedUpdate collider that is moving has passed the wall, because that is it's position step per frame. Visually we see that colliders did collide, but they didn't collide on any call to FixedUpdate. Now, there are two solutions to this, lower the speed or lower the timestep of FixedUpdate ( http://docs.unity3d.com/Manual/class-TimeManager.html ), but this can be bad for framerate, it all depends what machines are you targeting and how hardware hungry your game is.
There is also this open source script which you should look at :
http://wiki.unity3d.com/index.php?title=DontGoThroughThings#C.23_-_DontGoThroughThings.js
I have a WinForm in VB.Net with some images at the background which change every 5 seconds by a timer. The problem is that they change immediately and this does not look nice. I would like to add some fade-in effects, but I got the error that opacity is not a property for tableLayout.backgroundimage. I can only fade the form, but that's not what I want.
Here is my code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If imageNum > 3 Then
imageNum = 1
End If
Select Case imageNum
Case 1
Me.TableLayoutPanel4.BackgroundImage = Global.posta1.My.Resources.Resources.foto1
For FadeIn = 0.0 To 1.1 Step 0.1
Me.Opacity = FadeIn
Threading.Thread.Sleep(100)
Next
rd_btn1.Checked = False
rd_btn2.Checked = True
Case 2
Me.TableLayoutPanel4.BackgroundImage = Global.posta1.My.Resources.Resources.foto3
rd_btn2.Checked = False
rd_btn3.Checked = True
For FadeIn = 0.0 To 1.1 Step 0.1
Me.Opacity = FadeIn
Threading.Thread.Sleep(100)
Next
Case 3
Me.TableLayoutPanel4.BackgroundImage = Global.posta1.My.Resources.Resources.foto2
For FadeIn = 0.0 To 1.1 Step 0.1
Me.Opacity = FadeIn
Threading.Thread.Sleep(100)
Next
rd_btn3.Checked = False
rd_btn1.Checked = True
End Select
imageNum = imageNum + 1
End Sub
As far as I do know it's not possible to set the opacity of a single control, maybe you can do it by drawing the control yourself but you have to research about that because I have no knowledge about drawing controls yourself..
I'm a novice but what I would think would work would be to draw (using the PaintEventArgs argument of the paint event of the control) to do this:
Draw both the old and the new pic.
Lessen the alpha value of the old pic, whilst increasing the new pic's alha value, until they are 0.0, and 1.0
Hope this helped. Sorry if it didn't.
;)
I don't understand why the width function is implemented on all elements if it returns 0 for non-zero width elements. The following returns 0 for me.
Shoes.app do
p = para "My width is: "
para p.width
end
Why is that? (app.width does not return 0)
The problem is that the size of the para object is determined dynamically when it is drawn. At the time you create the second para, nothing has actually been laid out yet, so a width hasn't been set. You can see that accessing the width after drawing works as expected:
Shoes.app do
p = para "My width is: "
#para = para p.width
button 'Get Width' do
#para.text = p.width
end
end
The way to get around this is to use the start method, which is called when the containing slot is drawn for the first time:
Shoes.app do
p = para "My width is: "
width = para p.width
start do
width.text = p.width
end
end