Answer the question
In order to leave comments, you need to log in
How to make the Turret Cannon rotate (Transform) correctly with the Beam?
Cheerful time of the day, I'm again disturbing the Toaster forum with my tricky questions))) The question is how to rotate the gun along the y axis using a beam from the center of the camera and moving the mouse (To raise and lower the gun)... I already did a similar method for the turret and it works perfect...
Here it is://Движение турельки
Quaternion qTurret = Quaternion.LookRotation(planePoint - turretPosition,turretUpTransform);
turretTransform.rotation = Quaternion.RotateTowards(turretTransform.rotation, qTurret,30.0f * Time.deltaTime);
I did a similar method, but the gun turned over 90 and you can’t let go or raise the gun...
Here is a piece with the movement of the gun://Движение пушки
Vector3 v = new Vector3(0,distanceToPlane,(planePoint - turretPosition).magnitude);
Quaternion qGun = Quaternion.LookRotation(v);
if(Quaternion.Angle(Quaternion.identity, qGun) <= 30.0f)
gunTransform.localRotation = Quaternion.RotateTowards(gunTransform.localRotation,qGun,30.0f * Time.deltaTime);
I had to be smart and I made it simpler - MouseLookY... Everything would be fine, but due to the fact that in the "Imaginary" Ship there will be from 2 to 4 Controlled guns, the accuracy suffers...
Here is the MouseLook script:using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[AddComponentMenu("Camera-Control/Smooth Mouse Look")]
public class GunRotation : MonoBehaviour
{
public float sensitivityY = 15F;
public float minimumY = -30F;
public float maximumY = 30F;
float rotationY = 0F;
private List<float> rotArrayY = new List<float>();
float rotAverageY = 0F;
public float frameCounter = 20;
Quaternion originalRotation;
void Update()
{
rotAverageY = 0f;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotArrayY.Add(rotationY);
if (rotArrayY.Count >= frameCounter)
{
rotArrayY.RemoveAt(0);
}
for (int j = 0; j < rotArrayY.Count; j++)
{
rotAverageY += rotArrayY[j];
}
rotAverageY /= rotArrayY.Count;
rotAverageY = ClampAngle(rotAverageY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis(rotAverageY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
if (rb)
rb.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle(float angle, float min, float max)
{
angle = angle % 360;
if ((angle >= -360F) && (angle <= 360F))
{
if (angle < -360F)
{
angle += 360F;
}
if (angle > 360F)
{
angle -= 360F;
}
}
return Mathf.Clamp(angle, min, max);
}
}
Screenshot of what is happening on the stage and in the game:
If there are any options or materials about this, I will be glad)))
Thank you in advance for your support and help)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question