Ball not moved on drag but script detects velocity?


#1

Here, I have a script that should roll the ball on drag and based on drag. However, when I do this on Game mode, I see no change. But despite that, print(); tells me there is in fact velocity on the ball. Why isn’t the ball moving and why is my debug telling me that there is velocity? I understand what this script is trying to do, and any error I made is not clear.

Ball.cs / BallBehavior.cs
	void Start () {
		rigidBody = GetComponent<Rigidbody> ();
		rigidBody.useGravity = false;

	}

	public void Roll(Vector3 velocity){
		rigidBody.useGravity = true;
		rigidBody.velocity = velocity;
		print ("Velocity " + rigidBody.velocity);

	}
BallDrag.cs
	void Start(){
		ball = GetComponent<BallBehavior>();
	}

	public void DStart() {
		dragstartPos = Input.mousePosition; // Assigns value to dragstartPos (see decleration)
		timeStart = Time.time; //gets current time to determine swipe output
		print ("Drag Started");
	}

	public void DEnd() {
		dragendPos = Input.mousePosition; // Assigns value to dragstartPos (see decleration)
		timeEnd = Time.time;

		float dragDuration = timeEnd - timeStart;
		float launchVelocityX = (dragendPos.x - dragstartPos.x) / dragDuration; //gets the velocity based on speed and position of drag
		float launchVelocityZ = (dragendPos.y - dragstartPos.y) / dragDuration; // tl:dr velocity based on dragging of ball

		Vector3 finalVelocity =  new Vector3 (launchVelocityX, 0, launchVelocityZ); // gets the velocity the ball will be launched
		print ("final velo " + finalVelocity);
		ball.Roll (finalVelocity); // Rolls ball on end of drag
	}

#2

Hey Bill,

Harder to diagnose perhaps on this one, without seeing the whole project, or having the specific lecture you are on for comparison (although the former would be better still).

I note that you have possibly renamed one of the scripts, Balls.cs to BallBehaviour.cs, one thing I would check is that the script(s) you believe is/are attached to the GameObject still is/are. Depending on the version of Unity you use it can happily break references to scripts when they are renamed. I’m sure you already know if you have got this far through the course, but the class names and scripts names need to match also.

A couple of things to perhaps check, if neither of these are the source of your problem I would be happy to take a look at it for you if you could zip up/share your project files.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.


#3

Hi,

Here is the project saved in a .rar file on Google Drive. Hope you can fix it, this is a pretty weird one. I’ve tried your suggestions, though nothing has worked so far. This one’s stumped me as far as I can tell : )


#4

Hi Bill,

Ok, after a little investigating I have found your issue.

I had a rummage around in your existing scene and could experience the issue you mentioned. Rather than changing things in there I created a new scene to experiment in.

I created a cube, flattened it out and used that as a floor. I dropped your ball prefab, changed the scale from 20 on each axis to 1 and ran then game. No problems, but no functionality.

Next I updated the BallBehaviour.cs script to include this line at the bottom of the Start method;

rigidBody.Roll(new Vector3(0f, 0f, 5f);

I ran the game, the ball started to move - great - nothing on the prefab to suggest that the ball cannot move, and confirmed that your BallBehaviour.cs script was ok.

I reverted the code changes.

I noted that the drag functionality didn’t appear to be creating the logs in the console as it did in your scene. I took a look back at what was required in order to support this and realised I needed a Canvas, this also added an EventSystem I added one of those and then added a panel which would detect the mouse pointer clicks.

I left the panel full size, so it covers the whole canvas/screen (when the game is run). I added the Event Trigger components to the panel. I then set them as follows;

image

Minor note, but not the source of your problem, you have the DStart and DEnd methods wired up around the other way. You want the drag start method to be wired to the Pointer Down event and the drag end method wired to the Pointer Up event.

I ran the game again, success, your messages appeared and the ball went flying across my floor.

I returned to your scene and took a look at your canvas.

I then added a new canvas so I could flick between the two and look at differences. The first difference was the Layer that your canvas was set to, it was set to Default where-as the new canvas was set to UI. I changed yours but this made no difference to the behaviour. I disabled the new panel.

I disabled all of the panels within your canvas apart from the Touch Input. Ran the game, still no movement of the ball.

I then re-enabled the new canvas, added a panel, leaving it with the default image, moved it to the centre of the canvas, reduced its width a little, set the anchor points to the centre (e.g. not stretched) and added the two Event Triggers (as above in the screenshot). I disabled your Touch Input control, the ball behaved as expected;

(note, I’ve disabled the SpongeBob skybox in the above video so that the anchors for the panels were easier to see)

To bring the new panel more inline with your own I then removed the image, and set the alpha so that it was completely transparent.

I then copied your RectTransform values and pasted them into the new panel, ran the game, still works, although the panel now pokes out the top of the canvas.

I then duplicated the disabled L. Panel and R. Panel GameObjects from your original canvas and pasted them as child objects to the new canvas, enabled them and ran the game. Everything still worked as expected.

I cannot say what the exact issue has been, although is seems very related to your canvas and its child objects.

To resolve your issue, I would suggest;

  • create a new canvas
  • duplicate the L. Panel and R. Panel GameObjects and child them to the new canvas
  • tidy up their positions and anchors
  • create a new panel for the main centre area (between the two other panels)
  • add the Event Triggers to this panel, setting the methods up in the correct order
  • disable the original canvas and all child objects
  • run and test the game

If it works as expected, delete the original canvas and child game objects.

Hope this helps :slight_smile:


#5

I’ve followed your suggestion, but it doesn’t appear to be working, which is quite strange. My new canvas replicates your suggestion, with the similar anchors in the touch-input area as in the video. Any idea why? I’ve tried changing the set-up of the canvas, changing the shape of the input, nothing has worked so far.


#6

Hi Bill,

I’d probably need to see the project again to be of much further help. Happy to take another look if you want to zip up what you have at the moment, with your changes from the above, which haven’t worked for you. If I can spot anything I can reply and let you know :slight_smile:


#7

The link I set you has been updated with the panel I added according to your instructions, the left and right panels have been removed to mirror what is seen in the video (in the case that they interfere with the touch input). Here is the link I sent you if the link posted prior does not work for any reason. Thanks :slight_smile: !!!


#8

Hi Bill,

I grabbed your project and ran it, I can experience the issue.

I disabled your canvas, created a new canvas, added a child UI Panel to it and added the two Event Triggers, dragging the Bowling Ball GameObject from the Hierarchy as the object and then selected the appropriate two drag methods from your script. It worked.

I then disabled what I’d added and re-enabled what you had. I wondered whether you may have not associated the actual Bowling Ball GameObject from the Hierarchy in your Event Triggers, so I associated it for them and it resolves the issue.

Here’s a video of your project, straight from the .rar file, firstly not working, and then with the quick change which makes it work - and a demonstration of how I cannot bowl!

Here’s a quick video of me dragging the prefab of the Bowling Ball into the Event Triggers which then creates the same behaviour you were previously experiencing;

…and finally, here’s a fresh copy of the project opened from the .rar file where I select the object associated with the Event Triggers to indicate what it is, and as you can see, it was the prefab.

In hindsight, I should have probably explained that step in more detail in my previous reply. I note that I say about adding the Event Triggers but I didn’t specifically state with the Bowling Ball from the Hierarchy, I made an assumption there that you would be doing so. Chances are you have repeated what you had done the first time, using the prefab instead. Also, at that stage, I hadn’t realised that this was the issue, as it seemed more related to the canvas/panel but of course we were doing things differently which is why we had different outcomes. My apologies for not spotting this more specifically the first time around.

Hope this helps :slight_smile: