- Home /
click on object, drag to draw line in opposite direction, mouseUp to apply force 3D
I haven't been sure where to start with this question and all related questions that i've been able to find so far have only dealt with 2D space.
What I'm trying to achieve is to: 1. begin drawing a line when an object containing the script is clicked on in the game. 2. when the mouse is dragged backwards, a line is drawn in the opposite direction, however it needs to be locked to the XZ axis ( So the Y Coordinate stays at 0). 3. when the mouse is released, a physics calculation is done to add force to the ball and it gets shot away from the mouse.
I wrote up some pseudo code here to help visualize what I'm trying to achieve:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootBall : MonoBehaviour {
private Vector3 clickedPos; //where the user clicked
private Vector3 releasePos; //where the user releases the mouse
public float powerMultiplier; //adds extra force to object when released.
public float minPull; //minimum drag distance for physics force to be added
public float maxPull; //maximum distance the ray will calculate physics for to limit force added
//minPull and max Pull represent magnitudes
void Start () {
//get rigidbody component from gameobject (assume there is one attached).
}
void Update () {
}
void OnMouseDown(){
//Start to draw a ray
//clickedPos = this position
}
void OnMouseDrag(){
//Draw line in opposite direction BUT restrict it to the x and z axis
//(if current position - clickedPosition > maxPull) the line doesn't grow
}
void OnMouseUp(){
//if(clickedPos - releasePos > minpull) rigidbody.addforce(distancePulled * powerMultiplier)
}
}
Comment