![]() PlayerRigidbody.MoveRotation(newRotatation) Quaternion newRotatation = Quaternion.LookRotation(playerToMouse) Vector3 playerToMouse = (transform.position + turnDir) - transform.position Vector3 turnDir = new Vector3(CrossPlatformInputManager.GetAxisRaw("Mouse X"), 0f, CrossPlatformInputManager.GetAxisRaw("Mouse Y")) PlayerRigidbody.MoveRotation (newRotatation) Set the player's rotation to this new rotation. Quaternion newRotatation = Quaternion.LookRotation (playerToMouse) Create a quaternion (rotation) based on looking down the vector from the player to the mouse. Ensure the vector is entirely along the floor plane. Vector3 playerToMouse = floorHit.point - transform.position Create a vector from the player to the point on the floor the raycast from the mouse hit. If(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask)) Perform the raycast and if it hits something on the floor layer. Create a RaycastHit variable to store information about what was hit by the ray. Create a ray from the mouse cursor on screen in the direction of the camera. PlayerRigidbody.MovePosition (transform.position + movement) Move the player to it's current position plus the movement. Movement = movement.normalized * speed * ltaTime Normalise the movement vector and make it proportional to the speed per second. Set the movement vector based on the axis input. Turn the player to face the mouse cursor. ![]() Create a layer mask for the floor layer.įloat h = CrossPlatformInputManager.GetAxisRaw("Horizontal") įloat v = CrossPlatformInputManager.GetAxisRaw("Vertical") Int floorMask // A layer mask so that a ray can be cast just at gameobjects on the floor layer.įloat camRayLength = 100f // The length of the ray from the camera into the scene. Rigidbody playerRigidbody // Reference to the player's rigidbody. Vector3 movement // The vector to store the direction of the player's movement.Īnimator anim // Reference to the animator component. Public float speed = 6f // The speed that the player will move at. Public class PlayerMovement : MonoBehaviour Using UnitySampleAssets.CrossPlatformInput Hi guys i have a problem with that code: using UnityEngine
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |