Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Maveej · Nov 21, 2016 at 07:20 AM · forcetouchscreenthrowarc

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

alt text alt text

seconddisplay.png (4.8 kB)
firstdisplay.png (4.3 kB)
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 Maveej · Nov 22, 2016 at 03:57 AM 0
Share

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

avatar image Maveej · Nov 22, 2016 at 04:06 AM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Force to reach point 1 Answer

calculate the estimated distance travel based on the Force applied to Rigid Body 1 Answer

Realistic/No gravity spaceship movement? 1 Answer

Jump without character controller?? 2 Answers

How to calculate force from explosion on a rigidbody 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