- Home /
Turret + Cannon Mouse movement
Hey guys, this is a problem I've been strugling for days now but I can't seem to find a proper solution that will help me.
I've looked on other questions and formus but still I haven't find exactly what I need. I need two scripts: one for a turret that can spin around 360 degrees following the Mouse's X position and to stop when the X positions are the same. And I also need another script to rotate the cannon that's attached to the turret, but this has to spin only verticaly and between limits so it doesn't rotate into itself.
I am trying to make like a world of tanks playing style, this is what I'm trying to achieve:
http://www.youtube.com/watch?v=x__yN6hwuvI
Also I don't want any rigid bodies, nor do I want to use the Character Controller package in unity because it uses to much resources.
So far I came up with this:
Gun Script:
var goTarget : GameObject;
var maxDegreesPerSecond : float = 60.0;
private var qTo : Quaternion;
function Start () {
qTo = goTarget.transform.localRotation;
}
function Update () {
var v3T = goTarget.transform.position - transform.position;
var v3Aim : Vector3;
v3Aim.x = 0.0;
v3Aim.y = v3T.y;
v3T.y = 0.0;
v3Aim.z = v3T.magnitude;
qTo = Quaternion.LookRotation(v3Aim, Vector3.up);
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, qTo, maxDegreesPerSecond * Time.deltaTime);
}
Turret Script:
var goTarget : GameObject;
var maxDegreesPerSecond : float = 30.0;
private var qTo : Quaternion;
function Start () {
qTo = goTarget.transform.localRotation;
}
function Update () {
var v3T = goTarget.transform.position - transform.position;
v3T.y = transform.position.y;
qTo = Quaternion.LookRotation(v3T, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, maxDegreesPerSecond * Time.deltaTime);
}
But these follow an object in the game instead of the mouse position. I've also looked and ScreenToWorldPoint, Transform.Lookat and Raycasts but It just messes up everything even worse.
Please help me, I would really appreciate it.
You didn't write this code. I wrote this code. When borrowing code, please make reference to the original question and the original author.
Sorry man, I didn't say I wrote it. It's what I came up with so far that's close to what I need. I wrote a tone of code trying to use the mouse but eventually messed everything up, that's why I posted this because it's the closest to what I needed.
Use an empty game object for the target. Put code on the target that does a raycast each frame and places the empty game object at RaycastHit.point. If the raycast fails, don't move the target (so the gun will still point at the last position).
I've tried that, but when the tank moves it just gets messed up. I've created an empty game object, attached it to the front of the cannon and used that but when the tank stars moving it gets messed up. Also I need to constraint the vertical movement somehow, and I would like the turret to rotate untill it catches up with the mouses's X. This one just rotates it while the mouse is moving, when i stop moving the mouse it slow down and never catches up with the mouse position.
Forget the turret code for a $$anonymous$$ute. Just write a script that uses Raycasting() to place a object at the mouse cursor position. The question of placing an object at the mouse cursor using Raycasting has been answered many times on Unity Answers. After you have that working, just drag and drop the object into the goTarget variable in the Inspector.
Answer by Spincu · Jul 22, 2013 at 05:03 PM
How do I limit the rotation of the gun in this script ?
//public variables
public var targetFollow : GameObject; //load the empty game object that the cannon must follow on y axis
public var maxDegreesPerSecond : float = 60.0; //rotation speed of cannon
//private variables
private var qTo : Quaternion;
//load the followed object current rotation
function Start () {
qTo = targetFollow.transform.localRotation;
}
// y axis movement to follow target
function Update () {
var v3T = targetFollow.transform.position - transform.position;
var v3Aim : Vector3;
v3Aim.x = 0.0;
v3Aim.y = v3T.y;
v3T.y = 0.0;
v3Aim.z = v3T.magnitude;
qTo = Quaternion.LookRotation(v3Aim, Vector3.up);
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, qTo, maxDegreesPerSecond * Time.deltaTime);
Your answer
Follow this Question
Related Questions
Autoaiming Turret Cannon? 3 Answers
making an object stop responding with an object at a certain distance away 1 Answer
first Gun fires not well 2 Answers
Shooter in iOS and a visible Aim line before shooting 0 Answers
Rotating turret floats off tank in a sideways circle. Cannon floats out of turret when rotating up. 0 Answers