Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ak28 · Aug 16, 2014 at 05:22 PM · forcemouseclickaddforceatposition

Varied force and mouseButtondown help

 public class ball_Input : MonoBehaviour {
 Ray ry;
 public float distanceunits=100;
 public GameObject basketBall;
 public bool isHit=false;
 // Update is called once per frame
 RaycastHit hit;

 void Update () {
     ry = Camera.main.ScreenPointToRay (Input.mousePosition);
     //Vector3 positon = ray.GetPoint (distanceunits);
     if(Physics.Raycast(ry,out hit,distanceunits))
        {
         //Debug.Log (hit.transform.gameObject.name);
         isHit=true;

     }
 }

 void FixedUpdate()
 {
     if ((isHit == true)) {//&&(Input.GetMouseButtonDown(0))
                     basketBall.rigidbody.AddForceAtPosition(new Vector3(0,30,0),hit.transform.position);
                     isHit = false;
         Debug.Log("Clciked");
             }
     }
 }

I have attached this code to my main camera .I am begginer so i am making a simple program where the ball jumps whenever i click it,so far i have succeded in adding a force to the sphere , i cant get it add force only on mouseclick and also i would like to add force that makes the ball go different angles based on the point i click it (z axis not needed) Help

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
0

Answer by robertbu · Aug 16, 2014 at 05:47 PM

Here is a bit of rework to your script. For a one-time application of force, you don't need to use FixedUpdate(). Use FixedUpdate() for repeated or continuous application of force. This code assumes that any balls you want to kick have the name 'Ball'. Unless you want to be fancy, generally with a ball if you apply the force towards the center of the ball, the results are expected.

 using UnityEngine;
 using System.Collections;
 
 public class Ball_Input : MonoBehaviour {
     public float distanceunits = 100.0f;
     public float hitForce = 500.0f;
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
 
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             RaycastHit hit;
 
             if(Physics.Raycast(ray,out hit,distanceunits) && hit.transform.name == "Ball") {
                 var dir = (hit.transform.position - hit.point).normalized;
                 hit.transform.rigidbody.AddForce (dir * hitForce);
             }
         }
     }
 }
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

2 People are following this question.

avatar image avatar image

Related Questions

How can I add force to a raycast bullet? 2 Answers

AddForceAtPosition Locally Problem. 1 Answer

Add force at position Question 1 Answer

How To Perform A Mouse Click On Game Object 5 Answers

Mouse controlled Laserpointer 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