stepspaster.blogg.se

Unity 3d player controller
Unity 3d player controller







  1. #UNITY 3D PLAYER CONTROLLER CODE#
  2. #UNITY 3D PLAYER CONTROLLER PLUS#

Take care! public float AmbientSpeed = 100.

#UNITY 3D PLAYER CONTROLLER CODE#

Once again, here’s all the code together. Any tips or modifications would be great in the comments. Keep in mind this is pretty basic, and you can probably go further to optimize and make it more robust.

#UNITY 3D PLAYER CONTROLLER PLUS#

If you wanted to have a boost speed, it would be (Δtime multiplyed by (ambient speed plus boost speed)).Īnd there you go. We set it to the product of our rigidbody.rotation by Vector3.forward, and set the rigidbody.velocity to AddPos multiplied by (Δtime multiplied by our ambient speed). AddPos will be used to define our velocity. Rigidbody.velocity = AddPos * (ltaTime * AmbientSpeed) What the script does is that when the player collides with the trigger, which in this case will be the teleportation pad, it switches the position of the player to the designated location which is the Teleport Target. Vector3 AddPos = Vector3.forward ĪddPos = * AddPos Drag and drop the ThirdPerson Controller prefab from Assets on to the scene. We then multiply our rigidbody.rotation by AddRot and Viola! Rotation. We take those angle values, chuck them into a Vector3, and then directly modify our lerAngles member to rotate it to those values. lerAngles = new Vector3(-pitch, yaw, -roll) This is some helpful code that I found on the Unity forums. If you’re using a rigidbody-based controller or something else, then you’ll have to modify when the code is called and the positioning to suit your needs.

unity 3d player controller

This gives us the angles at which we want to rotate. The CharacterController is also used for the positioning of the raycast and spherecast later on. Here, we’re getting our input (defined by “ Roll,” “ Pitch,” and “ Yaw” which are defined in the Input Manager in Edit->Project Settings->Input), and multiplying it by (Δtime multiplied by our rotation speed). Yaw = Input.GetAxis("Yaw") * (ltaTime * RotationSpeed) Pitch = Input.GetAxis("Pitch") * (ltaTime * RotationSpeed) roll = Input.GetAxis("Roll") * (ltaTime * RotationSpeed)

unity 3d player controller

roll, pitch and yaw are for storing our input values. Right now, with this code Gravity works, but WASD is enabled as-well. Whenever I try to only use controller.Move () my Gravity function goes away. However, Im trying to make it so the player is constantly moving, and not controllable via WASD keys on the keyboard. AddRot is our temporary variable for storing the rotation value that we’ll be rotating by. I have a script which allows you to control a player, and jump. But, since I’m using a Rigidbody, it would make sense to use this for FixedUpdate(). Why UpdateFunction() instead of using Update() or FixedUpdate()? Well, I’ve answered my own question: It could be used for either case. Now we’re getting into our UpdateFunction(). RotationSpeed is the speed at which the object turns. You can remove this or change it so the object can move backwards and such). AmbientSpeed is minimum speed of the aircraft (in this example, the aircraft is always moving along its forward axis. Now, the function for the script is pretty straight forward, but I’ll break it down bit-by-bit (full code available at the bottom).









Unity 3d player controller