So I’m making this game cloning the facebook football mobile game. I could barely make the ball bounce whenever I touch it, but I couldn’t figure a way to make it go left and right if you touch the ball from the opposite direction. I was thinking like the post suggests about a way to apply a force from the position touched to the position of the ball.
using UnityEngine;
using System.Collections;
public class Touch : MonoBehaviour {
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
Debug.Log ("Clicked");
Vector2 pos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
RaycastHit2D hitInfo = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (pos), Vector2.zero);
// RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
if (hitInfo)
{
Debug.Log (hitInfo.transform.gameObject.name);
// Here you can check hitInfo to see which collider has been hit, and act appropriately.
hitInfo.rigidbody.AddForce(new Vector2( 0, 300));
}
}
}
}