- Home /
Question by
captainkrisu · Dec 16, 2015 at 09:57 PM ·
axisrotation axisturrettank
Tank turret 1 axis, but keeping turret rotation for parent.
I've been hours trying to make a turret rotate in 1 axis, but still keeping the rotation with parent. I kinda works, but when I rotate the parent object, the turret does not rotate where it should be.
void Update()
{
RaycastHit hit;
Vector3 fwd = MainCamera.transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(MainCamera.transform.position, fwd, out hit, 5000))
{
if (hit.transform.gameObject.tag == "Player")
{
}
else
{
Vector3 relativePos = hit.point - TurretAxis.transform.position;
relativePos.y = 0;
Quaternion rotation = Quaternion.LookRotation(relativePos);
TurretAxis.transform.rotation = Quaternion.Lerp(TurretAxis.transform.localRotation, rotation, Time.deltaTime * 50);
}
}
}
Comment
I got it somewhat working
Quaternion targetPos = Quaternion.LookRotation(hit.point - transform.position);
targetPos.z = 0;
targetPos.x = 0;
TurretAxis.transform.rotation = Quaternion.Slerp(TurretAxis.transform.rotation, targetPos, Time.deltaTime * 15);
Now the problem is that the TurretAxis is always rotating flat, not with the parent.
Also, trying localRotation had no success, It was fine before I moved the tank, after I rotated the tank right, so did the turret, in a way that it would not point where mouse was.
Your answer
Follow this Question
Related Questions
Tank Turret move on XAxis by Mouse XAxis 1 Answer
Rotating Tank Turret with correct angle 3 Answers
Turret Rotation on a moving object with rotation limits 3 Answers
Lock rotation of object 4 Answers
Fixing imported model axis. 1 Answer