- Home /
Input or Guidance seeking -- Where to find information to allow me to Fling object with Arc from fixed point of view with touch screen input.
Hello Members of Unity,
I have started my first project with Unity. I chose Unity eagerly hungry to learn C#. Going through as many tutorials as I can. Applying what I learn to my game. This has been a rewarding experience.
When I step outside of tutorials looking for specific guides results in a problem. Many guides for specific tasks are out dated. Unity 5 release changed how certain terminology is compiled creating errors while following out dated tutorials C# scripts. I could install the previous versions of Unity, but I won't capture updated specific knowledge. My request is really simple. Could anyone point me in a direction to content related to Unity 5 and performing a lobbing motion of a object. I will attach two images below to demonstrate it.
The first image is the view point of the camera. The touch screen input I desire would allow the player to drag left and right to aim. Swiping upwards would give the object force, producing a lobbing effect demonstrated by image 2 below.
Not requesting someone to write the code, I want to learn as much as possible. Only will learn by doing it. I feel like I am getting close with some examples I found, only to be running in circles trying to research compiler error codes. The Unity scripting reference can only get me so far.
Ideally I would like to set this up to run on my android phone and mom's iPad.
Thank you for any and all input.
Chris

If you couldn't tell, I am by no means proficient in C#.
I was able to come up with this.
using UnityEngine;
using System.Collections;
public class Swipetest1 : $$anonymous$$onoBehaviour {
public GameObject Source;
public float zForce;
public float ControlledForce;
public float maxTime;
public float $$anonymous$$SwipeDist;
public Vector3 startPos;
public Vector3 endPos;
float startTime;
float endTime;
float ystart;
float yend;
float xstart;
float xend;
float ydistance;
float xdistance;
float forcetime;
float xpower;
float ypower;
Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Began) {
startTime = Time.time;
startPos = touch.position;
xstart = startPos.x;
ystart = startPos.y;
}
else if (touch.phase == TouchPhase.Ended){
endTime = Time.time;
endPos = touch.position;
xend = endPos.x;
yend = endPos.y;
}
xdistance = xend - xstart;
ydistance = yend - ystart;
forcetime = endTime - startTime;
xpower = ControlledForce * (xdistance / forcetime);
ypower = ControlledForce * (ydistance / forcetime);
if (ydistance > 100) {
Lob ();
}
}
}
public void Lob(){
rb.AddForce (new Vector3 (xpower, ypower, zForce));
}
}
By no means am I saying anyone should use this. If anyone has anything to offer I would be very thankful. This simple chunk took me two days of research to figure out how to pull the variables out of input.gettouch and touchphase.ended. So happy I completed that hurdle.
If you test this script, you will find the game object will do two things I don't want to have happen.
1) Will sometimes go left rather than right and vice versa. 2) Your touch input while the object is mid air will come straight down impacting your plane with violent force due to the screen registering a touch I assume.
This is a lot of fun. Next comment I post will hopefully have a solution to controlling the xforce accurately.
Thanks everyone,
Chris
Hello so soon everyone.
I moved the calculations to only occur in the lob() function. I moved the lob() function to occur at the end of Touch.Phase = ended.
It feels much better. Here is the revised version.
using UnityEngine;
using System.Collections;
public class Swipetest1 : $$anonymous$$onoBehaviour {
public GameObject Source;
public float zForce;
public float ControlledForce;
public float maxTime;
public float $$anonymous$$SwipeDist;
public Vector3 startPos;
public Vector3 endPos;
float startTime;
float endTime;
float ystart;
float yend;
float xstart;
float xend;
float ydistance;
float xdistance;
float forcetime;
float xpower;
float ypower;
Rigidbody rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if (Input.touchCount > 0) {
Touch touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Began) {
startTime = Time.time;
startPos = touch.position;
xstart = startPos.x;
ystart = startPos.y;
}
else if (touch.phase == TouchPhase.Ended){
endTime = Time.time;
endPos = touch.position;
xend = endPos.x;
yend = endPos.y;
Lob ();
}
}
}
public void Lob(){
xdistance = xend - xstart;
ydistance = yend - ystart;
forcetime = endTime - startTime;
xpower = ControlledForce * (xdistance / forcetime);
ypower = ControlledForce * (ydistance / forcetime);
rb.AddForce (new Vector3 (xpower, ypower, zForce));
}
}
Your answer