Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

basic player movement

Discussion in 'Scripting' started by jagguy, Jun 12, 2012.

  1. jagguy

    jagguy

    Joined:
    May 28, 2012
    Posts:
    54
    I can move player without FPS default controller.

    I have searched a few tutorials and have found a way to move the player without using the default FPS that come with unity. I can have a rigid body and not walk partially into static objects where the FPS couldn't do that.

    The below code works fine but is there an easier way than this?

    Code (csharp):
    1.  
    2.  
    3.  Vector3 fwd = transform.TransformDirection(Vector3.forward);
    4.      Vector3 right = transform.TransformDirection(Vector3.right);
    5.       Vector3 back = transform.TransformDirection(Vector3.back);
    6.      Vector3 left = transform.TransformDirection(Vector3.left);
    7.    
    8.        
    9.         RaycastHit hit;
    10.    
    11.        
    12.  
    13.         if (Input.GetKeyDown("w"))  
    14.         {
    15.        
    16.             direction="up";
    17.         }
    18.        
    19.             if (Input.GetKeyUp("a") || Input.GetKeyUp("w") || Input.GetKeyUp("s")  || Input.GetKeyUp("d") )  
    20.         {
    21.             direction="none";
    22.         }
    23.        
    24.        
    25.        
    26.            
    27.        
    28.        
    29.         if(direction=="up")
    30.         {
    31.             if(Physics.Raycast(transform.position, fwd,out hit, raylength))
    32.             {
    33.                if (hit.collider.gameObject.name=="Wall" || hit.collider.gameObject.name=="Enemy")
    34.                 {
    35.                  Debug.Log("Hit Floor2" + hit.collider.gameObject.name);
    36.                  //transform.Translate(Vector3.back * Time.deltaTime* 5.0f);
    37.                  direction="none";
    38.                 }//if
    39.             }//if  
    40.             else
    41.                 {
    42.                     transform.Translate(Vector3.forward * Time.deltaTime* walk);
    43.            
    44.                 } //if
    45.            
    46.            
    47.         }//if
    48.    
    49.  
    50.  
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    I wrote this a while back while playing with the idea. It is a functional Physics Character controller.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PhysicsCharacter : MonoBehaviour {
    6.     // generic controls
    7.     float speed = 8;
    8.     float jumpSpeed = 12;
    9.    
    10.     // character controller
    11.     CharacterController controller;
    12.     public bool grounded = false;
    13.    
    14.     // camera control
    15.     float x=0;
    16.     float y=0;
    17.    
    18.     // Use this for initialization
    19.     void Start () {
    20.         if(!rigidbody)
    21.             gameObject.AddComponent<Rigidbody>();
    22.        
    23.         rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
    24.         rigidbody.useGravity = false;
    25.        
    26.         Vector3 angles = Camera.main.transform.eulerAngles;
    27.         x = angles.y;
    28.         y = angles.x;
    29.     }
    30.    
    31.     // Update is called once per frame
    32.     void Update () {
    33.         Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical"));
    34.         input = transform.TransformDirection(input);
    35.         if(input.magnitude > 1) input.Normalize();
    36.         float gravity = Physics.gravity.y * 3;
    37.         Vector3 move = rigidbody.velocity;
    38.        
    39.         Vector3 p1 = transform.position + Vector3.down * 0.5f;
    40.         float r = 0.45f;
    41.         RaycastHit hit;
    42.         grounded = false;
    43.         if(Physics.CapsuleCast(p1, p1, r, Vector3.down, out hit, 0.1f)){
    44.             if(Vector3.Dot(hit.normal, Vector3.up) > 0.5f) grounded = true;
    45.         }
    46.        
    47.         if(grounded){
    48.             move = input * speed;
    49.             move.y = gravity * Time.deltaTime;
    50.            
    51.             if(Input.GetButtonDown("Jump")){
    52.                 move.y = jumpSpeed;
    53.                 grounded = false;
    54.             }
    55.         } else {
    56.             move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed);
    57.             move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
    58.             move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed);
    59.         }
    60.        
    61.         transform.rotation = Camera.main.transform.rotation;
    62.         Vector3 euler = transform.eulerAngles;
    63.         euler.x = 0;
    64.         transform.eulerAngles = euler;
    65.        
    66.         rigidbody.velocity = move;// * Time.deltaTime
    67.        
    68.         //CollisionFlags flags = controller.Move(move * Time.deltaTime);
    69.         //grounded = (flags  CollisionFlags.Below) > 0;
    70.        
    71.        
    72.        
    73.        
    74.     }
    75.    
    76.     void LateUpdate(){
    77.         // quick mouse orbit
    78.         x += Input.GetAxis("Mouse X") * 5;
    79.         y -= Input.GetAxis("Mouse Y") * 5;
    80.        
    81.         if(y > 180) y -= 360;
    82.         if(y < -180) y += 360;
    83.         y = Mathf.Clamp(y, -60,60);
    84.        
    85.         Camera.main.transform.rotation = Quaternion.Euler(y,x,0);
    86.         Camera.main.transform.position = Camera.main.transform.rotation * new Vector3(0, 0, -3) + transform.position;
    87.     }
    88. }
    89.  
    It also controls the camera as well.
     
  3. Nirav-Madhani

    Nirav-Madhani

    Joined:
    Jan 8, 2014
    Posts:
    27
    its depreciated . here is new version

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PhysicsCharacter : MonoBehaviour {
    6.    // generic controls
    7.    float speed = 8;
    8.    float jumpSpeed = 12;
    9.    
    10.    // character controller
    11.    CharacterController controller;
    12.    public bool grounded = false;
    13.    
    14.    // camera control
    15.    float x=0;
    16.    float y=0;
    17.    
    18.    // Use this for initialization
    19.    void Start () {
    20.      if(!GetComponent<Rigidbody>())
    21.        gameObject.AddComponent<Rigidbody>();
    22.      
    23.      GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation;
    24.      GetComponent<Rigidbody>().useGravity = false;
    25.      
    26.      Vector3 angles = Camera.main.transform.eulerAngles;
    27.      x = angles.y;
    28.      y = angles.x;
    29.    }
    30.    
    31.    // Update is called once per frame
    32.    void Update () {
    33.      Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical"));
    34.      input = transform.TransformDirection(input);
    35.      if(input.magnitude > 1) input.Normalize();
    36.      float gravity = Physics.gravity.y * 3;
    37.      Vector3 move = GetComponent<Rigidbody>().velocity;
    38.      
    39.      Vector3 p1 = transform.position + Vector3.down * 0.5f;
    40.      float r = 0.45f;
    41.      RaycastHit hit;
    42.      grounded = false;
    43.      if(Physics.CapsuleCast(p1, p1, r, Vector3.down, out hit, 0.1f)){
    44.        if(Vector3.Dot(hit.normal, Vector3.up) > 0.5f) grounded = true;
    45.      }
    46.      
    47.      if(grounded){
    48.        move = input * speed;
    49.        move.y = gravity * Time.deltaTime;
    50.        
    51.        if(Input.GetButtonDown("Jump")){
    52.          move.y = jumpSpeed;
    53.          grounded = false;
    54.        }
    55.      } else {
    56.        move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed);
    57.        move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed);
    58.        move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed);
    59.      }
    60.      
    61.      transform.rotation = Camera.main.transform.rotation;
    62.      Vector3 euler = transform.eulerAngles;
    63.      euler.x = 0;
    64.      transform.eulerAngles = euler;
    65.      
    66.      GetComponent<Rigidbody>().velocity = move;// * Time.deltaTime
    67.      
    68.      //CollisionFlags flags = controller.Move(move * Time.deltaTime);
    69.      //grounded = (flags  CollisionFlags.Below) > 0;
    70.      
    71.      
    72.      
    73.      
    74.    }
    75.    
    76.    void LateUpdate(){
    77.      // quick mouse orbit
    78.      x += Input.GetAxis("Mouse X") * 5;
    79.      y -= Input.GetAxis("Mouse Y") * 5;
    80.      
    81.      if(y > 180) y -= 360;
    82.      if(y < -180) y += 360;
    83.      y = Mathf.Clamp(y, -60,60);
    84.      
    85.      Camera.main.transform.rotation = Quaternion.Euler(y,x,0);
    86.      Camera.main.transform.position = Camera.main.transform.rotation * new Vector3(0, 0, -3) + transform.position;
    87.    }
    88. }
    89.  
     
  4. ubaidraza

    ubaidraza

    Joined:
    Feb 13, 2017
    Posts:
    1
    Hey Your code working but there is still one issue when i play game my player goes into the air.
    what's the solution for it i'm waiting for your replay
    please i need it