- Home /
C# Clamp joystick rotation
Hello guys, I need a little bit of help. I got this gameobject that I'm rotating with a joystick, but I would like to limit the rotation it can do.
25-0 and 0-335 specifically, 25° before and after 0.
The problem is that I can't manage to clamp it right. I'm trying to use Mathf.clamp, but Euler angles don't take negative angles and between Quaternion etc, I'm lost... Here is my code for the rotation. I'd be thankful if anyone could help.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Snakemove : MonoBehaviour {
public float rotSpeed = 50.0f;
public float curRotYSpeed = 0.0f;
public float curRotXSpeed = 0.0f;
public float reverseByTime;
public void ChenilleFollow(){
curRotYSpeed = Mathf.Lerp (curRotYSpeed, rotSpeed * Input.GetAxis("StickD-Y-Axis"), reverseByTime * Time.deltaTime);
curRotXSpeed = Mathf.Lerp (curRotXSpeed, rotSpeed * Input.GetAxis("StickD-X-Axis"), reverseByTime * Time.deltaTime);
//Rotation Y
transform.Rotate(transform.InverseTransformDirection(transform.right), -curRotYSpeed * reverseByTime * Time.deltaTime);
//transform.Rotate(transform.InverseTransformDirection(transform.right), curRotYSpeed * reverseByTime * Time.deltaTime);
//RotationX
transform.Rotate(transform.InverseTransformDirection(transform.up), curRotXSpeed * reverseByTime * Time.deltaTime);
}
}
Comment