MyblogAd

Thursday, June 30, 2016

unity3D code for player movement

**************player physic code***********
using UnityEngine;
using System.Collections;
[RequireComponent ( typeof(BoxCollider))]
public class PlayerPhysic : MonoBehaviour {
    public LayerMask collisionMask;
    private BoxCollider collider;
    private Vector3 s;
    private Vector3 c;
    private float skin =.005f;
    [HideInInspector]
    public bool grounded;
    [HideInInspector]
    public bool movementStopped;

    Ray ray;
    RaycastHit hit;
    void start(){
        collider = GetComponent<BoxCollider>();
        s = collider.size;
        c = collider.center;
    }

    public void Move(Vector2 moveAmount){
        float deltaY = moveAmount.y;
        float deltaX = moveAmount.x;
        Vector2 p = transform.position;
        //check collision above and below
        grounded = false;
        for (int i=0; i<3; i++) {
            float dir = Mathf.Sign(deltaY);
            float x = (p.x + c.x - s.x/2)+ s.x/2 *i;
            float y = p.y + c.y + s.y/2 * dir;
            ray = new Ray(new Vector2(x,y), new Vector2(0,dir));
            Debug.DrawRay(ray.origin,ray.direction);
            if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaY)+ skin, collisionMask)){
                //get distance between player and the ground
                float dst = Vector3.Distance(ray.origin,hit.point);
                //stop players downwords movement
                if(dst > skin){
                    deltaY = -dst - skin * dir;
                }
                else{
                    deltaY=0;
                }
                grounded= true;
                break;
            }


        }
        // check left and right collision
        movementStopped = false;
        for (int i=0; i<3; i++) {
            float dir = Mathf.Sign(deltaX);
            float x = p.x + c.x + s.x/2 * dir;
            float y = p.y + c.y - s.y/2 + s.y/2 * i;
            ray = new Ray(new Vector2(x,y), new Vector2(dir,0));
            Debug.DrawRay(ray.origin,ray.direction);
            if (Physics.Raycast(ray,out hit,Mathf.Abs(deltaX)+ skin, collisionMask)){
                //get distance between player and the ground
                float dst = Vector3.Distance(ray.origin,hit.point);
                //stop players downwords movement
                if(dst > skin){
                    deltaX = -dst - skin * dir;
                }
                else{
                    deltaX=0;
                }
                movementStopped = true;
                break;
            }
          
          
        }
        if (!grounded && !movementStopped) {
            Vector2 playerDir = new Vector3 (deltaX, deltaY);
            Vector3 o = new Vector3 (p.x + c.x + s.x / 2 * Mathf.Sign (deltaX), p.y + c.y + s.y / 2 * Mathf.Sign (deltaY));
            ray = new Ray (o, playerDir.normalized);
            if (Physics.Raycast (ray, Mathf.Sqrt (deltaX * deltaX + deltaY * deltaY), collisionMask)) {
                grounded = true;
                deltaY = 0;      
            }
        }



        Vector2 finalTransform = new Vector2(deltaX, deltaY);

        transform.Translate (finalTransform);
  
    }

}
********player controller script********
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerPhysic))]
public class PlayerController : MonoBehaviour {

    //player handling
    public float gravity =20;
    public float speed=8;
    public float accelaration=30;
    public float jumpHeight = 15;
    private float currentSpeed;
    private float targetSpeed;
    private Vector2 amountToMove;
    private PlayerPhysic playerPhysics;

    void Start () {
        playerPhysics = GetComponent<PlayerPhysic>();
  
    }
  

    void Update () {
        if (playerPhysics.movementStopped) {

            targetSpeed = 0;
            currentSpeed = 0;
        }
        targetSpeed = Time.deltaTime * speed;
        currentSpeed = IncrementTowards (currentSpeed, targetSpeed, accelaration);
        if (playerPhysics.grounded) {
            amountToMove.y = 0;
            //jump
            //if(Input.GetKey("space"))

            if(Input.touchCount >0 && Input.GetTouch(0).phase == TouchPhase.Began)
            {
                amountToMove.y = jumpHeight;
            }
        }
        amountToMove.x = currentSpeed;
        amountToMove.y -= gravity * Time.deltaTime;
        playerPhysics.Move(amountToMove * Time.deltaTime);
    }
    private float IncrementTowards(float n, float target, float a){
    if(n == target){
     return n;
    }
    else{
        float dir = Mathf.Sign (target - n);
        n += a * Time.deltaTime * dir ;
        return (dir == Mathf.Sign (target - n ))? n: target ;
    }


}
}

No comments:

Post a Comment