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 EronSadiku11 · Jul 25, 2017 at 05:46 PM · vector3linerenderersphere

Making a ball move according to swipe of the mouse

Hi, i'm new around here so not that good at programming. I want to make a game where you would swipe with your mouse, and a ball (sphere) would shoot into the goal post and try to hit the crossbar. If you could help that would be awesome. Here are my Ball and DrawLine class which just draws a line as a reference to get the last and first point and to convert them into a Vector3 for then to move the ball.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Ball : MonoBehaviour {
 
     public DrawLine line;
     float speed = 10f;
 
     void Update () {
         Vector3 input = line.endVector;
         Vector3 direction = input.normalized;
         Vector3 velocity = direction * speed;
         Vector3 moveAmount = velocity * Time.deltaTime;
 
         transform.Translate(moveAmount);
     }
 }

 using UnityEngine;
 using System.Collections;
 
 public class DrawLine : MonoBehaviour
 {
     //reference to LineRenderer component
     private LineRenderer line;
     //var to store mouse position on the screen
     private Vector3 mousePos;
     //assign a material to the Line Renderer in the Inspector
     public Material material;
     //number of lines drawn
     private int currLines = 0;
 
     public Vector3 startVector;
     public Vector3 endVector;
 
     void Update()
     {
         //Create new Line on left mouse click(down)
         if (Input.GetMouseButtonDown(0))
         {
             //check if there is no line renderer created
             if (line == null)
             {
                 //create the line
                 createLine();
             }
             //get the mouse position
             mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             startVector = mousePos;
 
             //set the start point and end point of the line renderer
             line.SetPosition(0, mousePos);
             line.SetPosition(1, mousePos);
         }
         //if line renderer exists and left mouse button is click exited (up)
         else if (Input.GetMouseButtonUp(0) && line)
         {
             mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             //set the end point of the line renderer to current mouse position
             line.SetPosition(1, mousePos);
             //set line as null once the line is created
             line = null;
             currLines++;
         }
         //if mouse button is held clicked and line exists
         else if (Input.GetMouseButton(0) && line)
         {
             mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             endVector = mousePos;
             //set the end position as current position but dont set line as null as the mouse click is not exited
             line.SetPosition(1, mousePos);
         }
 
     }
 
     //method to create line
     private void createLine()
     {
         //create a new empty gameobject and line renderer component
         line = new GameObject("Line" + currLines).AddComponent<LineRenderer>();
         //assign the material to the line
         line.material = material;
         //set the number of points to the line
         line.SetVertexCount(2);
         //set the width
         line.SetWidth(0.15f, 0.15f);
         //render line to the world origin and not to the object's position
         line.useWorldSpace = true;
 
     }
 
     public Vector3 getStartVector()
     {
         return startVector;
     }
 
     public Vector3 getEndVector()
     {
         return endVector;
     }
 
 }

Thanks!

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

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

80 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

slowly rotate a object *need quick fix* 0 Answers

Basic LineRender Reflection 2 Answers

Position waypoint along sphere 1 Answer

Line renderer rotation and alignment 1 Answer

Linerenderer doesn't match my ray. 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