Hi guys, Im creating a 2D shooter, I have the main things down first, but with my 2d aming script I dont want my players arm to go over his head. I want to limit the arm rotation based on two variables, "maxUP" and "maxDown". How can I do this? Code (csharp): #pragma strict private var flipRight = false; private var flipLeft = false; var mainCamera : Camera; public var distance = 10.0; var PlayerSprite : Transform; var maxUP : float = 100; var maxDown : float = -96; function Update () { if(flipRight == true flipLeft == false){ transform.localScale.x = 1; transform.localScale.y = 1; var pos = mainCamera.WorldToScreenPoint(transform.position); var dir = Input.mousePosition - pos; var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); transform.rotation.z = Mathf.Clamp (transform.rotation.z, maxDown, maxUP); } if(flipRight == false flipLeft == true){ //transform.localScale.x = -1; transform.localScale.y = -1; var pos2 = mainCamera.WorldToScreenPoint(transform.position); var dir2 = Input.mousePosition - pos2; var angle2 = Mathf.Atan2(dir2.y, dir2.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.AngleAxis(-angle2, Vector3.forward); } } function PlayerFlipRight () { flipRight = true; flipLeft = false; } function PlayerFlipLeft () { flipRight = false; flipLeft = true; } Please reply ASAP, im really getting frustrated trying to find a good answer but most dont help me.
Just cap the angle. some psuedocode below. This could be optimized better if you know a bit more about Quaternions but it'll do. Code (csharp): //do your angle calculations if(transform.rotation.eulerangles.x > 100) { Quaternion newRot = Quaternion.euler(100,transform.rotation.eulerangles.y,transform.rotation.eulerangles.z); transform.rotation = newRot; }
glacius3000, psuedocode wont help me, im not an expert in quaternations but sir i need someone who actually help, by that i mean one who can actually show me an example code or just add a few lines of code to my code.
What is difficult on that piece of code? It just says, if the rotation is over 100(°?) in the x-direction, it sets it to 100. Nothing too fancy about it. The only noticable thing is: you can't set just a specific amount of rotation or scale to an Object. you always have to built up a complete Quarternion/Vector3.