Unity Parabola, motion trajectory at average speed

Source: Internet
Author: User

Before sharing the "golden finger" implementation of the parabola between two points, then a friend asked me, in general, will give the speed, how to simulate the natural trajectory.

I hear this is not very easy to achieve, according to the previous formula, two points between the time is constant, the trajectory is determined, that is, the average speed is constant.

Then in turn, at a given average speed, and then by distance/velocity, the time can be drawn, then the trajectory is determined.

OK, I don't have much nonsense, just on the code:

usingUnityengine;usingSystem.Collections; Public classpaowuline:monobehaviour{ Public floatShotspeed =Ten; Private floatTime =1;//represents the length of time from point A to B     PublicTransform Pointa;//Point A     PublicTransform POINTB;//Point B     Public floatg =-Ten;//Gravitational Acceleration//Use this for initialization    PrivateVector3 speed;//Initial velocity Vector    PrivateVector3 Gravity;//Gravity vector    PrivateVector3 Currentangle; voidStart () { time= Vector3.distance (pointa.position, pointb.position)/Shotspeed; Transform.position= Pointa.position;//Place The object at point a//calculate the initial velocity by a formulaSpeed =NewVector3 ((pointb.position.x-pointa.position.x)/Time , (POINTB.POSITION.Y-POINTA.POSITION.Y)/Time-0.5f* g * time, (POINTB.POSITION.Z-POINTA.POSITION.Z)/Time ); Gravity= Vector3.zero;//Gravity initial velocity is 0    }    Private floatDTime =0; //Update is called once per frame    voidfixedupdate () {gravity.y= G * (dTime + = time.fixeddeltatime);//V=at//Analog DisplacementTransform.position + = (speed + Gravity) *Time.fixeddeltatime; Currentangle.x=-mathf.atan ((speed.y + gravity.y)/speed.z) *mathf.rad2deg; Transform.eulerangles=Currentangle; }}

This time directly to add the angle, like friends can test their own.

Time-constant two-point trajectory: http://www.cnblogs.com/jqg-aliang/p/4806017.html

General parabolic trajectory: http://www.cnblogs.com/jqg-aliang/p/4806002.html#3292517

Well, the simple implementation algorithm for Angry Birds, bows and arrows is almost enough. I've been studying the missile flight algorithm for a long time, and there's a new direction,

The use of parabolic simulations of dynamic trajectories will have a more realistic and natural effect. Welcome attention.

Unity Parabola, motion trajectory at average speed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.