У меня на сцене 2 мяча, и я хочу, чтобы они двигались с определенной скоростью и со временем становились выше.Когда я отпускаю их по прямой линии, они остаются прежними, но как только я начинаю перемещать их по оси x, они остаются за той, которую я не двигал.Пожалуйста, помоги мне.ЗДЕСЬ КОД.
void Update () {ray ();
// If the game is paused, don't do anything
if (PauseScreenBehaviour.paused)
return;
// Movement in the x axis
float horizontalSpeed = 0;
//Check if we are running either in the Unity editor or in a
//standalone build.
#if UNITY_STANDALONE || UNITY_WEBPLAYER
// Check if we're moving to the side
horizontalSpeed = Input.GetAxis("Horizontal") * dodgeSpeed;
// If the mouse is held down (or the screen is tapped
// on Mobile)
if (Input.GetMouseButton(0))
{
horizontalSpeed = CalculateMovement(Input.mousePosition);
}
//Check if we are running on a mobile device
#elif UNITY_IOS || UNITY_ANDROID
if(horizMovement == MobileHorizMovement.Accelerometer)
{
// Move player based on direction of the accelerometer
horizontalSpeed = Input.acceleration.x * dodgeSpeed;
}
// Check if Input has registered more than zero touches
if (Input.touchCount > 0)
{
// Store the first touch detected.
Touch touch = Input.touches[0];
// Uncomment to use left and right movement
//horizontalSpeed = CalculateMovement(touch.position);
if(horizMovement == MobileHorizMovement.ScreenTouch)
{
horizontalSpeed = CalculateMovement(touch.position);
}
}
#endif
var movementForce = new Vector3(horizontalSpeed, 0, rollSpeed);
// Time.deltaTime is the amount of time since the // last frame (approx. 1/60seconds)
movementForce *= (Time.deltaTime * 60);
// Apply our auto-moving and movement forces rb.AddForce(movementForce);
rb.AddForce(horizontalSpeed, 0, rollSpeed);
}
/// Will figure out where to move the player horizontally
/// <param name="pixelPos">The position the player has
/// touched/clicked on<
/// <returns>The direction to move in the x axis</returns>
float CalculateMovement(Vector3 pixelPos)
{
// Converts to a 0 to 1 scale
var worldPos = Camera.main.ScreenToViewportPoint(pixelPos);
float xMove = 0;
// If we press the right side of the screen
if (worldPos.x < 0.5f)
{
xMove = -1;
}
else
{
// Otherwise we're on the left
xMove = 1;
}
// replace horizontalSpeed with our own value
return xMove * dodgeSpeed;
}