Unity launches bow and arrow trajectory implementation

Source: Internet
Author: User

Whether it is angry birds, or bow firing function, or the simulation of the projectile is affected by gravity and other parabolic trajectory, can use the method of this article to simulate the absolute truth.

As always, say the principle first. Is the parabolic movement, in the vertical direction of the acceleration movement, in the horizontal direction, do uniform motion.

The specific implementation on unity is to use transform for displacement simulations. As for why we do not use the physical simulation of rigid bodies, we do our own brain repair or testing.

So how do you use transform simulations? Let the object at the same time in two direction of displacement in the line, one is the initial velocity direction, one is the vertical direction.

The velocity in the vertical direction = A*t,a represents the acceleration of gravity and t represents the elapsed time.

OK now let's see how a simple bow trajectory is generated, on the code:

usingUnityengine;usingSystem.Collections;/// <summary>///Archery Trajectory Simulation///Loang Design, Welcome to exchange experience/// </summary> Public classTestray:monobehaviour { Public floatPower=Ten;//this represents the speed/intensity of the launch, which can be used to simulate different force sizes .     Public floatangle= $;//The angle of the launch, this doesn't have to be explained.     Public floatGravity =-Ten;//this represents the gravitational acceleration    PrivateVector3 Movespeed;//Initial velocity Vector    PrivateVector3 grityspeed = Vector3.zero;//velocity vector of gravity, T at 0    Private floatDTime;//the time has passed//Use this for initialization    voidStart () {//calculates the initial velocity vector by a formula//Angle * ForceMovespeed = Quaternion.euler (NewVector3 (-angle,0,0)) * Vector3.forward *Power; }        //Update is called once per frame    voidfixedupdate () {//calculate the gravity velocity of an object//v = at;GRITYSPEED.Y = Gravity * (dTime + =time.fixeddeltatime); //Displacement Simulation TrajectoryTransform. Translate (Movespeed *time.fixeddeltatime); Transform. Translate (Grityspeed*time.fixeddeltatime); }}

How simple is it? Give the script to an object directly, and the effect is there. But wait a moment, this pure displacement simulation is correct, but the object movement angle change is not, is not like the bow and arrow!

To solve this, is actually very simple, I believe you can write it.

How to simulate resistance, I do not have to explain, in the course of operation, the vertical velocity and the initial velocity in the horizontal direction of the speed component gradually reduced (pay attention to the value range) on the line.

Good night, everybody.

This article link: http://www.cnblogs.com/jqg-aliang/p/4806002.html, reprint please declare the source, thank you!

Unity launches bow and arrow trajectory implementation

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.