I made moving platforms on this video.

Video

But there is a problem, when my character gets on the platform, he starts to vibrate convulsively.

MovingPlatform script:

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]; } } } 

Script 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; } } } 

And the camera script that goes after the character:

 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)); } } } 

What could I miss?

My project

Thanks in advance.

  • It was necessary to also throw the script of the camera following the character)) - Alexey Shimansky

1 answer 1

Your problem is not in the character as such, and not in the platform, but in the fact that you control the camera position in FixedUpdate , which is most needed for objects with the Rigidbody/Rigidbody2D . And you need a method LateUpdate .

LateUpdate - just the same, referring to the documentation

This is useful to order script execution. For example, it should be noted that it has already moved inside.

preferably and should be used for the camera and its movement.

This is the main problem. And by placing the movement of the camera in that method, in principle, everything should be resolved. BUT (!) ...

Also try to make the calculation of posX and posY in the Update method just in case. That the calculation was there, and in LateUpdate already easy to write

 transform.position = new Vector3 (posX, posY, transform.position.z); 

since LateUpdate is called after all Update functions. Therefore, in fact, the movement of the camera is there.

And also look at the settings smoothTimeX , smoothTimeY , which also eventually make your camera tremble at certain values

  • The FixedUpdate method changed its name to LateUpdate, now the penguin shakes everywhere, not only on the platform. + Rendered posX and posY in Update, but transform.position = new Vector3 (posX, posY, transform.position.z); does not see them. ps for the future, do not take the code from different videos = D - Artik Slayer
  • @ArtikSlayer firstly where did you change the FixedUpdate hope in FollowCamera.cs , and not the character? You are shaking everywhere because of smoothTimeX/smoothTimeY , as I described in the answer .. Put zeros and make sure. Something needs to be changed there ........ Вынес posX и posY в Update, но transform.position = new Vector3 (posX, posY, transform.position.z); их не видит. Вынес posX и posY в Update, но transform.position = new Vector3 (posX, posY, transform.position.z); их не видит. - right ... they should be put into class variables. and since they are local to your method ... logical ... I hope you don’t need to learn the basics?)) ........ ps на будущее, не стоит брать код с разных видео - are you probably yourself? And then the code is taken from you from the project - Alexey Shimansky
  • In general, at the moment 2 things have helped. Change FixedUpdate to LateUpdate and replace moothTimeX and smoothTimeY with 0 or 0.001, but 0.01 does not work. Thank you, I didn’t think that the problem could be in the cell = D - Artik Slayer