- Home /
Can't get my laser beam to rotate
I've created a laser turret with a spawn point at the end of the gun. To fire the laser, I'm spawning a cube at the spawn point and scaling the cube to the length that I need for the laser. The problem is that the laser shows up facing the correct direction but half of it is in front and half at the back of the turret. I know that this is because of the origin point of the cube being dead centre of the cube. What I'm trying to do is shift the laser in the facing direction of the turret half of the length of the laser beam. I've tied everything I can think of and I can't get this part to work. One solution is to create an object with it's axes at one side then the scaling will take care of things. I I'm still curious to know if there's a simple Unity solution to this.
Any ideas?
Non-uniform scaling is liable to cause problems in any case. Have you tried using a LineRenderer for this? They seem like the obvious choice for lasers...
Answer by SmorgonMagma · Mar 15, 2012 at 01:15 PM
Actually, I solved this just after I posed the question. For anyone that's interested in how I did it, here's the code for the turret (which aligns itself with the player then scales the laser (a standard cube of 1x1x1 to the distance from the player then shifts itself along the local z axis of the attached spawn point by half the distance. The transform.Translate at the bottom of the code was the fix for my problem.
function Update() {
var amtToMove = LaserSpeedX * Time.deltaTime;
var relativePoint = transform.InverseTransformPoint(Player.position);
if(relativePoint.x <= -1.0) transform.Translate(Vector3.left amtToMove); else if(relativePoint.x >= 1.0) transform.Translate(Vector3.right amtToMove);
// Fire the laser if aligned else if(!LaserReloadTimer) { if(relativePoint.z < 50) {
// Create the laser var tempLaser = Instantiate(LaserShotZ,LaserSpawnPt.position,LaserSpawnPt.rotation);
// Set the length to the distance of the player
tempLaser.transform.localScale.z = relativePoint.z;
// Reposition relative to the laser centre point
tempLaser.transform.Translate(Vector3(0,0,1)*relativePoint.z/2);
LaserReloadTimer = LaserReloadTime;
}
} else LaserReloadTimer--; }
Your answer
Follow this Question
Related Questions
Help moving turret shoot lasers 2 Answers
Vector3.Forward not working? 1 Answer
Why does this script work correctly? 1 Answer