Search Unity

Moving platforms

Discussion in 'Scripting' started by hedkace, Jul 31, 2015.

  1. hedkace

    hedkace

    Joined:
    Jun 29, 2015
    Posts:
    3
    I'm gonna try to reword this I guess. How do I do this?



    In player's update, he remembers the last platform he touches and its velocity at the time. In his update, he translates by "transform.position = transform.position + platformSpeed * Time.delateTime;". plaftormSpeed is the velocity of the rigidbody on the last platform he was on as determined by collision detection with the little blue line there.

    Sometimes my jump button doesn't work. Sometimes when he should stand still with respect to a moving platform, he exceeds its speed. Sometimes when he jumps vertically with respect to a on a moving platform, he ends up moving at a slightly slower speed horizontally than the platform underneath. And sometimes he slides.

    He does have a rigidbody attached to handle gravity. I don't know it that's interfering. If you use the code below, give the platform a distance of 50 or so. A cyclePeriod and 4 seconds, wait times of 1 second and "x" for the axis. Make it boomerang and cycle. Then the platform will sit for a second. Move 50 units in x for a second. Sit for a second then move back to where it started to repeat all that again. You will see the player not perform very well.

    Can anyone point out my problems or point me to other reference that do this another way? Thank you for reading.

    **Original Post no one responded to*****************

    I'm trying to get simple platformer physics working. I'm using Unity 5 Personal.

    Here's the player code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour {
    5.  
    6.     public float speed;
    7.     public float jump;
    8.     public Camera cam;
    9.  
    10.     private bool onGround;
    11.     private Vector3 platformSpeed;
    12.     private Rigidbody rb;
    13.     private GameObject lastPlatform;
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         rb = GetComponent<Rigidbody> ();
    19.         lastPlatform = null;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.         onGround = false;
    25.         RaycastHit[] hits = Physics.RaycastAll(new Ray(transform.position,-1*Vector3.up));
    26.         foreach (RaycastHit hit in hits) {
    27.             if(hit.distance < 1.05f){
    28.                 if (hit.collider.CompareTag("Ground")){
    29.                     onGround = true;
    30.                     lastPlatform = hit.collider.gameObject;
    31.                     PlatformController pc = lastPlatform.GetComponent<PlatformController>();
    32.                     platformSpeed = pc.rb.velocity;
    33.  
    34.                 }
    35.             }
    36.         }
    37.  
    38.         if (onGround) {
    39.             if (Input.GetButtonDown("Jump")){
    40.                 rb.AddForce(jump * Vector3.up);
    41.             }
    42.             Vector3 tempF = cam.transform.forward;
    43.             Vector3 tempR = cam.transform.right;
    44.             rb.MovePosition (transform.position + (speed * new Vector3(tempF.x * Input.GetAxis ("Vertical") + tempR.x * Input.GetAxis ("Horizontal"), 0f, tempF.z * Input.GetAxis ("Vertical") + tempR.z * Input.GetAxis ("Horizontal")) + platformSpeed) * Time.deltaTime);
    45.         }
    46.         print(onGround);
    47.  
    48.         if (transform.position.y < -5f) {
    49.             if (lastPlatform != null){
    50.                 transform.position = lastPlatform.transform.position + 2f * Vector3.up;
    51.             }
    52.         }
    53.     }
    54. }
    55.  
    Here's the platform code

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlatformController : MonoBehaviour {
    5.  
    6.  
    7.     public string axis;
    8.     public float distance;
    9.     public bool boomerang;
    10.     public bool cycle;
    11.     public float cyclePeriod;
    12.     public float waitTime;
    13.     public Rigidbody rb;
    14.  
    15.     private float moveTime;
    16.     private float speed;
    17.     private int direction;
    18.     private Vector3 axisVector;
    19.     private float lastTime;
    20.  
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.  
    25.         rb = GetComponent<Rigidbody> ();
    26.  
    27.         moveTime = (cyclePeriod - 2 * waitTime) / 2f;
    28.         speed = distance / moveTime;
    29.  
    30.  
    31.         if (axis == "x" || axis == "+x" || axis == "X" || axis == "+X"){
    32.             axisVector = speed * Vector3.forward;
    33.         }
    34.         if (axis == "y" || axis == "+y" || axis == "Y" || axis == "+Y"){
    35.             axisVector = Vector3.up;          
    36.         }
    37.         if (axis == "z" || axis == "+z" || axis == "Z" || axis == "+Z"){
    38.             axisVector = Vector3.right;
    39.         }
    40.         if (axis == "-x" || axis == "-X"){
    41.             axisVector = -1 * speed * Vector3.forward;
    42.         }
    43.         if (axis == "-y" || axis == "-Y"){
    44.             axisVector = -1 * speed * Vector3.up;
    45.         }
    46.         if (axis == "-z" || axis == "-Z"){
    47.             axisVector = -1 * speed * Vector3.right;          
    48.         }
    49.     }
    50.  
    51.     // Update is called once per frame
    52.     void Update () {
    53.  
    54.         if (Time.time < lastTime + cyclePeriod) {
    55.             direction = -1;
    56.             if (!boomerang){
    57.                 if (Time.time >= lastTime + cyclePeriod - moveTime){
    58.                     transform.position = transform.position + direction * axisVector;
    59.                     direction = 1;
    60.                     lastTime = Time.time - cyclePeriod;
    61.                 }
    62.             }
    63.         }
    64.         if (Time.time < lastTime + cyclePeriod - moveTime) {
    65.             direction = 0;
    66.         }
    67.         if (Time.time < lastTime + waitTime + moveTime) {
    68.             direction = 1;
    69.         }
    70.         if (Time.time < lastTime + waitTime) {
    71.             direction = 0;
    72.         }
    73.         if (Time.time >= lastTime + cyclePeriod) {
    74.             direction = 0;
    75.             if (cycle){
    76.                 lastTime = Time.time;
    77.             }
    78.         }
    79.  
    80.         rb.velocity = direction * axisVector;
    81.         //transform.position = transform.position + direction * axisVector * Time.deltaTime;
    82.  
    83.     }
    84. }
    85.  
    My physics is not good. Moves with the platform too much and exceeds it when on the platform, but if you jump after the platform starts moving, you will retain the velocity of the platform but it seems maybe a little slow. Also, jump button doesn't always take. Any suggestions? Thanks.
     
    Last edited: Aug 2, 2015
  2. hedkace

    hedkace

    Joined:
    Jun 29, 2015
    Posts:
    3