• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DevMerlin · Feb 04, 2014 at 12:43 AM · rotationtransformmovement scriptparabola

With power and angle, what is the best way to create a parabolic arc?

I'm creating a custom controller for a Launch-type game. My opening sequence is already scripted and produces two variables: Angle, and Power. I do not want to use this with a Rigidbody and physics but create the actual parabolic arc and movement myself - I'm targeting lower-end devices. What kind of formula, or script would be best for this method?

EDIT: My goal is to do this using transform. I've already set up the smoothing in order to handle the movement.

 transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * smooth);
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nesis · Feb 04, 2014 at 01:50 AM

I'm not sure if you know this, but if you have a non-kinematic Rigidbody and set its velocity and let gravity influence it, it'll follow a parabolic arc. There are simple, specific equations you can use to calculate a projectile's trajectory.

For a slightly more lightweight solution, could alternatively define a class similar to this (in C#):

 public class MyProjectile : MonoBehaviour {
     public Vector3 gravity = new Vector3(0f,-9.81f,0f); //could instead use Physics.gravity
     private Vector3 velocity;
     
     public void Awake() {
         enabled = false; //start off disabled
     }
     
     public void FixedUpdate() {
         //apply gravity
         velocity += gravity*Time.fixedDeltaTime;
         
         /*if you're wanting to collide with things, here would 
         be a good place to perform physics sweep tests / raycasts
         as needed to know where to stop your object and make it 
         bounce*/
         
         transform.position += velocity*Time.fixedDeltaTime;
     }

     //call this from whatever script / method you want to 
     //initialise a projectile from. Eg, if done in another C# 
     //script, where projectilePrefab is a prefab you've made 
     //with this script attached:
     //GameObject o = (GameObject)Instantiate(projectilePrefab);
     //MyProjectile p = o.GetComponent<MyProjectile>();
     //if (p) {
     //    p.Initialise(somePower,someQuaternionAngle);
     //}
     public void Initialise(float power, Quaternion angle) {
         //here, power is speed (ie the magnitude of velocity)
         velocity = angle * Vector3.forward * power;
         enabled = true; //enable this script's update methods once we've initialised
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Following another object's position/rotation like parent/child relationship? 4 Answers

Smooth Rotation Help 1 Answer

Rotate Around Planet with Spaceship Face Set on Path 1 Answer

How to rotate a projectile to face the player in 2D? 1 Answer

Quaternion Slerp Sanity Check 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges