- Home /
Question by
PlagueT · Dec 10, 2020 at 01:53 PM ·
rotationtransform.rotationmathf.clamplocalrotation
If parent rotates, the child's rotation towards mouse is disturbed.,Parent rotation disturb child rotation towards mouse
There is a plane and if the plane rotates -z or +z the current rotation gets added to the child(turret). I use transform.localRotation so the mathf.clamps work correctly but transform.localRotation does not make the turret rotate towards the mouse. If i use transform.rotation it works fine but the mathf.clamp does not work. Let's say the plane is +20 degrees rotated but the mathf.clamp does not rotate the 20 degrees and the turret aims inside the plane. Pretty crazy explanation.
Code:
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 20f;
void Start()
{
}
void Update()
{
Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float TurrRot = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
TurrRot = Mathf.Clamp(TurrRot, 25, 170);
transform.localRotation = Quaternion.Euler(0f, 0f, TurrRot);
if ( TurrRot == Mathf.Clamp(TurrRot, 30,165))
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position,
firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
I would really appreciate if someone could help me out here.
Comment
Your answer