• 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 30, 2011 at 07:25 PM · 2dprojectile

Firing a projectile in 2D

I am creating a game where I am firing a projectile to move in a parabolic motion in 2D. My player can move left and right and with a mouseclick, fire a grapple (i used a capsule for now). However, it is moving in 3 dimensions. How can I make the grapple only move in 2 (x-axis and y-axis)?

here is my code for the player:

 var playerSpeed : int;
 
 var playerDist : float;
 
 var playerHealth: int;
 
 var mySlider : float = 1.0;
 
 var sliderMaxValue : int = 90;
 
 var timepart : float;
 
 var grapple : Rigidbody;
 
 var speed = 150;
 
 function Update () {
 
 amtToMove = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime;
 
 transform.Translate(Vector3.right * amtToMove);
 
 if(transform.position.x < -6.17){
 transform.position.x = -6.17;
 }
 
 if(transform.position.x > 6.17){
 transform.position.x = 6.17;
 }
 
 if(transform.position.x < 0){                            //Calculates the distance between the player and the castle
 playerDist = 6.17 + Mathf.Abs(transform.position.x);
 }
 
 if(transform.position.x == 0){
 playerDist = 6.17;
 }
 
 if(transform.position.x > 0){
 playerDist = 6.17 - transform.position.x;
 }
 
 if(Input.GetMouseButtonDown(0)){
 var instantiatedProjectile : Rigidbody = Instantiate (grapple, transform.position, transform.rotation);
 
 instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
 }
 }
 
 function OnGUI () {
     mySlider = LabelSlider (Rect (10, 100, 100, 20), mySlider, 90.0, "Angle:" + mySlider);
     GUI.Label(Rect(10,150,200,50), "Player Distance:" + playerDist);
     GUI.Label(Rect(10,130,200,50), "Timepart:" + timepart);
 
 }
 
 function LabelSlider (screenRect : Rect, sliderValue : int, sliderMaxValue : int, labelText : String) : float {
     GUI.Label (screenRect, labelText);
     screenRect.x += screenRect.width; // <- Push the Slider to the end of the Label
     sliderValue = GUI.HorizontalSlider (screenRect, sliderValue, 0.0, sliderMaxValue);
     return sliderValue;
 }
 
 function timepartChanger(){
 timepart = (playerDist)/(playerSpeed + 60);
 }
 
 

and here is my code for the grapple

 var grappleSpeed : int;
 
 function Update () {
 
 amtToMove = Time.deltaTime;
 
 transform.Translate(Vector3.up + Vector3.right * grappleSpeed * amtToMove); //Moves bullet up 
 }
Comment
Add comment · Show 4
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 G_Sacristan · May 30, 2011 at 07:30 PM 0
Share

could u format it a bit - i mean, code part as code

avatar image Peter G · May 30, 2011 at 07:47 PM 0
Share

Don't worry I fixed it :)

avatar image thatbigguy · May 30, 2011 at 07:54 PM 0
Share

ok thank you :) anyway when I play the game and my player fires, the capsule moves in a weird path that is not parabolic. Is there anyway for it to move in a parabolic trajectory in the x and y-axis?

avatar image thatbigguy · May 30, 2011 at 08:14 PM 0
Share

the capsule still collides with the player (using your code). will i have to add a ignoreCollisions line to the capsuleScript (to apply only to the player)?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Peter G · May 30, 2011 at 08:01 PM

This line is probably one causing your problem:

 if(Input.GetMouseButtonDown(0)){
      var instantiatedProjectile : Rigidbody = Instantiate (grapple, transform.position, transform.rotation);    
      instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, speed));
 }

There are still a number of issues that you could be having:

  1. Your grappling hook is colliding with the player and that is knocking it off course.

  2. Your player isn't facing directly on the x axis causing the projectile to shoot off at a slight angle.

So what can you do to solve your problem.

  1. Add an ignore collision call that will prevent the object from hitting the character. Note that this depends heavily on the type of collider your character uses.

  2. Create a custom velocity. locked on the xy plane. I'm going to show you the easiest solution.

       if ( Input.GetMouseButtonDown(0) ) {
              var instantiatedProjectile : Rigidbody = Instantiate (grapple, transform.position, transform.rotation);    
              Physics.IgnoreCollison(instantiatedProjectile.collider, GetComponent.<CharacterController>() );
    
                 var velocity = transform.forward;
                 velocity.z = 0;
                 velocity *= speed;
                 instantiatedProjectile.velocity = velocity;
         }
    
    
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
avatar image
0

Answer by thatbigguy · May 30, 2011 at 08:27 PM

now I am getting a "MissingComponentException: There is no 'CharacterController' attached to the player object, but a script is trying to access it."

Comment
Add comment · Show 1 · 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 Peter G · May 30, 2011 at 10:15 PM 0
Share

I just wrote that because that is one possible scenario of two or three. If you have a character controller on you object, you should do as my code did. If you have a collider + rigidbody, change GetComponent.<CharacterController>() to collider. And if you don't have a collider on your player, then remove the entire line.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

projectile affected by parent object 1 Answer

How to use RaycastHit2D for a 2D projectile? 2 Answers

Top down 2d shooting - XZ rotation 0 Answers

How do I fire a 2D projectile based on it's current angle/location. 1 Answer

sidescroller: flipping sides of player based on direction (C#) 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