Mathf.Clamp Negative Rotation for the 10th Million Time
Hello Guys, i know this questions gets asked a lot but i got this small c# script working so far but there is a stuttering when i want to clamp it in the negative rotation. I usually work with playmaker but want to force me scripting things that cant be done correctly or like this with so few lines of code ;)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun_Lockrotation : MonoBehaviour {
public float MaxDepression = -5f;
public float MaxElevation = 20f;
void LateUpdate()
{
transform.localEulerAngles = new Vector3 (Mathf.Clamp(transform.localEulerAngles.x, MaxDepression, MaxElevation), transform.localEulerAngles.y, transform.localEulerAngles.z);
}
}
Greetings, Jasper
Ehm, sorry i should tell for what i want to use this script; well yeah i have this tank in my garage besides some knowledge and i want the gun of that tank to have a "$$anonymous$$axDepression" and "$$anonymous$$axElevation" and have another script for the rotation laying before this script because im a beginner and dont know how to edit a script from the internet :)
maybe i combine them when i get better...
Answer by Jasperle · Jul 23, 2017 at 10:57 AM
For everyone who is interested, i got this code working with the information from another Question; link text
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Gun_Lockrotation : MonoBehaviour
{
public float MaxDepression = -20f;
public float MaxElevation = 8f;
void Update()
{
transform.localEulerAngles = new Vector3 (Mathf.Clamp((transform.localEulerAngles.x <= 180) ? transform.localEulerAngles.x : -(360 - transform.localEulerAngles.x), MaxDepression, MaxElevation), transform.localEulerAngles.y, transform.localEulerAngles.z);
}
}
This is my sample for clamping negative Rotations for something like a Tank Barrel or something :)
Hey @Jasperle thanks for the script. Just one tip of advice, LateUpdate works best for clamping floats/rotation.