- Home /
shooting laser to screen middle doesnt work...
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.
so is this an unfixable error? There must be a way to create a crosshair!
so the laser should be shot from the middle to my gun?
I 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.
Answer by goo-muffin · Jan 27, 2013 at 01:06 PM
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;
var startPos2 :Vector3;
var offset :Vector2;
function Start(){
lineRenderer.useWorldSpace = true;
}
function Update(){
startPos2 = transform.position;
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;
var ray : Ray = c.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
lineRenderer.SetPosition(0, startPos2);
if (Physics.Raycast (ray, hit, 100)){
lineRenderer.SetPosition(1, hit.point);
print("hit");
}else{
lineRenderer.SetPosition(1, c.ScreenToWorldPoint(new Vector3(Screen.width / 2, Screen.height / 2, maxLength)));
}
}else{
lineRenderer.SetPosition(0, Vector3(0,0,0));
lineRenderer.SetPosition(1, Vector3(0,0,0));
}
if(Input.GetKeyDown("f")){
if(Time.timeScale == 0){
Time.timeScale = 1;
}else{
Time.timeScale = 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);
}
I got it on my own
yea you'd want to shoot out to the middle then whatever you hit have the gun look at that so its aimed at it.
Your answer
Follow this Question
Related Questions
FPS Run Animation Gun - Rotation Issue 0 Answers
laser pointer 1 Answer
I need help with a firing script 1 Answer
Gun not rotating with the camera? 1 Answer
Rotation/position question. 1 Answer