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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by sydpes · Feb 06, 2014 at 02:21 AM · transform.positionballshot

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;
Comment
Add comment · Show 2
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 GameVortex · Feb 06, 2014 at 08:54 AM 0
Share

I would recommend you to start at some tutorial modules and learn how unity works: http://www.unity3dstudent.com/category/modules/beginner/

avatar image sydpes · Feb 06, 2014 at 01:42 PM 0
Share

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.

1 Reply

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

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;
     }
 }

Comment
Add comment · Show 9 · 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 06, 2014 at 02:56 PM 0
Share

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...

avatar image stevethorne · Feb 06, 2014 at 02:58 PM 0
Share

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.

avatar image APenguin · Feb 06, 2014 at 03:16 PM 0
Share

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.

avatar image stevethorne · Feb 06, 2014 at 03:16 PM 0
Share

Updated my answer for Javascript

avatar image stevethorne · Feb 06, 2014 at 05:50 PM 0
Share

Ok there were a couple of problems, they are fixed now

Show more comments

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

21 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 avatar image avatar image avatar image

Related Questions

Simple script to move object doesn't work 1 Answer

My transform positions have been behaving really odd today 0 Answers

transform.position 2 Answers

unity how to make objects look at random objects with the same tag 1 Answer

Why can't i modify object.trasform.position more than once? 0 Answers


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