Jump to content
Unity Insider Forum
Sign in to follow this  
Iboshido

flugbahn berechnen 2D

Recommended Posts

Hey Leute,

ich habe ein Gameobjekt "ball", welches aus einer Kanone geschossen wird. Ich habe zur Berechnung der Flugbahn des Balles einen Code aus dem Netz. Es hat wunderbar funktioniert, bis ich ein paar weitere Zeilen Code im ball hinzugefügt habe, damit der ball eine realistischeres physikalisches verhalten hat. Nun funktioniert aber natürlich die Berechnung der Flugbahn nicht mehr, da das neue physikalische Verhalten des balls nicht in die Berechnung der Flugbahn geht. 

Code um die Flugbahn zu Berechnen:

void Update()
{
 
for (int k = 0; k < numberOfDots; k++)
{                        
   x1 = ballPos.x + shotForce.x * Time.fixedDeltaTime * (dotSeparation * k + dotShift);  
   y1 = ballPos.y + shotForce.y * Time.fixedDeltaTime * (dotSeparation * k + dotShift) - (-Physics2D.gravity.y / 2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (dotSeparation * k + dotShift) * (dotSeparation * k + dotShift));  
                dots[k].transform.position = new Vector3(x1, y1, dots[k].transform.position.z);
}

 

 

Code um dem Ball eine realistischeres physikalisches Verhalten zu geben:

void Update()
    {
        if (rb2d.velocity.y < 0)
        {
            rb2d.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
        }
        else if (rb2d.velocity.y > 0)
        {
            rb2d.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime;
        }
 
    }

 

 

flugbahn.jpg

Share this post


Link to post
Share on other sites

Könntest du das gesamte Script fürs Berechnen der Flugbahn posten? Ich weiß z.B, gerade ned woher dotSeparation und dotShift.

Und was ich auch nicht was der z-Wert von

 dots[k].transform.position = new Vector3(x1, y1, dots[k].transform.position.z);

machen soll :)

Wenn ichs verstehe können wirs bestimmt zsm lösen :)

Share this post


Link to post
Share on other sites

danke für deine Hilfe 😁

der gesamte code für die Flugbahn sieht wie folgt aus

public class trajectoryScript : MonoBehaviour
{
    private float dotShift;                      //set to 5. How far the first dot is from the "ball"
    private float dotSeparation;                // set to 3. How far each dot is seperateted from each other
    private int numberOfDots;                   // set to 5. How many dots should be desplayed. Max 40. 
    public GameObject trajectoryDots;          //The parent of all the points representing the trajectory
    private GameObject ball;                    //The projectile the player will be shooting
    private Rigidbody2D ballRB;                 //The Rigidbody2D attached to the projectile the player will be shooting
    private Vector3 ballPos;                    //Position of the ball
    private Vector2 shotForce;                  //How much velocity will be applied to the ball
    private float x1, y1;                       //X and Y position which will be applied to each point of the trajectory
    public float shootingPowerX;                //set to 12. The amount of power which can be applied in the X direction
    public float shootingPowerY;                //set to 12. The amount of power which can be applied in the Y direction
    private GameObject[] dots;                   //The array of points that make up the trajectory

    public bool isInCannon = false;
    private Transform shootingDiraction;
    private Vector3 ballArrowDiff;
    private GameObject btn_shootBall;


    void Start()
    {
        if(gameObject.name == "Kanone")
            shootingDiraction = transform.parent.gameObject.transform.GetChild(0).gameObject.transform;
        else
            shootingDiraction = transform.GetChild(0).gameObject.transform;

        ball = GameObject.Find("Ball");
        btn_shootBall = GameObject.Find("Canvas").transform.Find("btn_shootBall").gameObject;
        isInCannon = false;
        
        ballRB = ball.GetComponent<Rigidbody2D>();                      

        if (trajectoryDots == null)
            Debug.Log("trajectoryDots is null");

        dots = trajectoryDots.GetComponent<MaskScript>().dots;

        dotSeparation = trajectoryDots.GetComponent<MaskScript>().dotSeparation;
        numberOfDots = trajectoryDots.GetComponent<MaskScript>().numberOfDots;
        dotShift = trajectoryDots.GetComponent<MaskScript>().dotShift;


    }

   

