Skip to content

Instantly share code, notes, and snippets.

@Shubhra22
Last active June 29, 2018 23:41
Show Gist options
  • Save Shubhra22/a02b0aa906646ad8acbcbf8608dff52a to your computer and use it in GitHub Desktop.
Save Shubhra22/a02b0aa906646ad8acbcbf8608dff52a to your computer and use it in GitHub Desktop.
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;
}
}
}
}
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