Created
October 4, 2013 17:48
-
-
Save jquave/6829836 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class HumanPlayer : MonoBehaviour { | |
| // Where the actor wants to be | |
| Vector3 finalTargetPosition; | |
| // Where the actor needs to go first in order to get to the final target | |
| Vector3 targetPosition; | |
| float walkForceMag; | |
| float maxVel = 5.0f; | |
| public float cameraHeight = 5.0f; | |
| // Use this for initialization | |
| void Start () { | |
| targetPosition = transform.position; | |
| walkForceMag = constantForce.force.magnitude; | |
| } | |
| // Update is called once per frame | |
| void FixedUpdate () { | |
| if(Input.GetMouseButton(0)) { | |
| float dist = 5.0f; | |
| Ray r = Camera.mainCamera.ScreenPointToRay(Input.mousePosition); | |
| Vector3 point = r.origin + (r.direction * dist); | |
| RaycastHit hitInfo; | |
| if(Physics.Raycast(Camera.mainCamera.transform.position, r.direction, out hitInfo)) { | |
| targetPosition = hitInfo.point; | |
| } | |
| } | |
| // Rotate to look in the direction we're going | |
| Vector3 tmpRot = transform.localRotation.eulerAngles; | |
| transform.LookAt(targetPosition); | |
| // Don't rotate along any axis other than y for this | |
| transform.localEulerAngles = new Vector3(tmpRot.x, transform.localEulerAngles.y, tmpRot.z); | |
| // Move attached force to be in the direction we're facing | |
| Vector3 moveDir = targetPosition - transform.position; | |
| if(moveDir.magnitude>1.0f) { | |
| // Make this x/z only, no y movement, derp. | |
| moveDir.y = 0; | |
| moveDir.Normalize(); | |
| constantForce.force = walkForceMag*moveDir; | |
| } | |
| else { | |
| constantForce.force = Vector3.zero; | |
| rigidbody.velocity *= 0.9f; | |
| } | |
| float vmag = rigidbody.velocity.magnitude; | |
| if(vmag>maxVel) { | |
| // Limit the velocity magnitude to maxVel | |
| float ratio = maxVel/vmag; | |
| // Multiply by the ratio to cause magnitude to be equal to maxVel | |
| rigidbody.velocity *= ratio; | |
| } | |
| Camera.main.transform.position = new Vector3(transform.position.x, transform.position.y + cameraHeight, transform.position.z); | |
| } | |
| public void Respawn(Vector3 p) { | |
| rigidbody.velocity = Vector3.zero; | |
| transform.position = p; | |
| targetPosition = transform.position; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment