Я сделал двигающиеся платформы, по этому видео.
Но есть проблема, когда мой персонаж становится на платформу, то он начинает судорожно вибрировать.
Скрипт MovingPlatform:
using UnityEngine;
using System.Collections;
public class MovingPlatform : MonoBehaviour {
public GameObject platform;
public float moveSpeed;
public Transform currentPoint;
public Transform[] points;
public int pointSelection;
// Use this for initialization
void Start () {
currentPoint=points[pointSelection];
}
// Update is called once per frame
void Update () {
platform.transform.position = Vector3.MoveTowards (platform.transform.position, currentPoint.position, Time.deltaTime * moveSpeed);
if (platform.transform.position == currentPoint.position)
{
pointSelection++;
if(pointSelection == points.Length)
{
pointSelection=0;
}
currentPoint = points[pointSelection];
}
}
}
Скрипт PlayerController:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public AudioSource jumpSoundEffect;
[Header ("Движение")]
public float JumpHeight;
public float moveSpeed;
private bool doubleJumped;
private float moveVelocity;
[Header ("Земля")]
[Space(10)]
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool grounded;
[Header ("Отбрасывание")]
[Space(10)]
public float knockback;
public float knockbackLength;
public float knockbackCount;
public bool knockbackFromRight;
private Animator anim;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
}
void FixedUpdate()
{
grounded = Physics2D.OverlapCircle (groundCheck.position,groundCheckRadius,whatIsGround);
}
// Update is called once per frame
void Update ()
{
if(Time.timeScale == 0f)
{
return;
}
if (grounded)
{
doubleJumped=false;
}
anim.SetBool ("Grounded", grounded);
if (Input.GetKeyDown (KeyCode.Space) && grounded)
{
//GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
Jump ();
}
if (Input.GetKeyDown (KeyCode.Space) && !doubleJumped && !grounded)
{
//GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
Jump ();
doubleJumped=true;
}
moveVelocity = 0f;
if (Input.GetKey (KeyCode.D))
{
//GetComponent<Rigidbody2D>().velocity = new Vector2 (moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
moveVelocity = moveSpeed;
}
if (Input.GetKey (KeyCode.A))
{
//GetComponent<Rigidbody2D>().velocity = new Vector2 (-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
moveVelocity = -moveSpeed;
}
if (knockbackCount <= 0) {
GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
} else {
if(knockbackFromRight)
GetComponent<Rigidbody2D> ().velocity = new Vector2 (-knockback, knockback);
if(!knockbackFromRight)
GetComponent<Rigidbody2D> ().velocity = new Vector2 (knockback, knockback);
knockbackCount -= Time.deltaTime;
}
var speed = Mathf.Abs(GetComponent<Rigidbody2D> ().velocity.x);
anim.SetFloat ("Speed", speed);
if(speed > 0.1f)
{
//Баг блоха.
if(GetComponent<Rigidbody2D>().velocity.x > 0)
transform.localScale = new Vector3(4f,4f,4f);
else if(GetComponent<Rigidbody2D>().velocity.x < 0)
transform.localScale = new Vector3(-4f,4f,4f);
}
}
public void Jump()
{
GetComponent<Rigidbody2D>().velocity = new Vector2 (GetComponent<Rigidbody2D>().velocity.x, JumpHeight);
jumpSoundEffect.Play ();
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.transform.tag == "MovingPlatform")
{
transform.parent = other.transform;
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.transform.tag == "MovingPlatform")
{
transform.parent = null;
}
}
}
И скрипт камеры, которая ходит за персонажем:
public class CameraFollow : MonoBehaviour {
private Vector2 velocity;
public float smoothTimeY;
public float smoothTimeX;
//public GameObject player;
public PlayerController player;
public bool bounds;
public Vector3 minCameraPos;
public Vector3 maxCameraPos;
// Use this for initialization
void Start () {
//player = GameObject.FindGameObjectsWithTag ("Player");
player = FindObjectOfType<PlayerController>();
}
// Update is called once per frame
void FixedUpdate () {
float posX = Mathf.SmoothDamp (transform.position.x, player.transform.position.x, ref velocity.x, smoothTimeX);
float posY = Mathf.SmoothDamp (transform.position.y, player.transform.position.y, ref velocity.x, smoothTimeY);
transform.position = new Vector3 (posX, posY, transform.position.z);
if (bounds)
{
transform.position = new Vector3(Mathf.Clamp(transform.position.x, minCameraPos.x, maxCameraPos.x),
Mathf.Clamp(transform.position.y, minCameraPos.y, maxCameraPos.y),
Mathf.Clamp(transform.position.z, minCameraPos.z, maxCameraPos.z));
}
}
}
Что я мог пропустить?
Заранее благодарен.