- Home /
Tank turret rotation problem
Hi everyone. I'm trying to make a tank with a rotating turret. I have this script in which I make a plane and use it to raycast the mouse position and tell the cannon to LookAt that position. The problem I have is that the plane I create (via scripting) is centered in (0,0,0) I detect the raycast but when I put the tank forward/backward/left/right, the turret can only LookAt forward/backward/left/right. How could I detect the raycast as if the center of the plane were right below my tank?
Here is the C# code:
using UnityEngine;
using System.Collections;
public class TankController : MonoBehaviour
{
public Transform turret;
public int smooth;
public float movSpeed;
public float rotSpeed;
Vector3 targetPosition;
void FixedUpdate ()
{
MoveTurret();
RotateTurret();
}
void RotateTurret()
{
Plane playerPlane = new Plane(transform.up, turret.localPosition);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float hitDist = 0.0f;
if(playerPlane.Raycast(ray, out hitDist))
{
print(hitDist);
Vector3 targetPoint = ray.GetPoint(hitDist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - turret.localPosition);
turret.rotation = Quaternion.Lerp(turret.rotation, targetRotation, Time.deltaTime * smooth);
}
}
}
I hope anyone can help me with this. Thanks a lot.
I don't understand how the placement of the plane would make any kind of difference for the rotation of the turret.
Also, why don't use use a Raycast hit for the raycast?
Plane playerPlane = new Plane(transform.up, turret.localPosition);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit = new RaycastHit();
if(playerPlane.Raycast(ray, out hit))
{
Vector3 targetPoint = hit.point;
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - turret.localPosition);
turret.rotation = Quaternion.Lerp(turret.rotation, targetRotation, Time.deltaTime * smooth);
}
But Plane.Raycast's second parameter can only be a float, not as in Physics.Raycast... I tried at first this, but the problem then was that if my cursor was on a wall or anything else, the turret would look at that position (rotating its X axis). This is the reason why I tried a Plane.Raycast... Any idea? Thanks a lot for your quick response. :)
Oh, I didn't even notice taht you didn't use Physics.Raycast. Well, you shuld definitely do that. You can avoid registering raycast hits on unwanted objects (like walls) by passing a Layer$$anonymous$$ask to the raycast function which only includes those layers that you want the ray to register. $$anonymous$$ake the ground layer different from the walls and then include only that ground layer in the Layer$$anonymous$$ask.
$$anonymous$$mm... Sounds great, I have never used Layer$$anonymous$$asks but I will investigate how to use them. I'll let you know if I can get it working. :)
IT's simple. You just declare it and if it's public you can select the layers in it in the inspector from the drop-down list. You can also assign layers by script in the Start function:
public Layer$$anonymous$$ask cursorHitLayer$$anonymous$$ask;
cursorHitLayer$$anonymous$$ask|= (1 << Layer$$anonymous$$ask.NameToLayer("Ground"));
Then in the Raycast function you just include it as a parameter:
if(playerPlane.Raycast(ray, out hit, $$anonymous$$athf.Infinity, cursorHitLayer$$anonymous$$ask)) {
}
Your answer
Follow this Question
Related Questions
rotate object to face mouse cursor along global y axis 0 Answers
Mathf.Clamp is 'Sticky'? 1 Answer
Can someone help me fix my Character-Set up? 1 Answer
Player not rotating with camera 1 Answer
Simulating Mouse Input for AI 0 Answers