Search Unity

Parabolic movement without Physics

Discussion in 'Getting Started' started by Meonaar, Jan 28, 2020.

  1. Meonaar

    Meonaar

    Joined:
    Oct 26, 2018
    Posts:
    2
    Hello! I just stuck on something. I read I think every post in here and watched dozens of videos but still dont get it.

    I have a Player. When I click mouse on a ground behind Player he have to move in that point parabolic way. Almost like cannonball, arrows and some other stuff, but not projectile.

    For now my Player moving with delay, but it's because of GetMouseButtonDown, so I need some way to fix this too.

    Code (CSharp):
    1. Vector3 targetPos;
    2.  
    3.     float speed = 5f;
    4.     float height = 1f;
    5.  
    6.     // Update is called once per frame
    7.     void Update()
    8.     {
    9.         targetPos = Input.mousePosition;
    10.         targetPos = Camera.main.ScreenToWorldPoint(targetPos);
    11.  
    12.         float x0 = transform.position.x;
    13.         float x1 = targetPos.x;
    14.         float distance = x1 - x0;
    15.         float nextX = Mathf.MoveTowards(transform.position.x, x1, speed * Time.deltaTime);
    16.         float baseY = Mathf.Lerp(transform.position.y, targetPos.y, (nextX - x0) / distance);
    17.         float parabole = height * (nextX - x0) * (nextX - x1) / (-0.25f * distance * distance);
    18.  
    19.         Vector3 nextPos = new Vector3(nextX, baseY + parabole, transform.position.z);
    20.  
    21.         if (Input.GetMouseButton(0))
    22.         {
    23.             transform.position = nextPos;
    24.         }
    25.  
    26.     }
    This is my code for this part of script. Can you suggest me better solution? I don't want to use Unity physics (addForce) for this.
     
    Last edited: Jan 28, 2020