Quantcast
Channel: Latest Questions by f_rodrigues
Viewing all articles
Browse latest Browse all 18

Contrains to position and rotation

$
0
0
I'm tryying to limiting the distance of two object to a min of 4 and a max of 60 and also the rotation.x of the first object to 40(in Eulers). But it doesn't seem to work. The position works just fine, but the rotation get's stuck and can't move back. EDIT: The setup is a camera and a cube, the script is in the camera. The target var is the cube. Here's the full code. public class look_at : MonoBehaviour { public GameObject target; public float moveSpeed; public bool cantMove; // Update is called once per frame void Update () { GetKeys (); transform.LookAt (target.transform); } void GetKeys() { Vector3 previousPos = transform.position; Quaternion previousRot = transform.rotation; cantMove = false; if (Input.GetKey (KeyCode.W)) { transform.Translate (Vector3.forward * moveSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.S)) { transform.Translate (Vector3.back * moveSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.D)) { transform.Translate (Vector3.right * moveSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.A)) { transform.Translate (Vector3.left * moveSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.UpArrow)) { transform.Translate (Vector3.up * moveSpeed * Time.deltaTime); } if (Input.GetKey (KeyCode.DownArrow)) { transform.Translate (Vector3.down * moveSpeed * Time.deltaTime); } if (Input.GetKeyDown (KeyCode.LeftShift)) { moveSpeed *= 2; } if (Input.GetKeyUp (KeyCode.LeftShift)) { moveSpeed /= 2; } cantMove = checkConstrains(); if (cantMove) { transform.position = previousPos; transform.rotation = previousRot; } } bool checkConstrains() { bool ret = false; Vector3 offsetPos = target.transform.position - transform.position; float sqrLen = offsetPos.sqrMagnitude; if(sqrLen < 4 || sqrLen > 60 || Mathf.Abs(transform.eulerAngles.x) > 40 || Mathf.Abs(transform.eulerAngles.x) < 0){ ret = true; } return ret; } }

Viewing all articles
Browse latest Browse all 18

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>