Last active
June 29, 2018 23:41
-
-
Save Shubhra22/a02b0aa906646ad8acbcbf8608dff52a 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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
//using UnityStandardAssets._2D; | |
namespace UnityStandardAssets._2D | |
{ | |
public class FollowPlayer : MonoBehaviour | |
{ | |
public GameObject Player; | |
float speed; | |
Vector3 targetPosition; | |
bool jump; | |
bool didJump; | |
public static FollowPlayer instance; | |
private void Awake() | |
{ | |
instance = this; | |
} | |
public float Speed | |
{ | |
get | |
{ | |
return speed; | |
} | |
} | |
public bool Jump | |
{ | |
get | |
{ | |
return jump; | |
} | |
set | |
{ | |
jump = value; | |
} | |
} | |
void Update() | |
{ | |
targetPosition = Player.transform.position - transform.position; | |
jump = false; | |
if (targetPosition.magnitude > 3) | |
{ | |
if (targetPosition.x < 0) | |
{ | |
speed = -1; | |
} | |
else | |
{ | |
speed = 1; | |
} | |
} | |
else | |
{ | |
speed = 0; | |
//print(targetPosition.magnitude); | |
} | |
} | |
private void OnTriggerEnter2D(Collider2D collision) | |
{ | |
if (collision.transform.tag == "Jump") | |
{ | |
jump = true; | |
} | |
} | |
} | |
} |
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 System; | |
using UnityEngine; | |
using UnityStandardAssets.CrossPlatformInput; | |
//using AISystem; | |
namespace UnityStandardAssets._2D | |
{ | |
[RequireComponent(typeof (PlatformerCharacter2D))] | |
public class Platformer2DUserControl : MonoBehaviour | |
{ | |
private PlatformerCharacter2D m_Character; | |
private bool m_Jump; | |
private void Awake() | |
{ | |
m_Character = GetComponent<PlatformerCharacter2D>(); | |
} | |
bool jumped; | |
private void Update() | |
{ | |
if (!m_Jump) | |
{ | |
// Read the jump input in Update so button presses aren't missed. | |
m_Jump = FollowPlayer.instance.Jump; //CrossPlatformInputManager.GetButtonDown("Jump"); | |
if(m_Jump) | |
print(m_Jump); | |
} | |
} | |
private void FixedUpdate() | |
{ | |
// Read the inputs. | |
bool crouch = Input.GetKey(KeyCode.LeftControl); | |
float h = FollowPlayer.instance.Speed;//CrossPlatformInputManager.GetAxis("Horizontal"); | |
// Pass all parameters to the character control script. | |
m_Character.Move(h, crouch, m_Jump); | |
m_Jump = false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment