Last active
August 29, 2015 13:57
-
-
Save smallrice45/9589576 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 MovePlayer : MonoBehaviour { | |
public float speed = 6.0F; | |
public float jumpSpeed = 8.0F; | |
public float gravity = 20.0F; | |
private Vector3 moveDirection = Vector3.zero; | |
void Update() { | |
CharacterController controller = GetComponent<CharacterController>(); | |
if (controller.isGrounded) { | |
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); | |
moveDirection = transform.TransformDirection(moveDirection); | |
moveDirection *= speed; | |
if (Input.GetButton("Jump")) | |
moveDirection.y = jumpSpeed; | |
} | |
moveDirection.y -= gravity * Time.deltaTime; | |
controller.Move(moveDirection * Time.deltaTime); | |
} | |
void OnControllerColliderHit(ControllerColliderHit hit) | |
{ | |
hit.collider.GetComponent<ObjectPingPongMove>().mPlayer = transform; | |
} | |
public void BlockMovePlayer(Vector3 movePos) | |
{ | |
transform.position += movePos; | |
} | |
} |
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 ObjectPingPongMove : MonoBehaviour { | |
public Transform targetA; | |
public Transform targetB; | |
private float timer; | |
private float weight; | |
public float moveSpeed = 1; | |
private Transform mTransform; | |
public Transform mPlayer; | |
void Awake() | |
{ | |
mTransform = GetComponent<Transform> (); | |
} | |
void Update () { | |
if (targetA != null){ | |
timer += Time.deltaTime; | |
weight = Mathf.Cos(timer * moveSpeed) * 0.5f + 0.5f; | |
Vector3 offestPos = targetA.transform.position * weight + targetB.transform.position * (1 - weight); | |
offestPos -= mTransform.position; | |
MoveBlock(offestPos); | |
} | |
} | |
void MoveBlock(Vector3 movePos) | |
{ | |
mTransform.position += movePos; | |
if (mPlayer) | |
{ | |
mPlayer.GetComponent<MovePlayer>().BlockMovePlayer(movePos); | |
} | |
mPlayer = null; | |
} | |
void OnCollisionEnter(Collision collision) | |
{ | |
print (collision.gameObject.name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment