- Home /
Moving Platform can't detect player collider2d
I have been trying to make a platform that is stationary by default, it will them start moving when a player collider has been detected and then once the player has gotten off the platform it will go back to the original starting point. my character is the default character robot boy in the standard assets with no changes. I also have the moving platform prefab that has the sprite renderer and collider as the child.
here is the script so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour {
private Vector2 startPosition;
private Vector2 newPosition;
[SerializeField] private int speed = 3;
[SerializeField] private int maxDistance = 1;
void Awake()
{
}
void Start()
{
startPosition = transform.position;
newPosition = transform.position;
}
void Update()
{
}
void OnTriggerEnter2D ()
{
newPosition.x = startPosition.x + (maxDistance * Mathf.Sin (Time.time * speed));
transform.position = newPosition;
}
}
Answer by umair94 · Feb 12, 2019 at 04:37 AM
make the method correct
void OnTriggerEnter2D(Collider2D col){ and define here to collide with which object }
must check the onTrigger bool in the inspector , after that we check it again if not solve
Thanks umair, it works however, it will only move once a very small amount and not continously. is there way to fix that.