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 /
avatar image
0
Question by lemiwinks · Jul 18, 2013 at 03:20 PM · c#golf

Need help with a golf game script

hey all. im working on a small crazy gold type game and so far so good, however ive been trying to create a script that will allow me to drag out an arrow from the ball that represents amount of power, this could also be moved around the ball in order to choose the direction it fires off to. once released, the ball will fire forward in the chosen direction and force. To make this simpler i have added a image which kind of explains what i mean, any help would be great as im completely lost here http://i.imgur.com/eSE4IpB.png

thankyou LMB

Comment
Add comment · Show 4
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 perchik · Jul 18, 2013 at 03:29 PM 0
Share

Right. So.... whats your question? If you have a specific problem with code you've already made then post the code and we'll try to help. If this is a plea for someone else to write code, it will probably go unanswered

avatar image lemiwinks · Jul 18, 2013 at 03:46 PM 0
Share

just some help in the right direction to go, i done expect someone to write code for me, just some help as i asked

avatar image perchik · Jul 18, 2013 at 04:02 PM 0
Share

some help doing what? What have you tried? What have you done?

avatar image lemiwinks · Jul 18, 2013 at 04:18 PM 0
Share

well i dont even know where to start with a script like this tbh =S

2 Replies

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

Answer by sparkzbarca · Jul 18, 2013 at 04:21 PM

so what you going to want to do is

get the position on the screen they are on.

Spawn a prefab arrow at that location that prefab should update so its forward points from start pos to current finger pos

so you'll want those variables.

the force will be the distance between start pos and current at the time that they release or whatever so roughly

 public  Gameobject arrowPrefab;
 Gameobject Arrow;
 
 Vector3 StartPos;
 Vector3 CurrentPos;
 
 void Update
 {
 if(touched)
 {
 //get the touch pos and convert it to a vector, i dont do android but i'm sure
 //its easy
 StartPos = touch position as a vector;
 
 
 //at the start we don't know which way to aim so were
 // going to give the arrow a generic rotation,
 
 Arrow = instantiate(arrowPrefab, StartPos, quanternion.identidy) as Gameobject;
 
 //now were going to keep track of where the finger is and keep updating 
 //arrow to point from start pos to finger until they lift up finger
 //then we'll destroy the Arrow and use the distance from finger start pos to
 //finger end
 //pos to determine how much force to toss the ball with.
 
 while(touched)
 {
 currentPos = //convert finger touch position to vector 3 again;
 
 //as long as the top of the arrow is it's forward direction the top
 // of arrow will look at finger
 Arrow.rotation.lookat(currentpos);
 
 }
 //at this point touched is no longer true they let up.
 
 float TossPower = vector3.distance(currentpos,startpos);
 
 now the further away they are the more we toss. you can do more math on Tosspower
 to do more changes like do 
 
 TossPower = ClubStrength * Tosspower;
 
 To have different clubs toss by different amounts. 
 
 }
 next you add force to toss.

this should help get you started.

Comment
Add comment · Show 14 · 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 lemiwinks · Jul 18, 2013 at 04:39 PM 0
Share

WOW, i dudnt expect such a good response. Thankyou!! im going to work on it now and see how it comes out. Ill post results =D Thankyou again

avatar image InfiniBuzz · Jul 18, 2013 at 04:41 PM 0
Share

please post this things as comments not as answers ;) also make sure to vote his answer up if you think its a good one - or tick it as the correct answer if it provided what you've been looking for :)

avatar image lemiwinks · Jul 19, 2013 at 03:54 PM 0
Share

ok so so far ive got this, which does return the touch position in the console so i gather so far im on track. what this is doing at the moment however is creating multiple arrows over and over and not even displaying them on screen. im also getting this error "Unityexeption: index out of bounds"

using UnityEngine; using System.Collections;

public class TouchDragControls : $$anonymous$$onoBehaviour {

 public GameObject arrowPrefab;
 GameObject Arrow;

 Vector3 StartPos;
 Vector3 CurrentPos;
 
 // Update is called once per frame
 void Update () {
 
 // If screen is touched
     if(Input.touches.Length >=0 )
     {
         // $$anonymous$$ake start position
         StartPos = Input.GetTouch(0).position;
         Debug.Log (Input.GetTouch(0).position);
     }

//at the start we don't know which way to aim so were // going to give the arrow a generic rotation,

     Arrow = Instantiate(arrowPrefab, StartPos, Quaternion.identity) as GameObject;
avatar image sparkzbarca · Jul 19, 2013 at 06:57 PM 1
Share

oh yea your instantiateing in update which well i messed up and didnt close my if statement but it should well actually thinking about it you need 2 things. basically what you'll do is an if statement to see if the arrow exists already and if the screen is being touched and only spawn if the screen is touched and the arrow doesnt exist.

//!arrow is the same as saying arrow == null; if the variable isn't assigned or we destroy or assign null to it which is what we'll do after they let off the screen then it will trigger the if

if(Input.touches.Length >= 0 && !arrow) { startpos.... instantiate.... } now it wont spawn over and over.

as for the error that error means that your trying to access something using an index and the item doesnt exist for example

lets say you had a list of 10 objects

list mylist;

and you went

mylist[10];

well that wont work becuause the 10 itmes are 0 to 9.

you need to double click on the error to show exactly which line it is that is causing the error.

if it is for example input.gettouch(0) that means there are no touches in gettouch because if there was even 1 it would return it as position 0. It could be you have an empty list your trying to access but without more code i cant tell.

avatar image sparkzbarca · Jul 19, 2013 at 07:02 PM 1
Share

lastly the position your spawning it at is the cameras position most likely. like on the same plane as the camera. its not that it isn't spawning it if you pause and check it'll probably be literally right on the camera and thats why you can't see it try thyis maybe

spawnposition = input.touch.position + camera.transform.forward * 4;

that should but it 4 meters in front of the camera. or maybe you camera.screenpointtoray . i'm not sure if touch.position returns a vector3 or vector2. vector2 woudl be a screen position but not a depth.

Show more comments
avatar image
0

Answer by kawther · Sep 09, 2013 at 08:28 PM

perhaps this will be helpful http://forum.unity3d.com/threads/86472-Drag-Shot-Mover-Get-your-physics-objects-moving!

Comment
Add comment · 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

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

19 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

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

A node in a childnode? 1 Answer

C# How to Detect Edges of a Collider 0 Answers

Starting with unity - which scripting language ? 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