stillwebsite.blogg.se

Unity 3d player controller
Unity 3d player controller












unity 3d player controller

Return Mathf.Sqrt(2 * jumpHeight * gravity) for the character to reach at the apex. From the jump height and gravity we deduce the upwards speed No raycast hit, hide the aim target by moving it far away Move your cube GameObject to the point where you clicked If (surfacePlane.Raycast(ray, out enter)) Ray ray = playerCamera.ScreenPointToRay(Input.mousePosition) Create a ray from the Mouse click position SurfacePlane.SetNormalAndPosition(Vector3.up, transform.position) (transform.position + new Vector3(-offsetVector.y * 2, 0, offsetVector.x * 2)) = Vector3.Lerp(, transform.position + cameraOffset, ltaTime * 7.4f) OffsetVector = cursorPosition - playerPosOnScreen PlayerPosOnScreen = playerCamera.WorldToViewportPoint(transform.position) ĬursorPosition = playerCamera.ScreenToViewportPoint(Input.mousePosition) R.AddForce(new Vector3(0, -gravity * r.mass, 0)) We apply gravity manually for more tuning control

unity 3d player controller

R.velocity = new Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z) R.AddForce(velocit圜hange, ForceMode.Velocit圜hange) Velocit圜hange.z = Mathf.Clamp(velocit圜hange.z, -maxVelocit圜hange, maxVelocit圜hange) Velocit圜hange.x = Mathf.Clamp(velocit圜hange.x, -maxVelocit圜hange, maxVelocit圜hange) Vector3 velocit圜hange = (targetVelocity - velocity) Apply a force that attempts to reach our target velocity TargetVelocity = new Vector3(Input.GetAxis("Horizontal") * (cameraDistance >= 0 ? -1 : 1), 0, Input.GetAxis("Vertical") * (cameraDistance >= 0 ? -1 : 1)) TargetVelocity = new Vector3(Input.GetAxis("Vertical") * (cameraDistance >= 0 ? -1 : 1), 0, Input.GetAxis("Horizontal") * (cameraDistance >= 0 ? 1 : -1)) Calculate how fast we should be moving If (cameraDirection = CameraDirection.x)ĬameraOffset = new Vector3(cameraDistance, cameraHeight, 0) Įlse if (cameraDirection = CameraDirection.z)ĬameraOffset = new Vector3(0, cameraHeight, cameraDistance) TargetObject = Instantiate(targetIndicatorPrefab, Vector3.zero, Quaternion.identity) as GameObject Plane that represents imaginary floor that will be used to calculate Aim target position Public CameraDirection cameraDirection = CameraDirection.x Public class SC_TopDownController : MonoBehaviour SC_TopDownController.cs using UnityEngine

#Unity 3d player controller code#

Create a new script, name it SC_TopDownController, remove everything from it and paste the code below inside it:.Step 1: Create the Scriptsįor this tutorial, we will only need one script. To make a Top-Down Character Controller in Unity, follow the steps below. Top-Down Shooter is a type of game where the player is controlled from a top-view perspective.Įxamples of top-down shooters are Hotline Miami, Hotline Miami 2, Original Grand Theft Auto, etc. Many people are familiar with game genres such as FPS (First-Person Shooter) and RTS (Real-Time Strategy), but there are also a large number of games that do not fall into just one specific category.














Unity 3d player controller