Question by
JAIDYZ · May 24, 2020 at 02:56 AM ·
rotationmovementscripting beginnerscriptingbasicsclamped rotation
How do i use the MathF.Clamp Function correctly?
So i have this script for rotation my turret in my android game and it works fine on the touchscreen of my android device. I am using the standard assets cross platform scripts attached to my UI buttons and everything works but i want to know how can i and an and statement to my current script so i can make it cross platform and use the Input.GetAxis as well as the cross platform axis?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.CrossPlatformInput;
public class Aim : MonoBehaviour {
public float minClamp = -90f;
public float maxClamp = -43f;
public float speed = 5.0f;
private float zRotation = -90f;
void Update()
{
zRotation = Mathf.Clamp(zRotation - CrossPlatformInputManager.GetAxis("Horizontal") * speed, minClamp, maxClamp);
transform.eulerAngles = new Vector3(zRotation, -90f, 0);
}
}
Comment
Answer by N-8-D-e-v · Jun 08, 2020 at 06:37 PM
Say you want to clamp the x position of your gameobject, you would write it like this
void Update()
{
SampleFloatX = Mathf.Clamp(SampleFloatX, //minvalue, //maxvalue);
}