- Home /
Question by
AndreiCelTare · May 20, 2015 at 05:17 AM ·
camerarigidbody3dmousemouseposition
3D mouse position
Hi, I'm trying to make a script for a free throw ball game and need to get the mouse position in the world, and it should be in 3D to be able to throw in all 3 axes. I'm kinda new to scripting and the script I wrote works, but not right. I am not sure how to get the depth or the y axis working, because the screen is only 2d.
using UnityEngine;
using System.Collections;
public class ShootBall : MonoBehaviour {
private Rigidbody rb;
private RaycastHit hit;
private Vector3 com;
private Vector3 shootDirection;
void Start () {
rb = GetComponent<Rigidbody>();
}
void Update () {
}
void OnMouseDown (){
com = rb.worldCenterOfMass;
Debug.Log (com);
}
void OnMouseDrag (){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Debug.Log(ray);
Physics.Raycast(ray, out hit);
}
void OnMouseUp (){
shootDirection = com - hit.point;
rb.AddForce (shootDirection * 100);
}
}
Comment
Your answer
Follow this Question
Related Questions
Getting the z of mouse position? 1 Answer
Adjusting Camera for Drag Rigid Body Script. 0 Answers
How should I control a 'rake' that pushes objects around a table? 2 Answers
Mouse axes based on position, not movement. 1 Answer
Mouse follow on network 0 Answers