Hi!
This is a quite complex question. I got a lasergun shooting a laser forward to where I look until it hits something. When it doesnt hit something it works perfect. He shoots so that the laserpointer ends at the screenmiddle. But if it comes closer to an object it doesnt shoot to the 3d point of the Screen middle… Here a picture:

Does it has something to do with the angle of my gun? Or how can I fix this?
BTW: My script looks like this:
var lineRenderer :LineRenderer;
@HideInInspector
var startPos :Vector2;
var maxLength :float = 1000;
var multiplier :float = 5;
var duration :int;
var isshooting :boolean;
var currentduration :int;
var point :Texture;
var line :Texture;
var chWidth :float;
var length :float;
var c :Camera;
function Start(){
lineRenderer.useWorldSpace = false;
}
function Update(){
if(Input.GetMouseButtonDown(0)){
if(isshooting == false){
isshooting = true;
currentduration = 0;
}
}
if(Input.GetMouseButton(0)){
if(isshooting == true){
if(currentduration <=duration){
isshooting = true;
currentduration ++;
}else{
isshooting = false;
}
}
}else{
isshooting = false;
}
if(isshooting){
var hit :RaycastHit;
Physics.Raycast(transform.position,transform.forward,hit);
if(hit.collider != null){
lineRenderer.SetPosition(1, Vector3(startPos.x, startPos.y, hit.distance * multiplier));
}else{
lineRenderer.SetPosition(1, Vector3(startPos.x, startPos.y, maxLength));
}
}else{
lineRenderer.SetPosition(0, Vector3(0,0,0));
lineRenderer.SetPosition(1, Vector3(0,0,0));
}
}
function OnGUI(){
/*GUI.DrawTexture(Rect(Screen.width / 2 - chWidth / 2 - 1, Screen.height/2 -length/2, 2, length), line);
GUI.DrawTexture(Rect(Screen.width / 2 + chWidth / 2 - 1, Screen.height/2 -length/2, 2, length), line);
GUI.DrawTexture(Rect(Screen.width / 2 - length / 2, Screen.height/2 - chWidth/2 - 1, length, 2), line);
GUI.DrawTexture(Rect(Screen.width / 2 - length / 2, Screen.height/2 + chWidth/2 - 1, length, 2), line);*/
GUI.DrawTexture(Rect(Screen.width / 2 - 3, Screen.height / 2 - 3, 6, 6), point);
}
This isn't a laser error. You need to update the laserpointer to reflect when you have some object in front, like a real laserpointer. If you laser won't come exact from middle, you can't shoot at exact middle without a certain distance.
– FLso is this an unfixable error? There must be a way to create a crosshair!
– goo-muffinso the laser should be shot from the middle to my gun?
– goo-muffinI believe that you need to shot from the middle of screen or update the laserpointer when you have a object in your front if you wish to make it accurate.
– FL