Rotation of an object towards another, but only up and down
Hi,
I'm making a game where I have a tank and I need to move the gun so that it points at the target.
I already have the turret rotation movement figured out, but I need to figure out the gun movement. But only up and down. The sideways movement is what the turret does. Unfortunately, I found that LookAt
needs at least two axes, otherwise the up and down direction only works in 90 degrees.
How can this problem be solved so that the cannon only rotates up/down and completely ignores left and right movement?
I've been drowning in the internet for days trying to find a solution to the problem, but to no avail. Thanks for the help.
using UnityEngine;
public class gunRotate : MonoBehaviour
{
public GameObject target;
void Start()
{
target = GameObject.Find("MousePosition");
}
void Update()
{
Vector3 targetPosition = new Vector3(
target.transform.position.x,
transform.position.y,
target.transform.position.z);
transform.LookAt(targetPosition);
}
}
Answer by andrew-lukasik · Apr 07 at 09:21 PM
TurretController.cs
using UnityEngine;
public class TurretController : MonoBehaviour
{
[SerializeField] Transform _chasis = null;
[SerializeField] Transform _turret = null;
[SerializeField] Transform _gun = null;
[SerializeField] Transform _target = null;
[SerializeField] float _turretDegreesPerSecond = 45;
[SerializeField] float _gunDegreesPerSecond = 10;
[SerializeField] Vector2 _gunRotationRangeX = new Vector2( -20 , 10 );
void Update ()
{
Vector3 targetPos = _target.position;
// turret control:
Vector3 targetPosTurret = new Vector3( targetPos.x , _turret.position.y , targetPos.z );
Quaternion turretRotationFinal = Quaternion.LookRotation(
forward: targetPosTurret - _turret.position ,
upwards: _chasis.up
);
float turretDegreesToFinal = Quaternion.Angle( _turret.rotation , turretRotationFinal );
_turret.rotation = Quaternion.Lerp(
_turret.rotation , turretRotationFinal ,
( _turretDegreesPerSecond / turretDegreesToFinal ) * Time.deltaTime
);
// gun control:
Vector3 targetPosGun = _turret.InverseTransformPoint( targetPos );
float gunAngleFinal = -Mathf.Atan2( targetPosGun.y , targetPosGun.z ) * Mathf.Rad2Deg;
float gunAngleLimited = Mathf.Clamp( gunAngleFinal , _gunRotationRangeX.x , _gunRotationRangeX.y );
_gun.localRotation = Quaternion.Lerp(
_gun.localRotation ,
Quaternion.Euler( gunAngleLimited , 0 , 0 ) ,
Mathf.Abs( _gunDegreesPerSecond / gunAngleLimited ) * Time.deltaTime
);
}
}
Transform hierarchy:
+ chasis
| chasis mesh
| + turret
| | turret mesh
| | + gun
| | | gun mesh
Thanks a lot, it works. I would like to ask, how can I limit the rotation so that the gun doesnt rotate more than 10 degrees down and 20 degrees up?
Thank you
I updated the OP and added smooth rotation as well.
Thank you very much. It helped me a lot.
Your answer
![](https://koobas.hobune.stream/wayback/20220613055127im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Rotation issue 0 Answers
rotate an object with HTC Vive controller 0 Answers
Character Rotation 0 Answers
My rotation is not the same in inspector as it is in the scirpt 0 Answers
How to make a 2D object tilt smoothly based on mouse movement? 1 Answer