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 rsd31 · Jul 15, 2016 at 09:42 AM · launchraycastslaunching

[HELP] [2D] Launch an object in a particular direction

So, I'm making a game (for touchscreen) for my college project. In this project, I want to launch a ball in the direction where user want's it to.

[What currently do I have?]:

 if (Input.GetMouseButtonDown(0)){
         hasStarted = true;
         print ("Left Mouse button clicked");
         this.GetComponent<Rigidbody2D>().velocity = new Vector2 (2f,7.5f);
 }

alt text

[Behavior I need]:

  • Ball should have a ray emerging from it in upward direction, the user should be able to move this ray between 10'o clock and 2'o clock.

  • After the user has acquired the direction he wants to point, he may tap on the screen to launch the ball (or maybe a button maybe used to launch the ball).

Can you please help me out with this?

capture.png (18.9 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

1 Reply

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

Answer by rsd31 · Jul 16, 2016 at 06:49 AM

So, I found the answer myself :D

1) For creating a ray, Just go here and copy down the script he has shown in the video ( Just remove the if (counter > dist) condition )

Link : https://www.youtube.com/watch?v=Bqcu94VuVOI

Let pointA be the ball, and pointB be an empty gameObject in the heirarchy. To move the ray, just move the pointB left or right, below is a script that I attached to pointB for moving it.

 public float speed = 15.0f;
     public float padding = 1.0f;
 
     float xmin;
     float xmax;
     // Use this for initialization
     void Start () {
         float distance = transform.position.z - Camera.main.transform.position.z;
         Vector3 leftmost = Camera.main.ViewportToWorldPoint(new Vector3(0,0,distance));
         Vector3 rightmost = Camera.main.ViewportToWorldPoint(new Vector3(1,0,distance));
         xmin = leftmost.x + padding;
         xmax = rightmost.x - padding;
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey(KeyCode.LeftArrow)){
             transform.position += Vector3.left * speed * Time.deltaTime;
             }
         if (Input.GetKey(KeyCode.RightArrow)){
             transform.position += Vector3.right * speed * Time.deltaTime;
             }
         float newX = Mathf.Clamp (transform.position.x, xmin, xmax);
         transform.position = new Vector3 (newX, transform.position.y, transform.position.z);
     }

I didn't had laser material, so I used flame material given in the standard assets of Unity 5.3.5f. And make the ray inActive after launching the ball. alt text

2) To launch the ball in particular direction go here, http://answers.unity3d.com/questions/315780/fire-a-projectile-towards-a-specific-object.html

Just instead of clone.velocity, use this.GetComponent<Rigidbody2D>().velocity

I used

 this.GetComponent<Rigidbody2D>().velocity = (ptB.transform.position - transform.position).normalized * speed;


c2.png (20.8 kB)
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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Unity Hub Launches 2018 instead of Unity 5 0 Answers

My game made by unity 2017.1.0f3 crash on launch on phone -1 Answers

How do I prevent Unity Hub from opening projects in the newest version? 0 Answers

How to I relaunch the application via key press? 1 Answer

"Launch" a rigidbody 3 Answers


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