八个方向踢足球
var speed = 6.0;
private var speed2 = 10;
private var speed3 = 25;
var jumpSpeed = 8.0;
var gravity = 20.0;
var qiu:GameObject;
var Male:GameObject;
var targetspeed : Vector3;
var run:boolean=false;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function Start(){
qiu=GameObject.Find("qiu");
Male=GameObject.Find("Male");
}
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
//~ moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//~ moveDirection = transform.TransformDirection(moveDirection);
//~ moveDirection *= speed;
//~ if(Input.GetKey(KeyCode.W)){
//~ transform.rotation=Quaternion.Euler(0, 0, 0);
//~ transform.Translate(Vector3.forward*5* Time.deltaTime, Space.World);
//~ }
//~ if(Input.GetKey(KeyCode.S)){
//~ transform.rotation=Quaternion.Euler(0, 180, 0);
//~ transform.Translate(Vector3.forward*-5* Time.deltaTime, Space.World);
//~ }
//~ if(Input.GetKey(KeyCode.A)){
//~ transform.rotation=Quaternion.Euler(0, 270, 0);
//~ transform.Translate(Vector3.right*-5* Time.deltaTime, Space.World);
//~ }
//~ if(Input.GetKey(KeyCode.D)){
//~ transform.rotation=Quaternion.Euler(0, 90, 0);
//~ transform.Translate(Vector3.right *5* Time.deltaTime, Space.World);
//~ }
if(Input.GetKey(KeyCode.W)){
transform.rotation=Quaternion.Euler(0, 0, 0);
transform.Translate(Vector3.forward*5* Time.deltaTime, Space.Self);
run=true;
}
if(Input.GetKey(KeyCode.S)){
transform.rotation=Quaternion.Euler(0, 180, 0);
transform.Translate(Vector3.forward*5* Time.deltaTime, Space.Self);
run=true;
}
if(Input.GetKey(KeyCode.A)){
transform.rotation=Quaternion.Euler(0, 270, 0);
transform.Translate(Vector3.forward*5* Time.deltaTime, Space.Self);
run=true;
}
if(Input.GetKey(KeyCode.D)){
transform.rotation=Quaternion.Euler(0, 90, 0);
transform.Translate(Vector3.forward*5* Time.deltaTime, Space.Self);
run=true;
}
if(Input.GetKey(KeyCode.W)&&Input.GetKey(KeyCode.D)){
transform.rotation=Quaternion.Euler(0, 45, 0);
}
if(Input.GetKey(KeyCode.D)&&Input.GetKey(KeyCode.S)){
transform.rotation=Quaternion.Euler(0, 135, 0);
}
if(Input.GetKey(KeyCode.S)&&Input.GetKey(KeyCode.A)){
transform.rotation=Quaternion.Euler(0, 225, 0);
}
if(Input.GetKey(KeyCode.A)&&Input.GetKey(KeyCode.W)){
transform.rotation=Quaternion.Euler(0, 315, 0);
}
if(Input.GetKeyUp(KeyCode.W)||Input.GetKeyUp(KeyCode.S)||Input.GetKeyUp(KeyCode.A)||Input.GetKeyUp(KeyCode.D)){
run=false;
}
if(run==false){
Male.animation.Play ();
}
if(run==true){
Male.animation.Play ("walk");
}
if(Input.GetKey(KeyCode.J)&&(Vector3.Distance(transform.position,qiu.transform.position)<2)){
targetspeed = transform.TransformDirection (Vector3.forward)*speed2;//-qiu.rigidbody.velocity;
print(targetspeed);
qiu.rigidbody.velocity=qiu.rigidbody.velocity+targetspeed;
}
if(Input.GetKey(KeyCode.K)&&(Vector3.Distance(transform.position,qiu.transform.position)<2)){
targetspeed = transform.TransformDirection (Vector3.forward)*speed3;//-qiu.rigidbody.velocity;
print(targetspeed);
qiu.rigidbody.velocity=qiu.rigidbody.velocity+targetspeed;
}
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
if(Vector3.Distance(transform.position,qiu.transform.position)<2){
//~ targetspeed = transform.TransformDirection (Vector3.forward)*speed-qiu.rigidbody.velocity;
//~ print(targetspeed);
//~ qiu.rigidbody.AddForce(targetspeed);
//~ qiu.transform.position=Vector3(transform.position.x,transform.position.y,transform.position.z+1);
//~ clone.velocity = transform.TransformDirection (Vector3.forward * 10);
}
// Apply gravity
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags & CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
本条目发布于2010年1月18日。属于Unity脚本分类。