I am making space shooter kind of scene. But in that i am shooting in the direction of mouse position.
All this works fine. i am having Enemy script like this:
function Start () {
SetPositionandSpeed();
}
function Update () {
var amttomove : float = currentSpeed * Time.deltaTime;
transform.Translate(Vector3.down * amttomove);
if(transform.position.y <= -6.00f)
{
SetPositionandSpeed();
}
}
function SetPositionandSpeed()
{
currentSpeed = Random.Range(minSpeed , maxSpeed);
x = Random.Range(-4.00f , 4.00f);
z = -6.72f;
y = 8.0f;
transform.position = new Vector3(x , y , z);
}
On mousebutton down I am Generating Bullet.
And my shooting script is this :
function Update ()
{
var bullet : GameObject = GameObject.Find("Bullet");
var hit : RaycastHit ;
var ray : Ray = Camera .main .ScreenPointToRay (Input.mousePosition );
if(Physics.Raycast (ray, hit)){
target = (hit.point - bullet.transform.position).normalized;
}
if(target != null){
var moveAmount : float = 6 * Time.deltaTime;
transform.Translate(target * moveAmount);
}
if(transform.position.y >= 7.0f)
{
Destroy(gameObject);
}
}
But sometimes when my mouse position comes over the enemy , my bullet changes direction slightly… So what do i need to do? May be Problem with RayCast…
Please Help me to solve out my problem…
Thanks for your support and help in advance…