Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 thatbigguy · May 23, 2011 at 03:49 PM · 2dphysicsgravityangle

How can I make a game object move in parabolic motion as if it were under gravity?

I am modifying a shooting game where instead of the bullet traveling straight up, I want it to act under gravity and move in a parabolic motion. Right now, I have the bullet moving in a straight line with a positive slope. Is there any way I can change my code for the bullet to make it behave this way? My game is in 2D and also uses the X-axis and Y-axis. Here is the bulletScipt:

var bulletSpeed : int;

var bulletAngle: float;

var explosion : Transform;

useGravity = true;

function FixedUpdate () { amtToMove = bulletSpeed * Time.deltaTime;

transform.Translate(Vector3.right + Vector3.up * amtToMove);

if(transform.position.y >=6.7){ Destroy(gameObject); }

if(transform.position.y <= -5){ Destroy(gameObject); } }

function OnTriggerEnter(otherObject: Collider){

if(otherObject.gameObject.tag == "enemy"){

playerScript.playerScore += 50;

otherObject.gameObject.transform.position.y = 7;

otherObject.gameObject.transform.position.x = Random.Range(-6,6);

var tempExplosion: Transform;

tempExplosion = Instantiate(explosion, transform.position, transform.rotation);

Destroy(gameObject);

} }

function OnGUI() { GUI.Label(Rect(10,70,200,50),"Bullet Velocity: "+ bulletSpeed);

GUI.Label(Rect(10,90,200,50),"Bullet Angle: "+ bulletAngle); }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Joshua · May 23, 2011 at 03:57 PM

Either give the bullet GameObject a Rigidbody with gravity enabled or script your own gravity;

 var startPosition : Vector3;
 var startTime : float;
 function Start () { startPosition = transform.position; startTime = Time.time; }
 function Update () { transform.position.y = startPosition - 4.905 * (Time.time-startTime)*(Time.time-startTime) }
Comment
Add comment · Show 10 · 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
avatar image thatbigguy · May 23, 2011 at 04:07 PM 0
Share

I was able to give the bullet a Rigidbody and enable gravity and when i fire, it does act under gravity. However, I want to control the angle (which can be controlled with the motion of the mouse) at which it is fired as well in order for the trajectory to be more parabolic

avatar image Joshua · May 23, 2011 at 04:09 PM 0
Share

So what is your question? Alter the angle.

avatar image thatbigguy · May 23, 2011 at 04:10 PM 0
Share

$$anonymous$$y question is how can I alter the angle.

avatar image Joshua · May 23, 2011 at 04:15 PM 0
Share

Right but this is the script controlling the bullet, to change where you instantiate it I assume you'd have to alter something in the turret's script. Check this out: http://www.youtube.com/watch?v=TfRhQO5PcuQ.

But basically you just want to alter the angle of the object that shoots.. can't make it any simpler then that..

avatar image flaviusxvii · May 23, 2011 at 07:07 PM 1
Share

This is one of the most confusing exchanges I've ever read. $$anonymous$$ake a turret with a turret script. $$anonymous$$ake a bullet with a bullet script. The turret will make bullets.

Simple.

Show more comments
avatar image
0

Answer by juanelo · May 23, 2011 at 11:09 PM

I believe the iTween library has function for handling that type of animation directly, without having to delve into the math yourself. Might come in handy.

http://itween.pixelplacement.com/index.php

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Help with orbits 1 Answer

Can I access Physics2dSettings in Project Settings via script 1 Answer

How to get animated sprite to adhere to gravity? 2 Answers

Need help on my script to calculate gravity for a mass 1 Answer

Check if 2D Player is grounded with Physics2D.Linecast 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges