Facebook
From HEWKEW, 5 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 354
  1.  
  2. public class Controller : MonoBehaviour { // Made by HEWKEW
  3.         public float speed = 4;
  4.         private CharacterController controller;
  5.  
  6.         private void Start() {
  7.                 //We need a controller. This will let us access to very useful functions.
  8.                 controller = GetComponent<CharacterController>();
  9.         }
  10.  
  11.         private void Update() {
  12.                 //We start our functions and keep them running.
  13.                 Move();
  14.                 Rotate();
  15.         }
  16.  
  17.         private void Move()
  18.         {
  19.                 //We need a Vector3 to storage our input horizontal for our x axis and vertical for our z axis. The y axis
  20.                 //Is not needed because we won't move our object vertically.
  21.                 Vector3 movement = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
  22.                 //Since movement has inputs with maximun 1 and minimun -1, we multiply it to our speed. For example:
  23.                 //Speed: 4 ==> Movement (1, 0, or -1) * Speed = (4, 0, -4)
  24.                 controller.SimpleMove(speed * movement);
  25.         }
  26.  
  27.         private void Rotate()
  28.         {
  29.                 //Ray from the main camera to the mouse position.
  30.                 Ray _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  31.                 //New plane (not a real plane) to get the raycast.
  32.                 Plane _plane = new Plane(Vector3.up, Vector3.zero);
  33.                 //Distance from the center of the plane to the raycast (position 0 + distance from it to the hit)
  34.                 float distance;
  35.  
  36.                 if(_plane.Raycast(_ray, out distance))
  37.                 {
  38.                         //We wont need to rotate our character to look to the ground. So we need a new Vector3 to ignore it.
  39.                         Vector3 point = new Vector3(_ray.GetPoint(distance).x, transform.position.y, _ray.GetPoint(distance).z);
  40.                         //Then, we look at that point.
  41.                         transform.LookAt(point);
  42.                 }
  43.         }
  44. }
  45.  

Replies to Movement TDS Simple Scripting rss

Title Name Language When
Re: Movement TDS Simple Scripting HEWKEW c 5 Years ago.