• 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 Fekete · Dec 29, 2013 at 11:24 AM · velocityaddforceforceballshoot

[Solved] How to shoot an object in front of the player?

I've wrote a script for my firstperson game which parents a football in front of the player.

My aim is to shoot the ball in front of the player if i press the left Mousebutton and hold down the "W" Key. Unfortunatelly i don't know how to give the ball a force to shoot it in front of the player. Would be nice if someone could help me to achive this! :)

I made a video about my Problem, maybe it helps to solve it: http://youtu.be/yMmylRfqZ4U

Thanks in advance for any help!

Greetings

-Edd

Here is my Script which i have put to the football:

 var thePlayer : CharacterController = GetComponent(CharacterController);
 var ParentPart : Transform;    //An empty object which is parented in front of the player
 var PickUpDistance : float = 2.4;
 var canPickUp : boolean = false;
 var parented : boolean = false;
 
 function Update () 
 {
     //is the player near enough to the football to pick it up?
     dist = Vector3.Distance(thePlayer.transform.position, transform.position);
     if (dist <= PickUpDistance) {canPickUp = true;} else { canPickUp = false; }
 
     //take the ball (parent it to the empty objekt which is parented to the player)
     if(Input.GetButtonDown("Fire2") && canPickUp) 
      {
          transform.parent = ParentPart;
          parented = true;
     }
             
     //Shoot
     if(Input.GetButtonDown("Fire1") && Input.GetKey(KeyCode.W) && parented)
      {
         parented = false;
         transform.parent = null;
         transform.rigidbody.constraints = RigidbodyConstraints.None;
         
         transform.rigidbody.AddForce (thePlayer.velocity.x,thePlayer.velocity.y,(thePlayer.velocity.z+100));    
     }

     //set the balls position to the position of the empty
     if(parented) 
     { 
         transform.position = ParentPart.position;
         transform.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
     }
 }
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
2

Answer by PlasmaByte · Dec 29, 2013 at 02:44 PM

You can use the forward variable in the transform component to get the current direction the player is facing. You can then add vectors in a much more intuitive way.

 transform.rigidbody.AddForce (thePlayer.velocity + thePlayer.transform.forward*100);
Comment
Add comment · Show 2 · 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 Fekete · Dec 29, 2013 at 05:11 PM 0
Share

This works great, thank you so much :) but you have to use a bigger factor otherwhise you won't notice the effect.

Unfortunatelly i can't upvote your answer, since i need at least a reputation of 15, but i will do as soon as possible. THX!

avatar image Soloman · Aug 15, 2018 at 03:41 PM 0
Share

You are awesome. Thank you very much

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

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

Using rigidbody.AddForce and speeding the object 2 Answers

i am not able to increase the speed of ball perodically in pong, in this code the speed of ball decrease while playing so help with this, how to increase speed of ball perodically?, 0 Answers

Bouncing Ball Game 3 Answers

Adding force to rigidbody2d to slide 1 Answer

if mouse button is pressed continual add force from min. to max.and then shoot a ball 2 Answers


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