0
\$\begingroup\$

I have a platform for catching a ball.

enter image description here

Platform physics material has bounciness value of 2 which pushes ball twice high from compared to its original position. Camera and platform have scripts which make them follow the ball. The platform can be moved on x-axis left and right. problem is. When the ball has acceleration up, I can't move the platform. Here are scripts.

FollowingPlatform script:

[SerializeField] private Transform targetToFollow;
[SerializeField] private PlatformConfigs platformConfigs;

void Start()
{

}

void Update()
{
    if (!targetToFollow)
    {
        Debug.Log("targetToFollow == null");
    }
    else
    {
        MoveUp();
    }
}

private void MoveUp()
{
    if (targetToFollow.position.y <
        transform.position.y + platformConfigs.distanceToTargetY)
        return;

    transform.position =
        new Vector2(transform.position.x,
        targetToFollow.position.y - platformConfigs.distanceToTargetY);

}

Platform controls script:

[SerializeField] private PlatformConfigs platformConfigs;

[SerializeField] private Rigidbody2D rb;

private Vector2 moveInput;

private Vector2 moveVelocity;

void Start()
{
    if (!rb)
        rb = GetComponent<Rigidbody2D>();
}

void Update()
{
    moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), 0);
    moveVelocity = moveInput.normalized * platformConfigs.movementSpeed;
}

private void FixedUpdate()
{
    rb.position = new Vector2
    (
        Mathf.Clamp(rb.position.x,
        - platformConfigs.xMoveRestriction, platformConfigs.xMoveRestriction),
       rb.position.y
    );

    rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);      
}

I have also tried to disable FollowingPlatform script and attach the platform to the camera as a child. But the camera has a similar FollowingCamera script which makes the camera follow the ball. Effect stays the same. I can't move platform then the ball accelerates up.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.