• 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 schpitz · May 10, 2014 at 11:45 PM · addforcepathdrawlinefootball

Drawing path and Throwing objects

Hi guys, I want to create a gameplay in which the player can draw a path in which his avatar can move around (and affect other objects). Alongside, if the player is close enough the another object, with a tap he can throw the object away (to the direction of the tap).

I used some codes taken from "Flight Control" gameplay and improved it, but something went wrong when i'm trying to implement the "tap" part (in which the player throw the ball away). I can't get the angle and the force right.

Attached an image, and the code (there's another script on the ball, affecting 'hasball' static-var according to collider enter and exit)

Any ideas for how should I fix it will be welcome, Cheers!

![alt text][1] [1]: /storage/temp/26467-test1.jpg

 #pragma strict
 
 private var dragging : boolean = false;
 private var moving : boolean = false;
 private var travelling : boolean = false;
 private var countDrag : int = 0;
 var pathLength : int;
 private var countMove : int = 0;
 private var mousePosition : Vector3;
 private var mousePoint : Vector3;
 private var pointCurrent : Vector3;
 private var pointStore : Vector3;
 private var targetWaypoint : Vector3;
 private var moveDirection : Vector3;
 var moveSpeed : float = 3.0;
 private var wayRotation : Quaternion;
 var damping : float = 6.0;
 private var posStoreX : Array = [];
 private var posStoreZ : Array = [];
 private var arrayPathMarker : GameObject[] = new GameObject[100];
 var objectPathMarker : GameObject;
 
 var imSelected : Transform;
 var imClickedOn : boolean = false;
 private var startPosY : float = 0.0;
 
 static var hasBall : boolean = false;
 static var canShoot : boolean = false;
 
 var TouchObject : GameObject; // marker for what object this object touch
 var passPower : Number = 500;
 
 var testPoint : GameObject; // marker for touch event - for throwing away
 
 function Start () {
     dragging = false;
     startPosY = transform.position.y;
 }
 
 
 function Update () {
     
     if (!moving) {
         var rayHit : RaycastHit;
         if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), rayHit))
         {
             mousePoint = rayHit.point;
             
             // select this object from being clicked on
             imSelected = rayHit.transform;
 
             if (Input.GetMouseButtonDown(0))
             {
                 if (imSelected == this.transform)
                     {imClickedOn = true;}
 
                 print("imSelected NOW - " + imClickedOn);
                 if (imClickedOn) {
                     OnTouchBegin(mousePoint);
                 } // check if object has been clicked on
                 
                 
             } else if (Input.GetMouseButton(0))
             {
                 if (dragging)
                     {OnTouchMove(mousePoint);} // check in-case mouse is down when object travelling is set to false with (dragging)
             }
             else if (Input.GetMouseButtonUp(0))
             {
                 if (dragging)
                     {OnTouchEnd(mousePoint);} // check in-case mouse is released when object travelling is set to false with (dragging)
             }
             
         }
     } else {
             if (Input.GetMouseButtonDown(0))
             {
                 resetPath();
             }
             var rayHit2 : RaycastHit;
             if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), rayHit2))
             {
                 mousePoint = rayHit2.point;
                 
                 // select this object from being clicked on
                 imSelected = rayHit2.transform;
 
                 if (Input.GetMouseButtonDown(0))
                 {
                     if (imSelected == this.transform)
                         {imClickedOn = true;}                    
                     print("imSelected NOW - " + imClickedOn);
                     if (imClickedOn)
                         {OnTouchBegin(mousePoint);} // check if object has been clicked on    
                             
 //                    checkShoot(mousePoint);    // check if it's possible to shoot/pass the ball
 
                 }
                 else if (Input.GetMouseButton(0))
                 {
                     if (dragging)
                         {OnTouchMove(mousePoint);} // check in-case mouse is down when object travelling is set to false with (dragging)
                 }
                 else if (Input.GetMouseButtonUp(0))
                 {
                     if (dragging)
                         {OnTouchEnd(mousePoint);} // check in-case mouse is released when object travelling is set to false with (dragging)
                 }
                 }        
     }
 }
 
 
 function OnTouchBegin (pointCurrent : Vector3) {
     countDrag = 0;
     posStoreX.Clear();
     posStoreZ.Clear();
     AddSplinePoint(pointCurrent);
     dragging = true;
     moving = false;
     checkShoot(pointCurrent);    // check if it's possible to shoot/pass the ball
 
 }
 
 
 function OnTouchMove (pointCurrent : Vector3) {
 //    finishTravelling();
     if ((dragging) && (countDrag < pathLength)) {
         AddSplinePoint(pointCurrent);
         travelling = true;
     } else {
         dragging = false;
         moving = true;
     }
 }
 
 
 function OnTouchEnd (pointCurrent : Vector3) {
     dragging = false;
     moving = true;
     if (imSelected)
         {imSelected = null; imClickedOn = false; 
 } // reset 'select this object if clicked on'
 }
 
 
 function AddSplinePoint (pointStore : Vector3) {
     // store co-ordinates
     posStoreX[countDrag] = pointStore.x;
     posStoreZ[countDrag] = pointStore.z;
     // show path : Instantiate and load position into array as gameObject
     arrayPathMarker[countDrag] = Instantiate(objectPathMarker, Vector3(pointStore.x, startPosY, pointStore.z), transform.rotation);
     // next position
     countDrag ++;
 }
 
 
 function FixedUpdate () {
 
     // -- move gameObject
     if ((travelling) && (posStoreX[countMove] != null)){
             
         // move aircraft along path in time
         targetWaypoint = Vector3(posStoreX[countMove], startPosY, posStoreZ[countMove]);
         moveDirection = targetWaypoint - transform.position;
         
         if(moveDirection.magnitude < 0.8) {
             Destroy (arrayPathMarker[countMove]); // remove current path marker
             countMove++; // next waypoint position
         } else {        
             transform.position += moveDirection.normalized * moveSpeed * Time.deltaTime; // move gameObject
             //transform.LookAt(targetWaypoint);
             wayRotation  = Quaternion.LookRotation(targetWaypoint - transform.position);
             transform.rotation = Quaternion.Slerp(transform.rotation, wayRotation , Time.deltaTime * damping);
         }
         
         if (countMove >= posStoreX.length)
             {finishTravelling ();} // stop at end of path    
         // --
     } else {
         countMove = 0;
     }
     
     
 //    // -- aircraft flying normally
 //    if (!travelling) {
 //        transform.position += (transform.forward * moveSpeed * Time.deltaTime);
 //    }
     
     // print out of current stored array
     if (Input.GetKeyDown("r")) {print ("posStoreX "+posStoreX+" : posStoreZ "+posStoreZ);}
 }
 
 
 function resetPath()
 {
     for (var i=0; i<(arrayPathMarker.length);i++)
     {
                 Destroy (arrayPathMarker[i]); // remove current path marker
     }    
     countMove = 0;    
     moving = false; 
     travelling = false; 
 
 }
 
 
 function finishTravelling () {
     countMove = 0;
     moving = false; 
     if (imSelected) {
         imSelected = null; 
         imClickedOn = false;
         } // reset 'select this object if clicked on'
     travelling = false; 
 }
 
 
 function checkShoot(other:Vector3) {
     if ((imClickedOn==false)) {
 //    if ((imClickedOn==false) && (Moving.hasBall==true)) {
 
         Moving.canShoot=true;
         print ("can shoot :)");
 
         var newTarget = Instantiate(testPoint, other, transform.rotation);
         passBall(other);
     }
 }
 
 function passBall(other2:Vector3){
 //        TouchObject.transform.LookAt(other2);
         TouchObject.rigidbody.AddForce(other2.x*passPower,other2.y*passPower,other2.z*passPower);
         Moving.hasBall = false;
 }
 
 function OnCollisionStay(other : Collision) {
     //    print the ball is not touched anymore, and note it
     TouchObject = other.gameObject;
 }
 
 function OnCollisionExit(other : Collision) {
     //    print the ball is not touched anymore, and note it
     TouchObject = null;
 }
 
test1.jpg (10.4 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

How i can draw curving path in scene and gameobjects walk on it 1 Answer

A problem with itween system ??? 0 Answers

Add force along a path 1 Answer

how to cancel iTween Catmull-Rom path 1 Answer

How to get .exe file to run through script? 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