This week in class we were tasked to create a small ragdoll game. Me and my teammate just decided to create a simple tool which would allow you to hit the ragdoll with an amount of force. It was quite basic but fit the criteria for the class.
The "game" had 2 different values you could change. Gravity and Physics Tick Time. Changing them did a few things but overall not a lot. This small project was just to mess around with ragdolls.
One thing that was pretty cool, was the camera script. The camera is able to zoom in and out as well as orbit around the ragdoll.
void Zoom () { cameraDist += Input.GetAxis("Mouse ScrollWheel") * -zoomSpeed; cameraDist = Mathf.Clamp(cameraDist, minZoom, maxZoom); Camera.main.transform.localPosition = Camera.main.transform.localPosition.normalized * cameraDist; }
The above code shows the zoom function. It's pretty simple, as it gets the axis of the scroll wheel which is between -1 and 1, which can then be scaled to change the camera's local position.
void Orbit () { if(Input.GetMouseButtonDown(1)){ mousePosLastFrame = Input.mousePosition; } if(Input.GetMouseButton(1)){ Vector3 mouseDir = Input.mousePosition - mousePosLastFrame; transform.eulerAngles += new Vector3(mouseDir.y, mouseDir.x, 0) * sensitivity * Time.deltaTime; mousePosLastFrame = Input.mousePosition; } }
The orbit function is a bit more complex. When the player holds down the right mouse button, they can move the mouse to orbit the camera around the ragdoll.
It does this by knowing both the mouse position of the current frame and the last frame. From this, it can find a direction and then apply that direction to the camera's euler angles.