Advertisement
obernardovieira

[Unity3D] Basic player movement

Oct 29th, 2013
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma strict
  2.  
  3. private var rotacao : float;
  4. private var distancia : float;
  5. private var xpos : float;
  6. private var zpos : float;
  7. public var player : GameObject;
  8. public var xcam : Camera;
  9. public var terreno : GameObject;
  10. private var grounded : boolean = false;
  11.  
  12. @script RequireComponent(Rigidbody, CapsuleCollider)
  13.  
  14. function Start() {
  15.     distancia = 0.1;
  16.     player.rigidbody.freezeRotation = true;
  17.     player.rigidbody.useGravity = true;
  18. }
  19. function Update() {
  20.     if(Input.GetKey(KeyCode.W)) {
  21.         rotacao = xcam.transform.eulerAngles.y;
  22.         xpos = (distancia * Mathf.Sin(rotacao*(Mathf.PI/180))) / 2.0;
  23.         zpos = (distancia * Mathf.Cos(rotacao*(Mathf.PI/180))) / 2.0;
  24.         player.transform.Translate(xpos,0,zpos);
  25.     }
  26.     else if(Input.GetKey(KeyCode.S)) {
  27.         rotacao = xcam.transform.eulerAngles.y + 180.0;
  28.         xpos = (distancia * Mathf.Sin(rotacao*(Mathf.PI/180))) / 2.0;
  29.         zpos = (distancia * Mathf.Cos(rotacao*(Mathf.PI/180))) / 2.0;
  30.         player.transform.Translate(xpos,0,zpos);
  31.     }
  32.     else if(Input.GetKey(KeyCode.A)) {
  33.         rotacao = xcam.transform.eulerAngles.y - 90.0;
  34.         xpos = (distancia * Mathf.Sin(rotacao*(Mathf.PI/180))) / 2.0;
  35.         zpos = (distancia * Mathf.Cos(rotacao*(Mathf.PI/180))) / 2.0;
  36.         player.transform.Translate(xpos,0,zpos);
  37.     }
  38.     else if(Input.GetKey(KeyCode.D)) {
  39.         rotacao = xcam.transform.eulerAngles.y + 90.0;
  40.         xpos = (distancia * Mathf.Sin(rotacao*(Mathf.PI/180))) / 2.0;
  41.         zpos = (distancia * Mathf.Cos(rotacao*(Mathf.PI/180))) / 2.0;
  42.         player.transform.Translate(xpos,0,zpos);
  43.     }
  44.     else if(Input.GetKey(KeyCode.Space)) {
  45.         if(grounded == true) {
  46.             player.transform.Translate(0.0,1.0,0.0);
  47.             grounded = false;
  48.         }
  49.     }
  50. }
  51. function OnCollisionEnter (hit : Collision) {
  52.     if(hit.gameObject.name == terreno.name) {
  53.         grounded = true;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement