I am trying to make a golf game. I am trying to project a ray from the finger position to the ball then shoot the ball when the finger is lifted in the direction of the ray. Also I would like it to get the force to shoot the ball from the distance from finger position to ball.
I am really not sure where to start, any ideas?
Thanks
Thanks I looked at those but I am not sure as to the type of raycast to use. All I can figure out is I need
RaycastHit.transform.gameObject : GameObject
to detect when it hits the golfball, but How do you calculate direction of raycast or length?
Thanks
Brandon
Thanks that helped a lot. From that example I got this
using UnityEngine;
using System.Collections;
public class Raycaster : MonoBehaviour {
Plane ballPlane = new Plane(player.position, Camera.main.forward);
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float point;
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
ballPlane.Raycast(camRay, out point);
Vector3 position = camRay.GetPoint(point);
Ray shotRay = new Ray(player.position, (position - player.position));
}
}
But I need it to start casting the ray on touch then when the person lifts their finger it move the ball in the direction of the ray with the force of the distance.
Thanks
Brandon
using UnityEngine;
using System.Collections;
public class Raycaster : MonoBehaviour {
Plane ballPlane = new Plane(ball.position, Camera.main.forward);
public Rigidbody ball;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float point;
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);
ballPlane.Raycast(camRay, out point);
Vector3 position = camRay.GetPoint(point);
Ray shotRay = new Ray(ball.position, (position - ball.position));
if (touch.phase != TouchPhase.Ended)
ball.AddForceAtPosition (Vector3.forward * force);
}
}
Ok this is what I got now. but how do I add the force and direction from the raycast to the ball
Thanks
Brandon