okay so I know this has been asked before but it seems like the other implementations are different from my own. Im working on a basic platformer and im having some issues with moving platforms. I’ve tried parenting, and im currently playing with directly setting the velocity when they are touched. I added a second collider to my player so that it will only check on his bottom but i cant get the velocity to transfer correctly.
Here is how my player object is set-up
and heres my platform
but when it runs depending on which implementation i use one of two things happens.
1.) if i parent then the player remains completely stationary
2.) if i set velocity the player moves at a different speed from the platform, but the velocity shows up as being the same between the two.
here is my code so far.
character controller
using UnityEngine;
using System.Collections;
public class characterController : MonoBehaviour {
//axis values
public float horizontal = 0;
public float vertical = 0;
public float shoot = 0;
public float moveMultiplier = 1;
public float jumpTime = 3;
public float jumpSpeed = 1;
public float jumpCounter = 0;
public bool jumping = false;
Rigidbody2D phy;
// Use this for initialization
void Start ()
{
phy = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update ()
{
//Debug.Log("player Velocity: " + Time.fixedTime + " - " + GetComponent<Rigidbody2D>().velocity);
horizontal =Input.GetAxis("horizontal");
vertical = Input.GetAxis("vertical");
//movement
phy.velocity = new Vector2((horizontal * moveMultiplier), phy.velocity.y);
//jumping
if (!jumping)
{
if (vertical != 0 && jumpCounter<jumpTime)
{
phy.velocity = new Vector2(phy.velocity.x, jumpSpeed * vertical);
jumping = true;
}
}
else
{
if (vertical != 0 && jumpCounter < jumpTime)
{
phy.velocity = new Vector2(phy.velocity.x, jumpSpeed * vertical);
}
jumpCounter += Time.deltaTime;
}
if(phy.velocity.y==0)
{
jumping = false;
jumpCounter = 0;
}
}
}
Platform code
using UnityEngine;
using System.Collections;
public class velMovingPlatform : MonoBehaviour {
public GameObject startPoint;
public GameObject endPoint;
public Vector2 distance;
public float time=5;
public Vector2 velocityReq;
public int phase = 0;
public float waitTime = 3;
public float timer=0;
// Use this for initialization
void Start ()
{
Vector2 startPos = startPoint.transform.position;
Vector2 endPos = endPoint.transform.position;
distance = new Vector2(Mathf.Abs(startPos.x - endPos.x), Mathf.Abs(startPos.y - endPos.y));
velocityReq = new Vector2(distance.x / time, distance.y / time);
}
// Update is called once per frame
void Update ()
{
//log player velocity
//Debug.Log("platform Velocity: " + Time.fixedTime + " - " + GetComponent<Rigidbody2D>().velocity);
switch (phase)
{
case 0:
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
timer += Time.deltaTime;
timer = Mathf.Clamp(timer, 0, waitTime);
if(timer==waitTime)
{
phase = 1;
timer = 0;
}
break;
case 1:
timer += Time.deltaTime;
timer = Mathf.Clamp(timer, 0, time);
if (timer == time)
{
phase = 2;
timer = 0;
}
else
{
GetComponent<Rigidbody2D>().velocity = velocityReq;
}
break;
case 2:
GetComponent<Rigidbody2D>().velocity = Vector2.zero;
timer += Time.deltaTime;
timer = Mathf.Clamp(timer, 0, waitTime);
if (timer == waitTime)
{
phase = 3;
timer = 0;
}
break;
case 3:
timer += Time.deltaTime;
timer = Mathf.Clamp(timer, 0, time);
if (timer == time)
{
phase = 0;
timer = 0;
}
else
{
GetComponent<Rigidbody2D>().velocity = -velocityReq;
}
break;
}
}
}
Platform Controller
using UnityEngine;
using System.Collections;
public class movingPlatformController : MonoBehaviour {
public GameObject player;
public bool locked = false;
public Vector3 oldpos;
public Vector3 newpos;
public Vector3 difference;
public GameObject othercollider;
void OnTriggerEnter2D(Collider2D other)
{
othercollider = other.gameObject;
//Debug.Log("colEnter layer: " + other.gameObject.layer);
if (other.gameObject.layer == 8)
{
// player.transform.SetParent(other.transform);
}
}
void OnTriggerStay2D(Collider2D other)
{
if(other.gameObject.layer==8)
{
player.GetComponent<Rigidbody2D>().velocity = other.GetComponent<Rigidbody2D>().velocity;
Debug.Log(player.GetComponent<Rigidbody2D>().velocity + " / " + player.GetComponentInParent<Rigidbody2D>().velocity);
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.layer == 8)
{
// player.transform.parent = null;
locked = false;
}
}
}
For the life of me i cant figure it out. Any Ideas?