- Home /
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
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
just some help in the right direction to go, i done expect someone to write code for me, just some help as i asked
some help doing what? What have you tried? What have you done?
well i dont even know where to start with a script like this tbh =S
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.
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
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 :)
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;
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.
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.
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!
Your answer
![](https://koobas.hobune.stream/wayback/20220613110356im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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