Hello,
I am calculating smoothed change of the speed for my spawning objects. They move towards my player and there is an impression that player falls through a pipe sort of. There are obstacles and powerups for him to collide with. All the objects that are "collidable" are instantiated the same way. I create them on a location and, by adding force (in one case I use translate though), moving them towards my player. One of my features - to decrease "falling" speed, so to decrease force or translate value for the objects. I want to do it gradually and I use Mathf.SmoothDamp. If my player collides with "net", the script receives a value in the function and decreases the speed (or force) value. Based on that I start to compare whether the current speed is less then maxSpeed(the normal speed) and if it is, I increase it smoothly. The problem is, that I have 4 variables for speed of different objects, I apply the same method to all of them, but the results are different. THe SmoothDamp calculation works different while base on the same conditions. To be precise, while the smoothTime variable is everywhere the same, the actual time that is taken for transition from current to target is different. Slower for two first variables. How is that possible??? THe script:
using UnityEngine;
using System.Collections;
public class VelocityController : MonoBehaviour {
[HideInInspector]
public float blocksVelocity = 60.0f;
[HideInInspector]
public float obstacleForce = 3000.0f;
[HideInInspector]
public float clocksForce = 3000.0f;
[HideInInspector]
public float netsForce = 3000.0f;
float blocksVelocityMax = 60.0f;
float obstacleForceMax = 3000.0f;
float clocksForceMax = 3000.0f;
float netsForceMax = 3000.0f;
void Start ()
{
}
void Update ()
{
ConstantIncreaseSpeed(blocksVelocityMax, obstacleForceMax, clocksForceMax, netsForceMax);
}
public void CalculateDesiredSpeed(float num)
{
blocksVelocity = blocksVelocity / num;
obstacleForce = obstacleForce / num;
clocksForce = clocksForce / num;
netsForce = netsForce / num;
}
public void ConstantIncreaseSpeed(float maxSpeed1, float maxSpeed2, float maxSpeed3, float maxSpeed4)
{
float currentSpeed1;
float currentSpeed2;
float currentSpeed3;
float currentSpeed4;
float velocity = 0.0f;
float smoothTime = 0.2f;
currentSpeed1 = blocksVelocity;
currentSpeed2 = obstacleForce;
currentSpeed3 = clocksForce;
currentSpeed4 = netsForce;
maxSpeed1 = blocksVelocityMax;
maxSpeed2 = obstacleForceMax;
maxSpeed3 = clocksForceMax;
maxSpeed4 = netsForceMax;
if(currentSpeed1 < maxSpeed1)blocksVelocity = Mathf.SmoothDamp(currentSpeed1, maxSpeed1, ref velocity, smoothTime);
if(currentSpeed2 < maxSpeed2)obstacleForce = Mathf.SmoothDamp(currentSpeed2, maxSpeed2, ref velocity, smoothTime);
if(currentSpeed3 < maxSpeed3)clocksForce = Mathf.SmoothDamp(currentSpeed3, maxSpeed3, ref velocity, smoothTime);
if(currentSpeed4 < maxSpeed4)netsForce = Mathf.SmoothDamp(currentSpeed4, maxSpeed4, ref velocity, smoothTime);
}
}
↧