• 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 sydpes · Feb 12, 2014 at 04:53 PM · shootingball

Shooting a ball?

Hi! I have this game situation:

  • have an animated soccer player who can move in all directions, roll and drag a soccer ball. He has trigger cube attached to him to detect when catches the ball;

  • a soccer field with two football goals, inside of them there's a goal trigger.

What I need is a script for when the ball enters the football player trigger cube's he could pass and shoot the soccer ball. The desired Pass/Shot effect is similar to Instantiate function but I don't need to clone a ball. The lerp and move to points funtions are not desireable because gaves the ball a "teleport" effect.

I made this Instantiate script, it works but it clones the ball, and I need just one ball in field. The shot/pass effect are perfect but it's not what I really need, any help please? Thx!

 using UnityEngine;
 using System.Collections;
 
 public class ShotaBall : MonoBehaviour {
     
     // the gun for shot
     public GameObject Trigger;
     
     // private data
     private GameObject Soccer=null;
     private float power;
     private float fireRate=1;
     private float nextFire=0.1f; 
     //------------------------------------------------------------
     // Use this for initialization
     void Start () {
         Soccer = GameObject.Find("Soccer");
     }
     //------------------------------------------------------------
     // Update is called once per frame
     // control of gun
     void Update () {
         
         // power of shot
         if( Input.GetKey(KeyCode.Joystick1Button3)) {
             power +=7.5f;
         }
         
         // firing of the SoccerBall 
         if( Input.GetKey(KeyCode.Joystick1Button3) && Time.time > nextFire) {
             nextFire = Time.time + fireRate; {
             GameObject obj = Instantiate(Soccer, Trigger.transform.position, Trigger.transform.rotation) as GameObject;
             obj.rigidbody.velocity = Trigger.transform.TransformDirection(Vector3.forward * power);
             obj.name = "SoccerClone";
             Destroy(obj, 5);
             power=7.5f;
     }
     }
 }
 }

 
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

2 Replies

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

Answer by JeffreyD · Feb 12, 2014 at 05:58 PM

I suppose you could use the script you have and instead of instantiating you could do a Find object (Soccer Ball). I've never used find but it looks pretty straightforward. See Script doc and search for find. Here is the link http://docs.unity3d.com/Documentation/ScriptReference/GameObject.Find.html

Or you could create a script to attach to the ball. Something like the following.

 function OnTriggerEnter(col : Collision)
 {
    // Look to see if the player collided with the ball
    if (col.gameObject.name == "Player")
    {
       // if yes then do your kick/move the ball however you want
        Debug.Log("ouch you kicked me!");
        var vec = Vector3(10, 10, 0); //x: float, y: float, z: float)
     rigidbody.AddForce (vec); // , Impluse);
    }
 }

You could also use OnCollisionEnter(col : Collision) instead. I'm not sure which one is better or which is "more efficient/proper". I think in this case you want Trigger so you can expand the box collider of the ball and turn on trigger. Right?

Or you could add the OnTriggerEnter() script (or add the function to your existing player control script) to the Player, but you'd need to test for if (col.gameObject.name == "SoccerBall") to see if the trigger seen by the player was the ball. Note: the OnTriggerEvent is only sent to each object that has a collider and that one of the collider object has a rigid body attached to it. See the script doc for OnTriggerEnter for more info. http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnTriggerEnter.html

I'm still trying to figure it all out myself. It seems as if there are a multitude of ways to do a single thing, like kick/move a ball on collision/trigger. Someways, I image, are more efficient than others. How to do all the different ways and which is most efficient is what I'm trying to figure out. Anyway, Hope this helps.

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 sydpes · Feb 12, 2014 at 09:39 PM 0
Share

Thank for helping, the script that you left doesn't work but it is a good idea. On other hand I don't undestand how a function "GameObject.Find" can simulate a shot/pass. Is it possible a RaycastHit shot the ball?

avatar image JeffreyD · Feb 13, 2014 at 02:59 AM 0
Share
 In what was does it not work. Is it not being called?
 
 Is it being called but not affecting the collision object?
 
 Please specify. 
 
 Take out the vector force and add whatever you want to do to move the ball.
 
 Yes you could rayCast the ball.
 
 
 function Update ()
 {
     var fwd = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;
     
     Debug.DrawRay(transform.position, fwd * 20, Color.red);
 
 
     if (Physics.Raycast(transform.position, fwd, hit))
     {
         Debug.Log ("We've hit " + hit.transform.tag);
         if (hit.transform.tag == "SoccerBall;")
         {
              Debug.Log("Rotate target");
 
                      // this does rotation but you could
                      // just as easily add a force or change in position
                      // of the hit object. It's up to you.
              hit.transform.Rotate (Vector3.up * Time.deltaTime * 100, Space.World);
             }
     }
 }
 
 Attach the script to the camera, which is a child of the player.

Sorry about the formatting. I"m ont that good with this editor.

avatar image
0

Answer by sydpes · Feb 13, 2014 at 11:36 PM

Hi again! Jeffrey, thank for the 2 scripts. After read your suggestions I made a script that pass/shot the ball! Finally! Very simple as I suspected!

 function OnTriggerStay (other : Collider)
     {
     if( Input.GetKey(KeyCode.Joystick1Button2)){ 
     if (other.attachedRigidbody) {   
     other.attachedRigidbody.AddForce(Vector3(0, 0, 1) * 1000); //x: float, y: float, z: float)
     }
     }  
     }  

 
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 sydpes · Feb 13, 2014 at 11:59 PM 0
Share

Jeffrey I will take a look to your raycast script too, as soon as possible, the clues that you gave are excellent! Thanks!

avatar image JeffreyD · Feb 15, 2014 at 04:07 AM 0
Share

Hey Sydpes, It sounds like you are on your way. We're all learning little by little. The docs are good. keep checking those out. There are great videos in the archive section of live learning. Check those out as well. Let me know if I can help. I'll do the best I can, but I'm still a newbie relatively speaking. Good Luck.

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

18 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

Related Questions

Calculating ball trajectory in full 3d world 8 Answers

How do you make a shoot up feature like in dx-ball? 2 Answers

Player doesn't shoot ball object forward like I tell him 2 Answers

Shooting in 2 dimensional games 2 Answers

Gun Fire, sparks on Collision 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