- Home /
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
Your answer
Follow this Question
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