Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving Platform Trouble

Discussion in 'Scripting' started by H00D3DM4N2787, Aug 20, 2014.

  1. H00D3DM4N2787

    H00D3DM4N2787

    Joined:
    Aug 6, 2014
    Posts:
    27
    I got platform moving working

    but the platform keeps moving from under my character and i fall off
    rather than taking my character with it, which is wat i want to happen.

    the platform is a cube with a box collider

    and my character is simply a mesh i made with a few scripts and a character controller attached.

    im wondering if theres something i need to do in order to make this work out

    if its a scripting issue
    heres my platform script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LRPlatform : MonoBehaviour {
    5.  
    6.     public float movespeed = 0.1f;
    7.  
    8.     public Transform mytransform;
    9.  
    10.     public GameObject origmesh;
    11.     public GameObject destmesh;
    12.  
    13.     public Transform Destin;
    14.     public Transform Origin;
    15.  
    16.     public bool switcch = false;
    17.  
    18.     // Use this for initialization
    19.     void Start () {
    20.  
    21.         mytransform = transform;
    22.  
    23.         destmesh = GameObject.FindGameObjectWithTag ("Destin");
    24.  
    25.         origmesh = GameObject.FindGameObjectWithTag ("Origin");
    26.  
    27.         Origin = origmesh.transform;
    28.  
    29.         Destin = destmesh.transform;
    30.  
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void FixedUpdate () {
    35.  
    36.         if (mytransform.position == Destin.position) {
    37.             switcch = true;
    38.         }
    39.         if (mytransform.position == Origin.position) {
    40.             switcch = false;
    41.         }
    42.  
    43.         if (switcch) {
    44.             mytransform.position = Vector3.MoveTowards (mytransform.position, Origin.position, movespeed);
    45.         } else {
    46.             mytransform.position = Vector3.MoveTowards (mytransform.position, Destin.position, movespeed);
    47.         }
    48.     }
    49. }
    and heres my character script for movement

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MoveJoe : MonoBehaviour {
    5.  
    6.     public float jump1 = 10;
    7.     public float turnspeed = 100;
    8.     public float walkspeed = 10;
    9.     public float runMultiplier = 3;
    10.     public float midairmove = 20;
    11.     public float gravity = 25;
    12.     public float airtime = 0;
    13.     public float jumptime = 1;
    14.     public float fall = 1;
    15.     public float boostspeed = 40;
    16.  
    17.     public CollisionFlags collideflags;
    18.  
    19.     private Vector3 movedirection;
    20.     private CharacterController ctrl;
    21.     private Transform mytransform;
    22.  
    23.     void Start () {
    24.  
    25.         mytransform = transform;
    26.         ctrl = GetComponent<CharacterController> ();
    27.         movedirection = Vector3.zero;
    28.  
    29.         }
    30.  
    31.     // Update is called once per frame
    32.     void Update () {
    33.  
    34.         Turn ();
    35.         Strafe ();
    36.  
    37.         movedirection.y -= gravity * Time.deltaTime;
    38.         collideflags = ctrl.Move (movedirection * Time.deltaTime);
    39.  
    40.         if (ctrl.isGrounded) {
    41.  
    42.             movedirection = new Vector3 ( 0, 0, 0);
    43.  
    44.             Walk ();
    45.  
    46.             airtime = 0;
    47.          
    48.             if (Input.GetButton ("Jump")) {
    49.                     movedirection.y += jump1;
    50.             }
    51.         }
    52.         else {
    53.  
    54.             if ((collideflags & CollisionFlags.CollidedBelow) == 0) {
    55.                 airtime += Time.deltaTime;
    56.  
    57.                 MidAirMove ();
    58.              
    59.                 if (airtime > fall) {
    60.                     Fall ();
    61.                 }
    62.              
    63.             }
    64.         }
    65.  
    66.         ctrl.Move (movedirection * Time.deltaTime);
    67. }
    68.  
    69.     void Turn () {
    70.  
    71.         if (Mathf.Abs (Input.GetAxis ("Horizontal")) > 0) {
    72.             mytransform.Rotate (0, Input.GetAxis ("Horizontal") * Time.deltaTime * turnspeed, 0);
    73.             }
    74.         }
    75.  
    76.     void Walk () {
    77.  
    78.         if (Mathf.Abs (Input.GetAxis ("Vertical")) > 0) {
    79.             if(Input.GetButton("Sprint")) {
    80.                 ctrl.Move(mytransform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical") * walkspeed * runMultiplier * Time.deltaTime);
    81.                 }
    82.             else {
    83.                 ctrl.Move(mytransform.TransformDirection(Vector3.forward) * Input.GetAxis("Vertical") * walkspeed * Time.deltaTime);
    84.                 }
    85.             }
    86.         }
    87.  
    88.     void Strafe () {
    89.  
    90.         if (Mathf.Abs (Input.GetAxis ("Strafe")) > 0) {
    91.             ctrl.Move(mytransform.TransformDirection(Vector3.right) * Input.GetAxis("Strafe") * walkspeed * Time.deltaTime);      
    92.             }
    93.         }
    94.  
    95.     void MidAirMove () {
    96.  
    97.         if (Mathf.Abs (Input.GetAxis ("Vertical")) > 0) {
    98.             ctrl.Move (mytransform.TransformDirection (Vector3.forward) * Input.GetAxis ("Vertical") * midairmove * Time.deltaTime);
    99.                 }
    100.         }
    101.  
    102.     public void Fall () {
    103.      
    104.     }
    105. }
    any advice would be greatly appreciated

    ill keep trying different things in the meantime
     
  2. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    There are two ways to handle this.

    1. When the character is on the platform. Apply the platform movement to the character in the FixedUpdate of your platform script.

    2. Looks like your character is physics based. The game I am currently developing had this problem. I handled this by adding a square pad collider with friction to the feet of my character whenever it is on a platform. And I changed my platform so that it's movement is handled by physics as well. This way, when the platform moves, the frictional forces make your character move.
     
  3. H00D3DM4N2787

    H00D3DM4N2787

    Joined:
    Aug 6, 2014
    Posts:
    27
    hmm how would i go about doing the second one?
     
  4. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    I am at work at the moment. When I get home tonight, I will post some code examples from my project (in about 8 hours).
     
  5. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    Back again.

    Rather than extracting example code for platform. I have copied my entire platform move script below. I have also included my editor which gives you some handles for positioning the way points in the scene editor.

    I add a box collider to my character. Positioned at its feet, by default this is disabled. The first block of example code, shows how I turn on the box collider when I am on a platform..

    Code (CSharp):
    1.      
    2. if (Physics.SphereCast(p1, capsuleCollider.radius, -Vector3.up, out hitInfo, distToGround - 0f, layerMaskWorld))
    3. {
    4.     // Check if the object we are on has the PlatformMove component
    5.     PlatformMove _move = hitInfo.transform.GetComponent<PlatformMove>();
    6.     if (_move)
    7.     {
    8.          // Activate our box collider so we have more friction on the platform
    9.          if (!boxCollider.enabled) boxCollider.enabled = true;
    10.      }
    11. }        
    12.  

    My Platform Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlatformMove : MonoBehaviour
    5. {
    6.     enum platformDirection { Forward, Backward }
    7.     enum movementPhase { accellerate, travel, decellerate }
    8.     public enum movementType { RotateClockwise, RotateAntiClockwise, LeftRight, UpDown, Diaganol }
    9.  
    10.     public float speed = 1f; // movements per second
    11.     public float timeToSpeed = 0.5f; // Accelleration time to full speed
    12.  
    13.     public movementType moveType = movementType.LeftRight;
    14.     public float mass = 500;
    15.  
    16.     public bool platformActive = true;
    17.     public int moveCount = 0;
    18.  
    19.     public Vector3 p0;
    20.     public Vector3 p1;
    21.     public Vector3 pA;
    22.     public Vector3 pB;
    23.     private Vector3 direction;
    24.     private movementPhase phase;
    25.     private platformDirection currentDir;
    26.     private float distance;
    27.     private Vector3 oldPosition;
    28.     private Quaternion oldRotation;
    29.     private Vector3 pw0;
    30.     private Vector3 pw1;
    31.     private float pushForce;
    32.     private float sqrSpeed;
    33.     private bool bFirst = true;
    34.     private float accellerationDistance = 0f;
    35.     private float currentSpeed = 0f;
    36.     private float distanceLeftToTravel = 0f;
    37.     private float lastDistanceLeftToTravel = 0f;
    38.     private Vector3 torqueForce;
    39.     private Rigidbody myRigidBody;
    40.  
    41.     private bool stopNext;
    42.     private int _moveCounter;
    43.  
    44.     void Start()
    45.     {
    46.  
    47.         // Add a rigid body
    48.         myRigidBody = transform.gameObject.AddComponent<Rigidbody>();
    49.         myRigidBody.mass = mass;
    50.         myRigidBody.useGravity = false;
    51.  
    52.  
    53.         if (moveType == movementType.LeftRight || moveType == movementType.UpDown || moveType == movementType.Diaganol)
    54.         {
    55.             // Convert way points to realworld coordinates
    56.             switch (moveType)
    57.             {
    58.                 case movementType.LeftRight:
    59.                     //pw0 = transform.TransformPoint(p0);
    60.                     //pw1 = transform.TransformPoint(new Vector3(p1.x, p0.y, p0.z));
    61.                     pw0 = pA;
    62.                     pw1 = new Vector3(pB.x, pA.y, pA.z);
    63.                     myRigidBody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
    64.                     break;
    65.                 case movementType.UpDown:
    66.                     //pw0 = transform.TransformPoint(p0);
    67.                     //pw1 = transform.TransformPoint(new Vector3(p0.x, p1.y, p0.z));
    68.                     pw0 = pA;
    69.                     pw1 = new Vector3(pA.x, pB.y, pA.z);
    70.                     myRigidBody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
    71.                     break;
    72.                 case movementType.Diaganol:
    73.                     //pw0 = transform.TransformPoint(p0);
    74.                     //pw1 = transform.TransformPoint(p1);
    75.                     pw0 = pA;
    76.                     pw1 = pB;
    77.                     myRigidBody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionZ;
    78.                     break;
    79.             }
    80.  
    81.  
    82.             // Set platform at first coordinate
    83.             transform.position = pw0;
    84.  
    85.             // Calculate the direction from p0 to p1
    86.             direction = (pw1 - pw0).normalized;
    87.  
    88.             // Calculate the distance between the 2 points
    89.             distance = (pw1 - pw0).sqrMagnitude;
    90.  
    91.             // Initialise work variables
    92.             oldPosition = transform.position;
    93.             currentDir = platformDirection.Forward;
    94.             phase = movementPhase.accellerate;
    95.         }
    96.         else
    97.         {
    98.             myRigidBody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ;
    99.  
    100.             if (moveType == movementType.RotateAntiClockwise)
    101.                 torqueForce = Vector3.forward;
    102.             else
    103.                 torqueForce = Vector3.back;
    104.         }
    105.  
    106.  
    107.  
    108.         // Don't move if not active
    109.         if (!platformActive) myRigidBody.isKinematic = true;
    110.         _moveCounter = moveCount;
    111.  
    112.  
    113.         // Calculate the force we need to use to get platform up to "speed" within "timeToSpeed" seconds
    114.         float accelleration = (2 * speed) / (timeToSpeed * timeToSpeed);
    115.         pushForce = transform.rigidbody.mass * accelleration;
    116.  
    117.         // We use square magnitudes for tracking speed, so calculate our speed as a spuare once for later use
    118.         sqrSpeed = speed * speed;
    119.  
    120.  
    121.  
    122.     }
    123.  
    124.  
    125.     public void StartMoving()
    126.     {
    127.         myRigidBody.isKinematic = false;
    128.         this.platformActive = true;
    129.         stopNext = false;
    130.         _moveCounter = moveCount;
    131.     }
    132.  
    133.     public void StopMoving()
    134.     {
    135.         platformActive = false;
    136.         myRigidBody.isKinematic = true;
    137.     }
    138.  
    139.     void FixedUpdate()
    140.     {
    141.  
    142.         if (platformActive)
    143.         {
    144.  
    145.             if (moveType == movementType.Diaganol || moveType == movementType.LeftRight || moveType == movementType.UpDown)
    146.             {
    147.                 StraightLineUpdate();
    148.             }
    149.             else
    150.             {
    151.                 RotateUpdate();
    152.             }
    153.  
    154.             if (moveCount > 0 && _moveCounter <= 0) StopMoving();
    155.  
    156.         }
    157.     }
    158.  
    159.     private void RotateUpdate()
    160.     {
    161.         currentSpeed = transform.rigidbody.angularVelocity.sqrMagnitude;
    162.  
    163.         if (currentSpeed < sqrSpeed)
    164.         {
    165.             transform.rigidbody.AddTorque(torqueForce * pushForce);
    166.         }
    167.  
    168.     }
    169.  
    170.     private void StraightLineUpdate()
    171.     {
    172.         // Get distance travelled since last frame
    173.         Vector3 thisDelta = transform.position - oldPosition;
    174.         currentSpeed = (thisDelta / Time.deltaTime).sqrMagnitude;
    175.         oldPosition = transform.position;
    176.  
    177.  
    178.         if (currentDir == platformDirection.Forward)
    179.             distanceLeftToTravel = distance - (transform.position - pw0).sqrMagnitude;
    180.         else
    181.             distanceLeftToTravel = distance - (transform.position - pw1).sqrMagnitude;
    182.  
    183.         switch (phase)
    184.         {
    185.             case movementPhase.accellerate:
    186.             case movementPhase.travel:
    187.                 StraightlineAccellerate();
    188.                 CheckDecelleratePoint();
    189.                 break;
    190.             case movementPhase.decellerate:
    191.                 Decellerate();
    192.                 break;
    193.         }
    194.  
    195.         lastDistanceLeftToTravel = distanceLeftToTravel;
    196.     }
    197.  
    198.     private void Decellerate()
    199.     {
    200.         if (currentDir == platformDirection.Forward)
    201.         {
    202.             // If we have reached waypoint or we are moving away from waypoint
    203.             if (distanceLeftToTravel < 0f || lastDistanceLeftToTravel < distanceLeftToTravel)
    204.             {
    205.                 currentDir = platformDirection.Backward;
    206.                 transform.rigidbody.velocity = Vector3.zero;
    207.                 phase = movementPhase.accellerate;
    208.  
    209.                 // Updated move counter
    210.                 _moveCounter--;
    211.             }
    212.             else
    213.             {
    214.                 // Slow down for turn
    215.                 transform.rigidbody.AddForce(direction * (-pushForce) * 0.75f);
    216.             }
    217.         }
    218.         else
    219.         {
    220.             // If we have reached waypoint or we are moving away from waypoint
    221.             if (distanceLeftToTravel < 0f || lastDistanceLeftToTravel < distanceLeftToTravel)
    222.             {
    223.                 currentDir = platformDirection.Forward;
    224.                 transform.rigidbody.velocity = Vector3.zero;
    225.                 phase = movementPhase.accellerate;
    226.  
    227.                 // Updated move counter
    228.                 _moveCounter--;
    229.             }
    230.             else
    231.             {
    232.                 // Slow down for turn
    233.                 transform.rigidbody.AddForce(direction * pushForce * 0.75f);
    234.             }
    235.         }
    236.     }
    237.  
    238.     private void CheckDecelleratePoint()
    239.     {
    240.         if (distanceLeftToTravel < accellerationDistance)
    241.         {
    242.             phase = movementPhase.decellerate;
    243.         }
    244.  
    245.     }
    246.  
    247.     private void StraightlineAccellerate()
    248.     {
    249.         // Accellerate if we are not up to speed
    250.         if (currentSpeed < sqrSpeed)
    251.         {
    252.             if (currentDir == platformDirection.Forward)
    253.                 transform.rigidbody.AddForce(direction * pushForce);
    254.             else
    255.                 transform.rigidbody.AddForce(direction * (-pushForce));
    256.         }
    257.         else
    258.         {
    259.             phase = movementPhase.travel;
    260.             if (bFirst)
    261.             {
    262.                 // First time through, save distance travelled to come up to speed
    263.                 accellerationDistance = (transform.position - pw0).sqrMagnitude;
    264.  
    265.                 // Don't let accelleration distance be greater than half the distance
    266.                 // Between 2 way points
    267.                 if (accellerationDistance > (distance / 2))
    268.                     accellerationDistance = distance / 2;
    269.                 bFirst = false;
    270.             }
    271.         }
    272.     }
    273.  
    274. }
    275.  
    Platform Move Editor:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5. [CustomEditor(typeof(PlatformMove))]
    6. public class PlatformMoveEditor : Editor
    7. {
    8.  
    9.     PlatformMove p;
    10.  
    11.     private static Vector3 pointSnap0 = Vector3.one * 0.1f;
    12.     private static Vector3 pointSnap1 = Vector3.one * 0.1f;
    13.  
    14.     SerializedProperty speed;
    15.     SerializedProperty timeToSpeed;
    16.     SerializedProperty moveType;
    17.     SerializedProperty mass;
    18.     SerializedProperty active;
    19.     SerializedProperty moveCount;
    20.     SerializedProperty pA;
    21.     SerializedProperty pB;
    22.  
    23.     GUIStyle boxStyle;
    24.     private Color softGreen = new Color(.67f, .89f, .67f, 1f);
    25.     private static GUIContent pointContent = GUIContent.none;
    26.  
    27.     void OnEnable()
    28.     {
    29.         p = (PlatformMove)target;
    30.  
    31.         speed = serializedObject.FindProperty("speed");
    32.         timeToSpeed = serializedObject.FindProperty("timeToSpeed");
    33.         moveType = serializedObject.FindProperty("moveType");
    34.         mass = serializedObject.FindProperty("mass");
    35.         active = serializedObject.FindProperty("platformActive");
    36.         moveCount = serializedObject.FindProperty("moveCount");
    37.         pA = serializedObject.FindProperty("pA");
    38.         pB = serializedObject.FindProperty("pB");
    39.  
    40.         if (Application.isPlaying) return;
    41.     }
    42.  
    43.  
    44.     void OnSceneGUI()
    45.     {
    46.  
    47.  
    48.         p = (PlatformMove)target;
    49.         if (p == null) return;
    50.  
    51.         //Vector3 oldPoint0 = p.transform.TransformPoint(p.p0);
    52.         Vector3 oldPoint0 = p.pA;
    53.         Vector3 newPoint0 = Handles.FreeMoveHandle(oldPoint0, Quaternion.identity, 0.4f, pointSnap0, Handles.DotCap);
    54.  
    55.         if (oldPoint0 != newPoint0)
    56.         {
    57.             newPoint0.z = 0f;
    58.             //p.p0 = p.transform.InverseTransformPoint(newPoint0);
    59.             p.pA = newPoint0;
    60.  
    61.         }
    62.  
    63.         //Vector3 oldPoint1 = p.transform.TransformPoint(p.p1);
    64.         Vector3 oldPoint1 = p.pB;
    65.         Vector3 newPoint1 = Handles.FreeMoveHandle(oldPoint1, Quaternion.identity, 0.4f, pointSnap1, Handles.DotCap);
    66.  
    67.         if (oldPoint1 != newPoint1)
    68.         {
    69.             newPoint1.z = 0f;
    70.             //p.p1 = p.transform.InverseTransformPoint(newPoint1);
    71.             p.pB = newPoint1;
    72.  
    73.         }
    74.  
    75.  
    76.         if (GUI.changed)
    77.         {
    78.             EditorUtility.SetDirty(target);
    79.         }
    80.  
    81.     }
    82.  
    83.     public override void OnInspectorGUI()
    84.     {
    85.         p = (PlatformMove)target;
    86.         if (p == null) return;
    87.  
    88.         serializedObject.Update();
    89.  
    90.         // Check if any control changed between here and EndChangeCheck
    91.         EditorGUI.BeginChangeCheck();
    92.  
    93.         EditorGUILayout.PropertyField(speed, true);
    94.         EditorGUILayout.PropertyField(timeToSpeed, true);
    95.         EditorGUILayout.PropertyField(moveType, true);
    96.         EditorGUILayout.PropertyField(mass, true);
    97.         EditorGUILayout.PropertyField(active, true);
    98.         EditorGUILayout.PropertyField(moveCount, true);
    99.         EditorGUISetPos(pA);
    100.         EditorGUISetPos(pB);
    101.  
    102.  
    103.         // If any control changed, then apply changes
    104.         if (EditorGUI.EndChangeCheck())
    105.         {
    106.             serializedObject.ApplyModifiedProperties();
    107.         }
    108.  
    109.     }
    110.  
    111.     /// <summary>
    112.     /// Dispalay Scene property with drop area
    113.     /// </summary>
    114.     /// <param name="sceneId"></param>
    115.     private void EditorGUISetPos(SerializedProperty point)
    116.     {
    117.         boxStyle = GUI.skin.box;
    118.         EditorGUILayout.BeginVertical(boxStyle);
    119.  
    120. //        EditorGUILayout.BeginHorizontal();
    121.         //float px = point.vector3Value.x;
    122.         //float py = point.vector3Value.y;
    123.         //float pz = point.vector3Value.z;
    124.  
    125.         EditorGUILayout.PropertyField(point, true);
    126.         //EditorGUILayout.PropertyField(point.FindPropertyRelative("x"), GUILayout.MaxWidth(20));
    127.         //EditorGUILayout.PropertyField(point.FindPropertyRelative("y"), GUILayout.MaxWidth(20));
    128.         //EditorGUILayout.PropertyField(point.FindPropertyRelative("z"), GUILayout.MaxWidth(20));
    129.  
    130.         //Vector3 newPos = new Vector3(px, py, pz);
    131.         //if (point.vector3Value != newPos) point.vector3Value = newPos;
    132.  
    133.         GUILayout.FlexibleSpace();
    134.         {
    135.             GUI.color = softGreen;
    136.             if (GUILayout.Button("set", GUILayout.Width(75f)))
    137.             {
    138.                 point.vector3Value = p.transform.position;
    139.             }
    140.             GUI.color = Color.white;
    141.         }
    142.         GUILayout.FlexibleSpace();
    143. //        EditorGUILayout.EndHorizontal();
    144.  
    145.         EditorGUILayout.EndVertical();
    146.  
    147.     }
    148.  
    149. }
     
  6. H00D3DM4N2787

    H00D3DM4N2787

    Joined:
    Aug 6, 2014
    Posts:
    27
    Thank you

    i will study these scripts closely
    and i will see wat i can learn from them

    thanks again for all ur help
     
    Zaddo67 likes this.
  7. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    No worries. I hope this helps.

    You should be able to copy the PlatformMove & PlatformMoveEditor scripts directly into your project. Then attach the PlatformMove to your platform. This should hopefully just work as is. Note: the script adds a rigidbody to the platform, so make sure you don't have one already on it. Once you have done this, your character should start to move with the platform, but it will be a bit slippery. So just add the Box collider to your character and turn it off and on as I describe above and hopfully you get the result you want.
     
  8. H00D3DM4N2787

    H00D3DM4N2787

    Joined:
    Aug 6, 2014
    Posts:
    27
    i know i can copy them over

    but i dont like doing that

    i like learning from it and seeing if i can recreate my own script
    copying it i dont feel like im learning
     
  9. H00D3DM4N2787

    H00D3DM4N2787

    Joined:
    Aug 6, 2014
    Posts:
    27
    Im sorry one last question

    i dont use rigid body on my character

    but for the scripts you've given me to work

    does i need a rigidbody on my character?
    because im quite happy with the character controler
    with out the rigidbody
     
  10. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    I applaud your desire to learn. Doing it yourself is certainly the best way to improve your knowledge.

    My platform implementation won't work if your character doesn't have a rigid body. I thought from your scripts, your character was physics based. My mistake.

    You will have to go back to the first option I suggested above and that is apply the platform movement to your character.

    Just quickly, here are my thoughts on the steps you should take.

    1. add class variable to LRPLatform
    public Transform character.

    2. add class variable to MoveJoe
    public LRPlatform lrplatform

    3. In LRPlatform.FixedUpdate. if character variable is not null, then move the character with the platform movement.you will need to calculate the platform movement.
    if (character != null) character.transform += platformmovement

    4. In MoveJoe Update, you will need to set and clear platform variables as needed.

    // Do a spherecast or raycast down from character to see if you are standing on a platform. Set distance so it is just blow the feet of character
    if (Physics.SphereCast(p1, capsuleCollider.radius, -Vector3.up, out hitInfo, distToGround - 0f, layerMaskWorld))
    {
    // Check if the object we are on has the PlatformMove component
    LRPlatform platform = hitInfo.transform.GetComponent<LRPlatform>();
    if (platform)
    {
    platform.character = transform;
    lrplatform = platform;
    }
    else
    {
    if (lrplatform != null)
    {
    lrplatform.character = null;
    lrplatform = null;
    }
    }
    }
     
  11. H00D3DM4N2787

    H00D3DM4N2787

    Joined:
    Aug 6, 2014
    Posts:
    27
    actually thanks to ur help
    i figured a nice work around
    it works similarly to ur first option
    so thanks for everything :)
     
  12. cranky

    cranky

    Joined:
    Jun 11, 2014
    Posts:
    180
    I am using a physics based controller, and I solved this problem a while back by simply parenting the player to the platform upon collision, and de-parenting upon exiting. When the player leaves the platform, I also apply the platform's velocity to the player so that you move with it when you jump.

    I did try a friction implementation a while back, but it made player movement slower on the platform due to friction.
     
    Zaddo67 likes this.
  13. Zaddo67

    Zaddo67

    Joined:
    Aug 14, 2012
    Posts:
    489
    Nice solution. Much more simple/elegant than the friction solution I proposed.