    void Update()
    {
        if (ball != null) 
        {
            ballPos = ball.transform.position;                                    
            ballArrowDiff = shootingDiraction.transform.position - ballPos; 
            shotForce = new Vector2(ballArrowDiff.x * shootingPowerX, ballArrowDiff.y * shootingPowerY);
        }

        if (isInCannon)
        {
            skr_ballShootControler.instance.isballReadyForshoot = true;
            ballRB.constraints = RigidbodyConstraints2D.FreezeAll;
            trajectoryDots.SetActive(true);                             //Display the trajectory
            btn_shootBall.SetActive(true);
            btn_shootBall.transform.position = Camera.main.WorldToScreenPoint(this.transform.position);


            for (int k = 0; k < numberOfDots; k++)
            {                           //Each point of the trajectory will be given its position
                x1 = ballPos.x + shotForce.x * Time.fixedDeltaTime * (dotSeparation * k + dotShift);    //X position for each point is found
                y1 = ballPos.y + shotForce.y * Time.fixedDeltaTime * (dotSeparation * k + dotShift) - (-Physics2D.gravity.y / 2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (dotSeparation * k + dotShift) * (dotSeparation * k + dotShift));    //Y position for each point is found
                dots[k].transform.position = new Vector3(x1, y1, dots[k].transform.position.z); //Position is applied to each point
            }

           
            if (skr_ballShootControler.instance.isShooting) // if ball is inside the cannon
            {  
                isInCannon = false;                                         
                skr_ballShootControler.instance.isShooting = false;
                btn_shootBall.SetActive(false);


                if (trajectoryDots.activeInHierarchy)
                {                           //If the player was aiming...
                    ballRB.constraints = RigidbodyConstraints2D.None;
                    trajectoryDots.SetActive(false);                                //The trajectory will hide
                    ballRB.velocity = new Vector2(shotForce.x, shotForce.y);    //The "ball" will have its new velocity
                    if (ballRB.isKinematic == true)
                    {                           //If the "ball" was kinematic...
                        ballRB.isKinematic = false;                             //It's no longer kinematic
                    }
                }
                
            }


        }
       
    }

 

Share this post


Link to post
Share on other sites

 Ich denke man muss nur diese Zeile anpassen(wie du ja selbst schon vermutet hast) :

y1 = ballPos.y + shotForce.y * Time.fixedDeltaTime * (dotSeparation * k + dotShift) - (-Physics2D.gravity.y / 2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (dotSeparation * k + dotShift) * (

Hast du die Zeile selber geschrieben? Weil Time.deltaTime*Time.deltaTime macht für mich irgendwie wie keinen Sinn :P(falls dus erklären kannst)

Ich glaube du musst in der for Schleife mit einem If-Statement unterscheiden ob der Ball prositive rb2d.velocity.y hat oder eine negative und jenachdem welcher Fall eintritt du die yAxis der Dots anders berechnen musst.  

Share this post


Link to post
Share on other sites

Ne hab den code nicht selber geschrieben, habe ihn aus dem Netz.

Ich verstehe leider nicht ganz wie ich das, was du sagst umsetzen kann 😅

Share this post


Link to post
Share on other sites
Dein Code
for (int k = 0; k < numberOfDots; k++)
            {                           //Each point of the trajectory will be given its position
                x1 = ballPos.x + shotForce.x * Time.fixedDeltaTime * (dotSeparation * k + dotShift);    //X position for each point is found
                y1 = ballPos.y + shotForce.y * Time.fixedDeltaTime * (dotSeparation * k + dotShift) - (-Physics2D.gravity.y / 2f * Time.fixedDeltaTime * Time.fixedDeltaTime * (dotSeparation * k + dotShift) * (dotSeparation * k + dotShift));    //Y position for each point is found
                dots[k].transform.position = new Vector3(x1, y1, dots[k].transform.position.z); //Position is applied to each point
            }
              
                                 
 /*Du musst bei y1 unterschieden ob rb2d.velocity.y < 0 oder rb2d.velocity.y > 0 ist weil es ist ja ein unterschied ob der Ball hoch oder runter fliegt
 Somit musst  du das mit nem if Statement an der Stelle unterscheiden*/
                                 
for (int k = 0; k < numberOfDots; k++)
            {                           //Each point of the trajectory will be given its position
                x1 = ballPos.x + shotForce.x * Time.fixedDeltaTime * (dotSeparation * k + dotShift);    //X position for each point is found
           		 if(rb2d.velocity.y < 0) {
				//y1 = berechnung für die flugbahn rb2d.velocity.y < 0
				}
		   if(rb2d.velocity.y > 0){
				//y1 = berechnung für die flugbahn rb2d.velocity.y > 0
				}
                dots[k].transform.position = new Vector3(x1, y1, dots[k].transform.position.z); //Position is applied to each point
            }

Hoffe so ist es verständlicher :3 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...