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 seannorbury · Nov 07, 2016 at 11:57 PM · c#movementaddforce

Why can't I get ball movement with this script?

I have been trying to figure out how to add an impulse force on the ball with a button press. I can get movement from the ball with the commented out OnMouseDown() method, but I cannot figure out how to do it otherwise. Here is the code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using UnityEngine.Events;
 using System.Timers;
 
 public class LaunchBall : MonoBehaviour
 {
 
     public Rigidbody rb;
     public float forceOnBall;
     public float percentage = 0;
     public bool buttonPressed = false;
     public int sign = 1;
 
     /* void OnMouseDown()
      {
          GetComponent<Rigidbody>().AddForce(transform.right * 500);
          GetComponent<Rigidbody>().useGravity = true;          
      }  */    
 
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
      
     public void ButtonDown()
     {
         buttonPressed = true;
     }
 
     public void ButtonUp()
     {
         buttonPressed = false;       
     }
 
     public void Update()
     {
         if (buttonPressed)
         {
             percentage+=sign;
             Debug.Log(percentage);
 
             if (percentage == 100 || percentage == 0)
                 sign *= -1;
         }  
     }
 
     public void HitBall()
     {
         forceOnBall = percentage * 100;
 
         if (!buttonPressed && percentage > 0)
         {
             rb.AddForce(0, 0, forceOnBall, ForceMode.Impulse);  
         }
     }
 }

alt text

unity-screenshot.png (168.0 kB)
Comment
Add comment · Show 1
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 Filhanteraren · Nov 08, 2016 at 03:27 AM 0
Share

Looking at your code you never call the HitBall function anywhere.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Filhanteraren · Nov 08, 2016 at 03:41 AM

Here is a fixed code that calls the HItBall method:

 public class LaunchBall : MonoBehaviour
 {
     public float force = 1;
 
     private Rigidbody rb;
     private float forceOnBall;
     private float percentage = 0;
     private bool buttonPressed = false;
     private int sign = 1;
 
     private void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     public void Update()
     {
         if (Input.GetButton("Fire1"))
         {
             percentage += sign;
             Debug.Log(percentage);
 
             if (percentage == 100 || percentage == 0)
                 sign *= -1;
         }
 
         if (Input.GetButtonUp("Fire1"))
         {
             HitBall();
         }
     }
 
     public void HitBall()
     {
         forceOnBall = percentage * force;
 
         if (percentage > 0)
         {
             rb.AddForce(0, 0, forceOnBall, ForceMode.Impulse);
         }
     }
 }
Comment
Add comment · Show 5 · 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 seannorbury · Nov 08, 2016 at 06:53 AM 0
Share

Thanks for your help. I am now looking forward to messing around with the physics of it tomorrow. Is it usually better to code for something like this ins$$anonymous$$d of trying to use the event triggers as I did before? I still don't understand why I couldn't get any movement. It seems like I was close.

avatar image seannorbury · Nov 08, 2016 at 06:58 AM 0
Share

I just saw the comment you posted. Whenever I tried to call the HitBall function before, the button and ball would just disappear when I hit play. I may have had it outside of an if statement in the update function.

avatar image seannorbury · Nov 08, 2016 at 05:25 PM 0
Share

When using this code, the button disappears when I hit play, then if I click the mouse anywhere on the screen, the button reappears. I was trying to get the percentage to go up when I push the UI Button down, and then shoot the ball when I release the UI Button. With this, I can click the mouse anywhere on the screen and it will increase the percentage and shoot ball upon release.

avatar image Filhanteraren seannorbury · Nov 08, 2016 at 10:00 PM 0
Share

What UI system are you using to display the button.

The fixed code just checks for any input regardless of any ui

avatar image seannorbury Filhanteraren · Nov 08, 2016 at 11:54 PM 0
Share

I made the button with create from the hierarchy.

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

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

What is wrong with this script? 3 Answers

Script makes plane point in general direction but doesn't point fully... read description please! 1 Answer

How can I get the player to face the direction it is going in a Unity 2D Game?,How do I make it so that my character faces in the direction it is facing in a 2D Unity Game 0 Answers

Problems With Walking Code 1 Answer

AddForce not working on Instantiated prefab 0 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