- Home /
How to make a limit so that my character stays on the screen?
so, i make a 3d endless runner game for mobile and I got a little problem. my character move to right and left just fine, but if I keep move to the right or left, it makes the char go outside the screen. I try to fix it using an invisible wall, but it's not the best option for me. can anyone help me solved it? i think I need to adjust my pathIndex variable, but I don't know how. I'm still very new at coding. thx
public int pathIndex = 0; public float turnRate = 2.5f; public float pathSpacing = 0.9f; float targetX; public bool isGrounded; [Range(0, 2.0f)] float m_groundLevel;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (transform.position.x < targetX)
{
transform.position += turnRate * Time.deltaTime * Vector3.right;
if (transform.position.x > targetX)
{
transform.position = new Vector3(targetX, transform.position.y, 0);
}
}
else if (transform.position.x > targetX)
{
transform.position += turnRate * Time.deltaTime * Vector3.left;
if (transform.position.x < targetX)
{
transform.position = new Vector3(targetX, transform.position.y, 0);
}
}
}
public void Jump()
{
if(transform.position.y > m_groundLevel) return;
GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpPower, 0));
}
public void Turn(int direction)
{
pathIndex += direction;
targetX = pathIndex * pathSpacing;
}
}
Comment
Your answer
Follow this Question
Related Questions
Terrain into Model 0 Answers
How do I know if a 3d model is Mobile Friendly? 2 Answers
Joystick movement problems 0 Answers