Drawline for a longer distance
Hello!
I have this code that draws a line to the mouse cursor. What i want to achive is drawing it "beyond" the cursor. To be more specific id like to draw a line to the mouse and then further with the same direction.
The line is suppose to symbolize a bullets trajectory which should not ofcourse stop at the mouse position but continue further.
if (Input.GetButtonDown("Fire1")) {
mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, -.5f);
lookPos = new Vector3(Camera.main.ScreenToWorldPoint (mousePos).x,Camera.main.ScreenToWorldPoint (mousePos).y,this.transform.position.z);
lookPos = lookPos - transform.position;
float angle = Mathf.Atan2 (lookPos.y, lookPos.x) * Mathf.Rad2Deg + 90;
transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
RaycastHit2D hit = Physics2D.Raycast (muzzle.transform.position, (mousePos - muzzle.position), weaponRange, whatToHit);
if (Time.time > nextFire)
{
nextFire = Time.time + fireRate;
Debug.DrawLine (muzzle.position, (lookPos+muzzle.position), Color.red,0.05f);
Debug.Log ("shot");
}
}
I have tried to fiddle with
Debug.DrawLine (muzzle.position, (lookPos+muzzle.position), Color.red,0.05f);
but to no success.
Any help is appretiated!
Answer by kidmosey · Sep 25, 2016 at 01:05 PM
Your line is only getting drawn for the frame that is resetting the nextFire variable.
So if you are getting more than 100 fps, this line is only displaying for one tenth of a second.
Also, depending on which game object this behavior is attached to, you may not see the line because it is drawing in the look direction of the camera.
Your answer
Follow this Question
Related Questions
How to change the position of a Raycast (on Center of the Player 2D)? 0 Answers
wall check is always returning true even though the distance doesn't fit my parameters :( 1 Answer
Plane is blocked all Raycast 0 Answers
I can't get Physic.Raycast to work,My Physics.Raycast don't work 0 Answers
Inconsistent Jump Heights 1 Answer