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): Vector3 fwd = transform.TransformDirection(Vector3.forward); Vector3 right = transform.TransformDirection(Vector3.right); Vector3 back = transform.TransformDirection(Vector3.back); Vector3 left = transform.TransformDirection(Vector3.left); RaycastHit hit; if (Input.GetKeyDown("w")) { direction="up"; } if (Input.GetKeyUp("a") || Input.GetKeyUp("w") || Input.GetKeyUp("s") || Input.GetKeyUp("d") ) { direction="none"; } if(direction=="up") { if(Physics.Raycast(transform.position, fwd,out hit, raylength)) { if (hit.collider.gameObject.name=="Wall" || hit.collider.gameObject.name=="Enemy") { Debug.Log("Hit Floor2" + hit.collider.gameObject.name); //transform.Translate(Vector3.back * Time.deltaTime* 5.0f); direction="none"; }//if }//if else { transform.Translate(Vector3.forward * Time.deltaTime* walk); } //if }//if
I wrote this a while back while playing with the idea. It is a functional Physics Character controller. Code (csharp): using UnityEngine; using System.Collections; public class PhysicsCharacter : MonoBehaviour { // generic controls float speed = 8; float jumpSpeed = 12; // character controller CharacterController controller; public bool grounded = false; // camera control float x=0; float y=0; // Use this for initialization void Start () { if(!rigidbody) gameObject.AddComponent<Rigidbody>(); rigidbody.constraints = RigidbodyConstraints.FreezeRotation; rigidbody.useGravity = false; Vector3 angles = Camera.main.transform.eulerAngles; x = angles.y; y = angles.x; } // Update is called once per frame void Update () { Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical")); input = transform.TransformDirection(input); if(input.magnitude > 1) input.Normalize(); float gravity = Physics.gravity.y * 3; Vector3 move = rigidbody.velocity; Vector3 p1 = transform.position + Vector3.down * 0.5f; float r = 0.45f; RaycastHit hit; grounded = false; if(Physics.CapsuleCast(p1, p1, r, Vector3.down, out hit, 0.1f)){ if(Vector3.Dot(hit.normal, Vector3.up) > 0.5f) grounded = true; } if(grounded){ move = input * speed; move.y = gravity * Time.deltaTime; if(Input.GetButtonDown("Jump")){ move.y = jumpSpeed; grounded = false; } } else { move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed); move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed); move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed); } transform.rotation = Camera.main.transform.rotation; Vector3 euler = transform.eulerAngles; euler.x = 0; transform.eulerAngles = euler; rigidbody.velocity = move;// * Time.deltaTime //CollisionFlags flags = controller.Move(move * Time.deltaTime); //grounded = (flags CollisionFlags.Below) > 0; } void LateUpdate(){ // quick mouse orbit x += Input.GetAxis("Mouse X") * 5; y -= Input.GetAxis("Mouse Y") * 5; if(y > 180) y -= 360; if(y < -180) y += 360; y = Mathf.Clamp(y, -60,60); Camera.main.transform.rotation = Quaternion.Euler(y,x,0); Camera.main.transform.position = Camera.main.transform.rotation * new Vector3(0, 0, -3) + transform.position; } } It also controls the camera as well.
its depreciated . here is new version Code (csharp): using UnityEngine; using System.Collections; public class PhysicsCharacter : MonoBehaviour { // generic controls float speed = 8; float jumpSpeed = 12; // character controller CharacterController controller; public bool grounded = false; // camera control float x=0; float y=0; // Use this for initialization void Start () { if(!GetComponent<Rigidbody>()) gameObject.AddComponent<Rigidbody>(); GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotation; GetComponent<Rigidbody>().useGravity = false; Vector3 angles = Camera.main.transform.eulerAngles; x = angles.y; y = angles.x; } // Update is called once per frame void Update () { Vector3 input = new Vector3(Input.GetAxis ("Horizontal"),0,Input.GetAxis("Vertical")); input = transform.TransformDirection(input); if(input.magnitude > 1) input.Normalize(); float gravity = Physics.gravity.y * 3; Vector3 move = GetComponent<Rigidbody>().velocity; Vector3 p1 = transform.position + Vector3.down * 0.5f; float r = 0.45f; RaycastHit hit; grounded = false; if(Physics.CapsuleCast(p1, p1, r, Vector3.down, out hit, 0.1f)){ if(Vector3.Dot(hit.normal, Vector3.up) > 0.5f) grounded = true; } if(grounded){ move = input * speed; move.y = gravity * Time.deltaTime; if(Input.GetButtonDown("Jump")){ move.y = jumpSpeed; grounded = false; } } else { move.x = Mathf.Clamp(move.x + input.x * 0.01f * speed, -speed, speed); move.y = Mathf.Clamp(move.y + gravity * Time.deltaTime, gravity, jumpSpeed); move.z = Mathf.Clamp(move.z + input.z * 0.01f * speed, -speed, speed); } transform.rotation = Camera.main.transform.rotation; Vector3 euler = transform.eulerAngles; euler.x = 0; transform.eulerAngles = euler; GetComponent<Rigidbody>().velocity = move;// * Time.deltaTime //CollisionFlags flags = controller.Move(move * Time.deltaTime); //grounded = (flags CollisionFlags.Below) > 0; } void LateUpdate(){ // quick mouse orbit x += Input.GetAxis("Mouse X") * 5; y -= Input.GetAxis("Mouse Y") * 5; if(y > 180) y -= 360; if(y < -180) y += 360; y = Mathf.Clamp(y, -60,60); Camera.main.transform.rotation = Quaternion.Euler(y,x,0); Camera.main.transform.position = Camera.main.transform.rotation * new Vector3(0, 0, -3) + transform.position; } }
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