Touch Input not working correctly

Greetings,
The ball launches on mouse down but does not compute speed or direction. I removed the “Launch (launchVelocity)” on the ball script but to no avail. Thanks for the assist.

Hi and welcome to the community :slight_smile:

There are quite a few places where the control of the ball can go amiss in this section of the course.

If you would like to share your project files I will happily take a quick look for you and see if I can spot anything.

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.

Also, it is better to copy/paste your code into the forum and apply the code formatting characters before and after it, as opposed to taking screenshots. They tend to not be overly readable and on mobile devices require a lot of scrolling around to read each line of code. Additionally, when people offer to help you they are then unable to simply copy/paste a part of your code back to you with correction/suggestions. You will find you get more responses if you posts are easier to respond to :slight_smile:

Screenshots are, of course, very useful for error messages or details from within the Unity editor itself, or just to showcase your work! :slight_smile:


See also;

Thanks. And thanks for the advice on posting. I tried deleting and recreating the touch panel but that didn’t seem to help. I’ve the link to the zipped file. Thanks again.

https://drive.google.com/drive/folders/191diaqzkksdybbwshdFCj-Pg7rRyrhjv

You’re very welcome :slight_smile:

Nope :slight_smile:

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.

I tired the upload but that failed I moved to Google drive. Thanks

1 Like

10.4MB :slight_smile:

Downloading now, I have to leave in a few minutes (school run) but I will see if there is anything quick I can spot, if not I will reply when I get back.


Updated Tue Dec 11 2018 09:31

Sorry for the delay @namevac01, school run, errands, and bad traffic!

Ok, so I had a quick look. The issue is in your Ball.cs script;

using UnityEngine;

public class Ball : MonoBehaviour 
{
	private Rigidbody rigidBody;
	public Vector3 launchVelocity;
	private AudioSource audioSource;

	// Use this for initialization
	void Start ()
        {
		rigidBody = GetComponent<Rigidbody> ();
		rigidBody.useGravity =false;
		//Launch (launchVelocity);
	}

	public void Launch (Vector3 velocity)
	{
		rigidBody.useGravity = true;
		rigidBody.velocity = launchVelocity;

		audioSource = GetComponent <AudioSource>();
		audioSource.Play ();
	}	
}

In the above you declare a member variable launchVelocity, within the Inspector this is set to 1, 0, 450.

In your DragLaunch.cs script you have a DragEnd method;

public void DragEnd () 
{
	// Launch the ball
	dragEnd = Input.mousePosition;
	endTime = Time.time;

	float dragDuration = endTime - startTime;

	float launchSpeedX = (dragEnd.x - dragStart.x) / dragDuration;
	//you can't do z so you calculate y and it translates
	float launchSpeedZ = (dragEnd.y - dragStart.y) / dragDuration;
	// needs three numbers in 3D space
	Vector3 launchVelocity = new Vector3 (launchSpeedX, 0, launchSpeedZ);
	ball.Launch (launchVelocity);
}

On the last line here you call the Launch method and pass in the launch velocity which has been calculated based on the mouse movement.

The Launch method within Ball.cs doesn’t currently use this though, it ignores the parameter that you pass in, in favour of the member variable, so each time you call Launch it is still use a Vector3 of 1, 0, 450.

If you pop the following Debug.Log statements into your Launch method within Ball.cs you’ll be able to see what is happening;

public void Launch (Vector3 velocity)
{
    Debug.Log("Member variable `launchVelocity` = " + launchVelocity.ToString());
    Debug.Log("Parameter `velocity` = " + velocity.ToString());

    rigidBody.useGravity = true;
    rigidBody.velocity = launchVelocity;

    audioSource = GetComponent <AudioSource>();
    audioSource.Play ();
}

The solution here is to use the velocity parameter value that you pass to the Launch method from the DragEnd method within DragLaunch.cs.

public void Launch (Vector3 velocity)
{
    rigidBody.useGravity = true;
    rigidBody.velocity = velocity;    // corrected line

    audioSource = GetComponent <AudioSource>();
    audioSource.Play ();
}

Hope this helps :slight_smile:

I see said the blind man as he picked up the hammer and saw. I don’t need a public Vector3 launchVelocity as it confuses the program. Thanks a lot. I’m just learning this and you’ve been a big help. If you don’t mind, do you know of any C# tutorials. I feel like I need to understand the language better. Thanks again.

1 Like

Hi,

hehe, we often learn through mistakes but perhaps put the hammer and saw down, just for now :slight_smile:

It isn’t so much about the variable confusing the program, from memory, you started off in this section by just clicking the button and the ball would travel down the aisle and hit the pins. In order to make that piece of functionality work you created a variable which was exposed so it could be seen in the Inspector where you could then set the velocity of the ball. This is where you set the 1, 0 , 450. You then used this variable in the Start method, which is probably why you have it commented out there now, because you were building up the functionality as the lectures/section progress.

With the ball now moving, the next step was to give it that kind of flick input control, the drag start/end functionality that you build up. When this was added, you could argue that the other member variable wasn’t really necessary anymore and could have been removed. Had it been removed you would have, probably, instinctively used the parameter of a similar name in your Launch code.

I can’t remember whether the original member variable was removed in the course but I would suspect not, so it isn’t like this is a huge mistake on your part, you just had two variables that were being used for the velocity of the ball and you wired your ball up to use the first one rather than the one which had been set to use the values from the drag start/end code you created. No biggy :slight_smile:

Hopefully what you’ll take from this is the understanding that the parameter and member variables are different, as are local variables, local to the method and that a Debug.Log statement goes a long way when it comes to debugging issues :slight_smile:

Regarding the C# tutorials, not really, I haven’t used any myself so I can’t really recommend any. I’m sure there are plenty on YouTube, also, the tutorials on the Unity website itself are often quite good as they tend to speak fairly slowly and concisely but these will often be for specific things within Unity, as opposed to creating variables/methods. The Microsoft Docs website is also good for examples and explanations, but their approach does vary, sometimes it will be written fairly clearly and I would suggest easily for someone who is new to C# to understand, other times you’d probably have had to have used the documentation quite a few times to get the gist of what they are putting across.


See also;

Privacy & Terms