0
\$\begingroup\$

I have a prefab that is moving from one point to another thanks to MoveTowards().

Currently, I am having trouble with a functionality where the player can press a button and make the object jump, affecting only the Y coordinate.

I tried doing the following before putting the MoveTowards() code down:

void Start()
{
    playerInstance = Instantiate(playerPrefab, startPos.position, Quaternion.LookRotation(endPos.position), null);
    speed = Random.Range(2f, 8f);

    currentPlayerInstancePositionY = playerInstance.transform.position.y;
    isGrounded = true;
}

void Update()
{
    if (isGrounded)
    {
        if (Input.anyKeyDown)
        {
            playerInstancePositionY = jumpForce;
        }
        else
        {
            playerInstancePositionY = currentPlayerInstancePositionY;
        }
    }
    else
    {
        playerInstancePositionY -= gravity * Time.deltaTime;
    }

    currentPlayerInstancePositionY = playerInstancePositionY;

    playerInstance.transform.position = Vector3.MoveTowards(playerInstance.transform.position, endPos.position, speed * Time.deltaTime);

I just used custom gravity to decide when the object falls. But the problem I found was that MoveTowards() takes a Start Position, an End Position and a Float in its method. But it cannot change individual coordinate values.

As the only control you have over the object is making it jump, I was wondering if there was another way to implement this since the Y value is not being inserted into the prefab before it is given the MoveTowards() function. I am trying to implement the jump mechanic without any RigidBodies, CharacterControllers or Raycasts.

Any help would be appreciated.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Show us your whole movement method, including the place where you call MoveTowards. We need this context to be able to test solutions and have confidence that they'll work in your version. \$\endgroup\$
    – DMGregory
    Commented Jul 30, 2019 at 22:55
  • 1
    \$\begingroup\$ "I am trying to implement the jump mechanic without any RigidBodies, CharacterControllers or Raycasts" - Why? \$\endgroup\$
    – Philipp
    Commented Jul 31, 2019 at 8:56
  • \$\begingroup\$ @DDGamer83 Instead of posting or linking code in comments, you can just edit your question and add the code. I did that for you. \$\endgroup\$
    – Philipp
    Commented Jul 31, 2019 at 20:06

1 Answer 1

1
\$\begingroup\$

transform.MoveTowards makes the object move closer on all axis'. In this case you seem to only want to move it on the x-axis (and z-axis if this is a 3d game), but not on the y-axis, so MoveTowards is counter-productive here.

If you really want to keep using MoveTowards, then I would split this game-object into two, a parent and a child. The parent game object would representing the root-position on the floor which you move with MoveTowards. If you move the parent, you also move the child. The child-object would be used to handle the jumping offset. The jumping-script would change its transform.localPosition - the offset from the parent. The child object would be the one with the renderer.

But what I would recommend is to stop using MoveTowards and only change the x-coordinate (and optionally z-coordinate) in the "move towards target" code.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .