Created
May 13, 2013 04:15
-
-
Save jquave/5566118 to your computer and use it in GitHub Desktop.
This file contains 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 Player : MonoBehaviour { | |
int hp = 10; | |
public Camera mainCam; | |
public Transform cam; | |
CharacterMotor motor; | |
AudioSource sfxSource; | |
public GameObject animPart1; | |
public GameObject animPart2; | |
public GameObject animPart3; | |
Vector3 slp1; | |
Vector3 slp2; | |
Vector3 slp3; | |
Vector3 startScale; | |
double nextCanAttack = 0; | |
float attackDelay = 0.25f; // Time between attacks | |
public float walkSpeed = 2.0f; | |
public GameObject eyes; | |
Vector3 startPosition; | |
// Use this for initialization | |
void Start () { | |
motor = GetComponent<CharacterMotor>(); | |
sfxSource = GetComponent<AudioSource>(); | |
startPosition = transform.position; | |
slp1 = animPart1.transform.localPosition; | |
slp2 = animPart2.transform.localPosition; | |
slp3 = animPart3.transform.localPosition; | |
startScale = transform.localScale; | |
#if UNITY_IPHONE | |
/* | |
// For Mobile | |
Shader fallbackShader; | |
fallbackShader = Shader.Find("Mobile/Bumped Specular (1 Directional Light)"); | |
foreach(Transform t in gameObject.GetComponentInChildren<Transform>()) { | |
if(t.gameObject.renderer) { | |
t.gameObject.renderer.material.shader = fallbackShader; | |
t.gameObject.renderer.material.SetFloat("_Shininess",0.5f); | |
} | |
}*/ | |
#endif | |
} | |
void Attack() { | |
if(nextCanAttack>Time.time) return; | |
//sfxSource.Play(); | |
mainCam.transform.position += Vector3.up; | |
nextCanAttack = Time.time + attackDelay; | |
Vector3 tp = new Vector3(0,0,0); | |
if(Input.touchCount>0) tp = new Vector3(Input.touches[0].position.x, Input.touches[0].position.y, 0); | |
else tp = Input.mousePosition; | |
Ray ray = mainCam.ScreenPointToRay(tp); | |
RaycastHit hit; | |
Debug.DrawRay(ray.origin, ray.direction*100,Color.red,1000.0f); | |
if (Physics.Raycast (ray, out hit)) { | |
//sfxSource.Play(); | |
if(hit.collider) { | |
// Touched this object | |
Debug.Log("Touched " + hit.collider.gameObject.name); | |
} | |
} | |
} | |
// Update is called once per frame | |
void Update () { | |
if(!cam) return; | |
Vector3 directionVector = Input.GetAxis("Vertical") * cam.TransformDirection(Vector3.forward); | |
// Apply the direction to the CharacterMotor | |
motor.inputMoveDirection = walkSpeed*(directionVector + Input.GetAxis("Horizontal")*-cam.TransformDirection(Vector3.left)); | |
motor.inputJump = Input.GetButton("Jump"); | |
//motor.inputMoveDirection *= 5.0f*Input.GetAxis("Boost"); | |
// Walk forward on iOS | |
if(Input.touchCount>0) | |
motor.inputMoveDirection = walkSpeed*cam.TransformDirection(Vector3.forward); | |
// Rotate to look in the direction we're going | |
transform.LookAt(transform.position + motor.inputMoveDirection); | |
if(Input.GetButton("Fire1")) Attack(); | |
sfxSource.volume = motor.GetDirection().magnitude; | |
if(!sfxSource.isPlaying) | |
sfxSource.Play(); | |
if(motor.GetDirection().magnitude>0.1f) { | |
float m = 0.1f; | |
float f = (m*Mathf.Sin(Time.time*10.0f)-m/4); | |
animPart1.transform.localPosition = slp1 + f*Vector3.up; | |
animPart2.transform.localPosition = slp2 + f*Vector3.up; | |
animPart3.transform.localPosition = slp3 + f*Vector3.up; | |
transform.localScale = startScale + f/2.0f*Vector3.up; | |
transform.localScale += f/2.0f*Vector3.left; | |
//sfxSource.Play(); | |
} | |
//else | |
//sfxSource.Stop(); | |
AdhereToGroundNormals(); | |
} | |
// Turn the bot so he is at the same angle as the ground he's standing on | |
void AdhereToGroundNormals() { | |
/* | |
Ray ray = new Ray(transform.position, Vector3.down); | |
RaycastHit hit; | |
//Debug.DrawRay(ray.origin, ray.direction*100,Color.red,1000.0f); | |
if (Physics.Raycast (ray, out hit)) { | |
//sfxSource.Play(); | |
if(hit.collider) { | |
// Touched this object | |
Debug.Log("tested " + hit.collider.gameObject.name); | |
Vector3 incomingVec = hit.point - transform.position; | |
Vector3 reflectVec = Vector3.Reflect(incomingVec, hit.normal); | |
Debug.DrawRay(hit.point, reflectVec,Color.green,1000.0f); | |
// transform.LookAt(reflectVec); | |
transform.RotateAroundLocal(Vector3.left, 1); | |
} | |
}*/ | |
} | |
public float pushPower = 0.00f;//05F; | |
float pushAmt = 4.0f; | |
void OnControllerColliderHit(ControllerColliderHit hit) { | |
Rigidbody body = hit.collider.attachedRigidbody; | |
if (body == null || body.isKinematic) | |
return; | |
if (hit.moveDirection.y < -0.3F) | |
return; | |
// Lock to one direction | |
Vector3 pushDir; | |
if( Mathf.Abs(hit.moveDirection.x) > Mathf.Abs(hit.moveDirection.z) ) { | |
pushDir = new Vector3(hit.moveDirection.x, 0, 0); | |
} | |
else { | |
pushDir = new Vector3(0, 0, hit.moveDirection.z); | |
} | |
eyes.renderer.material.shader = Shader.Find("Specular"); | |
Debug.DrawLine(transform.position+Vector3.up, hit.collider.transform.position+Vector3.up,Color.red, 0.25f); | |
//float angleToDirectionToPush = ; | |
eyes.renderer.material.SetColor("_SpecColor", Color.red); | |
Vector3 pushVel = (pushDir * pushPower).normalized; | |
Vector3 vectorToBox = transform.position - hit.collider.transform.position; | |
Vector3 vectorToPush = pushVel; | |
float angleToBox = Vector3.Angle(vectorToBox, vectorToPush); | |
Debug.Log("A: "+angleToBox); | |
Debug.DrawLine(transform.position+Vector3.up, transform.position+Vector3.up+pushVel,Color.green, 0.25f); | |
// Only push if we're on the right side of the box to be pushing | |
if( Mathf.Abs(180-angleToBox)<25 ) | |
body.velocity = pushAmt * pushVel; | |
} | |
public void TakeDamage(float damage) { | |
hp -= (int)damage; | |
} | |
public void TakePushback(float pushback, Transform source) { | |
Vector3 pushDirection = (source.position - transform.position).normalized; | |
motor.inputMoveDirection = -pushback*pushDirection; | |
} | |
public void Die() { | |
transform.position = startPosition; | |
motor.SetVelocity(Vector3.zero); | |
Debug.Log("Die"); | |
} | |
void onGUI() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment