I am creating a rectangular bat dynamically. The length of bat and angle depends upon the drag of mouse.
The starting point say POINT1 of the bat is the point where mouse left button is down.
End point/length say POINT2 of the bat where I release the mouse button.
Now I am trying to hit a moving ball using the bat. When I move POINT2 slow the collision is perfect and the ball is hit. But when I move POINT2 fast the collision do not happen.
i have even tried the Donotgothrough script but no avail.
http://wiki.unity3d.com/index.php?title=DontGoThroughThings#
Below is the code I am using
private var point1:Vector3;
private var point2:Vector3;
var lengthOfLine:float;
var cube : GameObject;
private var ray:Ray;
private var hit:RaycastHit;
private var background:GameObject;
private var colliderOfBg:BoxCollider;
function Start ()
{
colliderOfBg = GameObject.Find(“Bg”).GetComponent(BoxCollider);
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.renderer.material.color = new Color(0.259,0.807,1);
cube.AddComponent(Rigidbody);
cube.rigidbody.useGravity = false;
cube.rigidbody.mass = 10;
cube.rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
cube.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
background = GameObject.Find(“Bg”);
lengthOfLine = (background.renderer.bounds.max.x + background.renderer.bounds.max.y)/3;
}
function Update ()
{
if(!Ball.pauseGame)
{
if(Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(colliderOfBg.Raycast(ray,hit,100.0))
{
if(Input.GetMouseButtonDown(0))
{
point1 = hit.point;
point1.z = 0;
cube.transform.position = point1;
}
point2 = hit.point;
point2.z = 0;
if(Vector3.Distance(point1,point2) < lengthOfLine)
{
var speed:float = Vector3.Distance(point1,point2);
var direction:Vector3 = point2 - point1;
cube.transform.LookAt(point2);
cube.transform.localScale = new Vector3(0.01,0.09,direction.magnitude);
cube.transform.position = Vector3.MoveTowards(point1, point2, speed/2);
}
}
}
}
}
1504742–84967–$DrawLine.js (1.78 KB)