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;
}
}
}
}
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 Unity - Scripting API: GameObject.Find
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. Unity - Scripting API: MonoBehaviour.OnTriggerEnter(Collider)
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.