How would I make addforce only apply when a ball is touching an object on my character

I want to make a ball launch from a catapult mechanism when i press the space bar. How would i make it so that it has to be touching an object on the character so that it would move. Also how would I make it that it goes in the direction that the character is facing. This is my code so far. I am using C# using UnityEngine; using System.Collections;

 public class shooting : MonoBehaviour {
     Animator anim;
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator> (); 
     // Update is called once per frame
     void Update () {
         if (Input.GetKey (KeyCode.Space))
                         rigidbody.AddForce (Vector3.right * 100);
         if (Input.GetKey (KeyCode.Space))
             rigidbody.AddForce (Vector3.up * 70);
more ▼

asked Jun 27, 2014 at 04:57 PM

avatar image

1 3

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

to move the ball at the direction the character facing use transform.forward vector instead of Vector3.forward.

Another way is to use rigidbody.AddForceRelative(Vector3.forward * 100f) - it will cause the same result (applies force in forward direction of the character).

Also its better to use just one AddForce / AddForceRelative call:

rigidbody.AddForceRelative(Vector3.forward 100f + Vector3.up 70f)

more ▼

answered Jun 28, 2014 at 10:08 AM

avatar image

1.2k 1 6 9

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jun 27, 2014 at 04:57 PM

Seen: 236 times

Last Updated: Jun 28, 2014 at 04:11 PM