我的场景中有2个球,我希望它们以一定的速度运动并随着时间的推移变得更高。 当我让它们沿一条直线移动时,它们保持不变,但是一旦我开始在x轴上移动它们,它们便会停留在我未移动的那根后面。 请帮帮我。 这是代码。
无效更新() { 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;
}