I’m simply trying to prevent the object from rotating past a certain limit but still maintain rotation update. However unity is not allowing me to use the code inside the if statement because it isn’t a variable. How would I be able to make sure that the rotation doesn’t exceed it’s treshold?
targetRotation = Quaternion.LookRotation(enemyTarget.transform.position - transform.position);
if(targetRotation.eulerAngles.x > 50)
{
targetRotation.eulerAngles.x = 49;
}
rb.MoveRotation(targetRotation);
Don’t read / write from .eulerAngles. Due to gimbal lock they are not reliable.
Instead:
keep your own float angle variable.
adjust that variable up/down
clip that variable to a range
set the rotation with Quaternion.Euler()
Here’s an example of using floats to drive rotations:
Turret aiming/rotating:
I just did this a few weeks ago for a gun turret system in my Jetpack Kurt Space Flight game, which is getting more combat mode targets.
This is the aiming portion (it’s a partial class):
You should call StartAimer() from Start()
You should call UpdateAiming() from Update()
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public partial class TurretBrainAndTargeter
{
// Feel free to make these public, drag them in using the Editor,
// then remember to …
1 Like