- Home /
Script for passing and shooting a ball
Hi, I'm making a game where I need to pass and shot a ball/sphere. In my script the solution I made was the function transform.position, but the "teleport" effect wasn't what I'm looking for. I need my ball go to a point to another moving with velocity and not teleporting. How I can do this? Any help? Thanks! I put here a small part of my script for any suggestions.
if(inTrigger){
if( Input.GetKey(KeyCode.Joystick1Button1)){
Soccer.transform.position = Pass.transform.position;
I would recommend you to start at some tutorial modules and learn how unity works: http://www.unity3dstudent.com/category/modules/beginner/
Thank you, I know this site and I've seen dozens of tutorials. Very helfull website. $$anonymous$$y question is specific: an alternative solution to transform.position for making a shot/pass. I tried Instantiate but it clone a lot of balls.
Answer by stevethorne · Feb 06, 2014 at 01:50 PM
You can use a Vector3.Lerp
Change your if statement to this:
if ( inTrigger ) {
if ( Input.GetKey( KeyCode.Joystick1Button1 ) && !isPassing ) {
StartPass( Pass.transform.position );
}
}
Make sure these variables are defined:
var isPassing : boolean = false;
var timeSoFar : float = 0;
var timeToPass : float = 3; // how long you want the pass to take.
var startPosition : Vector3 = Vector3.zero;
var finalPosition : Vector3 = Vector3.zero;
Place this function in your script:
function StartPass( finalPos : Vector3 ) {
startPosition = Soccer.transform.position;
finalPosition = finalPos;
timeSoFar = 0;
isPassing = true;
}
Place this in the Update function:
if ( isPassing ) {
var percent = timeSoFar / timeToPass;
Soccer.transform.position = Vector3.Lerp( startPosition, finalPosition, percent );
timeSoFar += Time.deltaTime;
if ( timeSoFar >= timeToPass ) {
isPassing = false;
}
}
Thank you very much for your script! it gives an error cs(1,6) CS0116 "A namespace can only contain type and namespace declarations". In the script editor the word "bool" in line 1 is "underlined" showing an error... I don't know why...
This isn't intended as the final script, but merely as pieces of the code that you'll need to use it. Place it in your script where it's needed, don't replace everything with this code. I can't give you the final script because you did not provide us with your entire script. I don't know how to merge this with your code, but you do.
You can't take a c# script and just place it within your javascript. You would need to convert it to c#.
$$anonymous$$ake a c# script first, then use the code which was provided by stevethorne. Don't forget to add your original variables.
Ok there were a couple of problems, they are fixed now