Hello,
A bit of background::::: I wrote a 2D player controller in C# and it was working fine until I realized that I could go through the back of box colliders. if I hit the collider from the front it pops me up to the next collider up (it's stairs doe they get smaller as you go up) if you hit it from the back (where the stack of colliders are aligned) it lets you go through the collider and back to the front of the object. I tried adding a Polygon collider to see what would happen and it pops you to the bottom of the collider rather than treating the back of the collider as a solid wall. I am using transform.translate() for movement
I posted this problem on unity answers and was told that I should use addForce or if I did not want to do that then use Moveposition and it would fix my problem. My problem now is that I cannot get moveposition to work.
I have it written like this:
void MoveRight()
{
//this.transform.Translate(Vector2.right * Speed * Time.deltaTime);
this.GetComponent<Rigidbody2D>().MovePosition(this.transform.position + transform.right * this.Speed * Time.deltaTime);
}
Here is the full player controller:: please note that I am only using movePosition in the MoveRight() method which is what I am trying to get working.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float Speed = 2f; //Player Movement Speed;
public float JumpForce = 300f; // Fource to add to player jump (how high he goes)
private string Platform = "Desktop"; // What device is game being played on
private int Jumps = 0; // How many jumps has player made (resets to 0 when player lands, this helps with Double jumps)
public Transform GroundCheckPoint; //Empty Game object, check if it touches ground to allow jumping
public float GroundCheckRadius; // How big a radius to give GroundCheckPoint
public LayerMask GroundLayer; // Layers to allow jumping on
public bool Grounded = true; // Is player touching the ground
public bool TouchLeft = false;
public bool TouchRight = false;
public bool TouchJump = false;
public bool TouchPowerUp = false;
public bool TestMobileControls = false;
void Start()
{
if (Application.platform == RuntimePlatform.WindowsPlayer)
this.Platform = "Desktop";
else if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer)
this.Platform = "Mobile";
if (TestMobileControls)
this.Platform = "Mobile";
//If they are not playing on mobile. Hide touch controls
if(this.Platform != "Mobile")
{
GameObject[] mobileUI = GameObject.FindGameObjectsWithTag("MobileControl");
foreach (GameObject ui in mobileUI)
ui.SetActive(false);
}
}
void Update ()
{
Grounded = Physics2D.OverlapCircle(GroundCheckPoint.position, GroundCheckRadius, GroundLayer);
switch(Platform) //Change controls based on device that game is being played on
{
case "Desktop":
this.PcMove();
break;
case "Mobile":
this.MobileMove();
break;
}
}
void MoveLeft()
{
this.transform.Translate(-Vector2.right * Speed * Time.deltaTime);
}
void MoveRight()
{
//this.transform.Translate(Vector2.right * Speed * Time.deltaTime);
this.GetComponent<Rigidbody2D>().MovePosition(this.transform.position + transform.right * this.Speed * Time.deltaTime);
}
void Jump()
{
if (this.Grounded)
{ // If player is touching the ground
this.GetComponent<Rigidbody2D>().AddForce(transform.up * JumpForce);
}
}
void PcMove()
{
if (Input.GetKey(KeyCode.D))
this.MoveRight();
if (Input.GetKey(KeyCode.A))
this.MoveLeft();
if (Input.GetKeyDown(KeyCode.Space))
this.Jump();
}
void MobileMove()
{
if (this.TouchLeft)
this.MoveLeft();
if (this.TouchRight)
this.MoveRight();
if (this.TouchJump)
{
this.Jump();
this.TouchJump = false;
}
}
}
I have been searching the web but my code looks correct to me. any ideas what I am doing wrong in MovePosition() ?
Thanks in advanced
~Zero