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 /
  • Help Room /
avatar image
0
Question by DaviLope · Jan 16, 2017 at 07:45 AM · scripting problemvector3forcevectorfootball

Kicking at 45° direction

I want to kick a ball forward with a 45° elevation, my best thought is to use Vector3, however I cannot figure out how to make a Vector3 that does it, the best i´ve been able to do so far is to kick it with a semi-circle elevation, but my intention is to kick it like this...


alt text

alt text


Is there a way to make this kind of vector? Or am I totally wrong using Vector3?

1.jpg (87.7 kB)
2.jpg (235.9 kB)
Comment
Add comment · Show 2
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 KoenigX3 · Jan 16, 2017 at 11:27 AM 0
Share

Could you insert some of your code? I would like to see how the ball gets kicked, is there a RigidBody that gets an AddForce call, or what is it.

avatar image DaviLope KoenigX3 · Jan 18, 2017 at 03:53 AM 0
Share

This is what i´m using right now, it works well, however I´d still wish I could achieve a straight direction like in the pictures...

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class striking : $$anonymous$$onoBehaviour 
 {
     public float power = 20;
 
 
     void Update()
     {
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightShift))
             TryHitBall();
     }
 
 
     void TryHitBall()
     {
         if (power > 0) 
         {
             Rigidbody rb = GetComponent<Rigidbody>();
             rb.velocity = StrikeVec() * power;
         }
     }
 
 
     private Vector3 StrikeVec()
     {
         Vector3 elevation = new Vector3 (0,0.8f,0);
         Vector3 sight     = Camera.main.transform.forward;
         Vector3 fixsight  = sight - new Vector3 (0,0.2f,0);
 
         Vector3 direction = fixsight + elevation;
 
         return direction.normalized;
     }
 
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by KoenigX3 · Jan 18, 2017 at 09:57 AM

You can use the Rigidbody.useGravity property to disable gravity. That will make the ball fly like it would in space. Later on you can enable the gravity for the ball if you want.

 void TryHitBall()
      {
          if (power > 0) 
          {
              Rigidbody rb = GetComponent<Rigidbody>();
              rb.useGravity = false;
              rb.velocity = StrikeVec() * power;
          }
      }

For example, if the net has a collider on it, you can use that to reactivate the gravity for the ball.

 void OnCollisionEnter(Collision collision)
 {
     if(collision.gameObject.name == "Goal")
     {
         GetComponent<Rigidbody>().useGravity = true;
     }
 }

One more thing. If you want to use the Rigidbody more than once, it is a good idea to create a global variable of it, and assign it on the start. This way you will get rid of using GetComponent every time you want to do something with the ball, which will make it more performance-efficient. Put a Rigidbody variable next to the public power float, and assign it on Start().

 Rigidbody rb;
 
 void Start()
 {
     rb = GetComponent<Rigidbody>();
 }
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 DaviLope · Jan 21, 2017 at 06:19 AM 0
Share

Excellent.

That did it, I actually had no idea of the ".useGravity" function.

Thanks for sharing your knowledge.

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

116 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 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 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 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 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 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

How too climb a ladder with the FPS Controller correctly 0 Answers

Vector question 0 Answers

how can I "play" the whole MoveTowards sequence after one click? 1 Answer

Get All Game Objects Between 2 Points 1 Answer

Get Vector3 From Another Script 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