- Home /
Dragging and throwing a ball
I've set a scene up to pick up a ball on mouse click and drag it according to raycasts from the camera to a collider surface. My objective is to apply velocity according to the drag direction on release. To accomplish this, I made an array that collects a "trail" of previous positions. I'm simply trying to add velocity on release based on a direction created from the oldest trail position to the ball position.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Toss : MonoBehaviour {
public float throwForce; // a scalar for the force on ball release
public float dragDelay; // the interval on which the trail information is collected
public GameObject followBall; // an indicator of the oldest trial position
private RaycastHit[] hits; // raycast for background plane
private RaycastHit[] rayHits; // raycast for ball
public Camera mainCamera; // input for main camera
private Rigidbody rb; // this rigidbody
private bool dragging; // indicator of drag
private float dragCounter; // counter variable for trail information
private GameObject earlyBall; // indicator of trail position
private Vector3[] trail; // array for trail info
void Start () {
dragging = false;
rb = GetComponent<Rigidbody>();
trail = new Vector3[5];
}
// Update is called once per frame
void Update () {
// if mouse is clicked, raycast for colliders with the tag "Ball"
if (Input.GetMouseButtonDown(0))
{
Ray ballRay;
ballRay = mainCamera.ScreenPointToRay(Input.mousePosition);
rayHits = Physics.RaycastAll(ballRay);
for (int q = 0; q < rayHits.Length; q++)
{
if (rayHits[q].collider.tag == "Ball")
{
dragging = true; // When ball is clicked, start dragging
rb.useGravity = false; // Turn off gravity
}
}
}
if (dragging == true) // If dragging is true, raycast to background object and position ball according to hits
{
Ray ray;
ray = mainCamera.ScreenPointToRay(Input.mousePosition);
hits = Physics.RaycastAll(ray);
dragCounter = dragCounter + Time.deltaTime;
trail[0] = rb.position;
for (int i = 0; i < hits.Length; i++)
{
if (hits[i].collider.tag == "Background")
{
rb.position = new Vector3(hits[i].point.x, hits[i].point.y, hits[i].point.z);
}
}
if (dragCounter >= dragDelay) // Set trail information according to interval value
{
dragCounter = 0;
followBall.transform.position = trail[4];
for (int x = 0; x < trail.Length; x++)
if (x + 1 < trail.Length)
{
trail[x + 1] = trail[x];
}
}
}
if (Input.GetMouseButtonUp(0)) // if mouse is released apply velocity according to earlier position and current position, scale by throwForce
{
if (dragging == true)
{
dragging = false;
rb.position = trail[0];
rb.velocity = trail[4] - trail[0] * throwForce;
rb.useGravity = true; // reactivate gravity
earlyBall = Instantiate(followBall, trail[0], Quaternion.identity); // instantiate indicators of earlier position and position on throw
earlyBall.GetComponent<Renderer>().material.color = Color.red;
Instantiate(followBall, trail[4], Quaternion.identity);
}
}
}
}
The dragging works and the trail seems to work as intended (indicated by smaller ball). But when I try to "throw" I get a bad result...
I feel like I should get the right direction for velocity from subtracting the trail position from the ball position, but no luck.
Answer by JVene · Jul 25, 2018 at 02:20 AM
From what I can decipher reading the code, it appears that you depend upon the trail array of vectors to control the force.
Are you intentionally copying element 0 to all elements of that array?
for (int x = 0; x < trail.Length; x++)
if (x + 1 < trail.Length)
{
trail[x + 1] = trail[x];
}
This starts by pointing at element 0. It copies element zero to element 1. Element 1 and 0 are now the same thing. Next loop, 1 is copied to 2. Now all 3 are the same.
I don't think that is your intent, and it may be the origin of the problem of other code relies upon it.
To "scoot" the list toward the end of the array, you have to start and the end (length - 1), and work toward 0.
That said, have you heard of a circular queue? The can do this kind of thing without the time spent scooting data.
Sorry, that's my mistake - though I do get 1 interval of previous position. I'm feeling like the core problem lies with how I'm using this value to produce a velocity. Thanks for the reply. I will look into implementing a circular cue.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnCollisionStay only detects collisions after a secondary collision 0 Answers
C# script not working 1 Answer
Why isn't OnTriggerEnter() called in the beginning of the game? 1 Answer