Bounce the 3d ball opposite to the touched point
I'm trying to create a ball juggling game. I want the ball to move the opposite direction of the touched point. I mean, it should move to northwest if i touched it in southeast part. Right now i can only move it in upper direction only. I need help. Here is the code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour { private GameObject ball; Rigidbody rb;
// Use this for initialization
void Start () {
ball = GameObject.FindGameObjectWithTag ("Player");
rb = ball.GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.name == "Ball")
{
//rb.AddForceAtPosition(hit.transform.position.normalized, transform.position);
//rb.AddForce(Vector3.up*200);
rb.AddForce((ball.transform.position - hit.point).normalized * 20, ForceMode.VelocityChange);
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Rigidbody addforce not working 0 Answers
rigidbody,addforce is affecting all clones which are clicked on through raycast 0 Answers
AddExplosionForce not affecting every rigidbody 1 Answer
DESPERATE How to align transform.up with another vector3 (direction) ? 1 Answer
Why Rigidbody.AddForce is not acting like the correct physical force? 1 Answer