- Home /
how to add force in z direction in input.getmousebuttondown ?
hi, im using below script to add force to a ball, but it is adding force to X & Y axis only. Z is showing Zero. How to add force relative to swipe? I've attached this script to main camera so that i can wsipe at any point please halp me to z direction.
using UnityEngine; using System.Collections;
public class CameraScript : MonoBehaviour {
Vector3 mousestart, mouseend;
float starttime, endtime;
Vector3 force;
public GameObject ball;
Rigidbody rb;
// Use this for initialization
void Start ()
{
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
starttime = Time.time;
mousestart = Input.mousePosition;
}
if (Input.GetMouseButtonUp (0))
{
endtime = Time.time;
mouseend = Input.mousePosition;
float totaltime = endtime - starttime;
Vector3 distance = mouseend - mousestart;
force = (distance/totaltime);
Debug.Log(distance.z);
ball.rigidbody.AddForce(force);
}
}
}
Comment
Best Answer
Answer by IgorAherne · Sep 02, 2015 at 07:34 AM
Vector3 distance.z = (mouseend - mousestart).magnitude;