- Home /
Question by
Alfredo_bahena123 · Feb 02, 2018 at 02:56 AM ·
playermovement script
I am trying to make an objective to go to the next level but there is an issue
Im using this code to move my character in a crossy road style game
void Update () {
if (canmove == true)
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Vector3 fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(transform.position, fwd, 3f))
{
print("There is something in front of the object!");
}
else
{
canmove = false;
z = movespeed;
Move();
z = 0;
}
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Vector3 down = transform.TransformDirection(-Vector3.forward);
if (Physics.Raycast(transform.position, down, 3f))
{
print("There is something in front of the object!");
}
else
{
canmove = false;
z = -movespeed;
Move();
z = 0;
}
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
Vector3 left = transform.TransformDirection(Vector3.left);
if (Physics.Raycast(transform.position, left, 3f))
{
print("There is something in front of the object!");
}
else
{
canmove = false;
x = -movespeed;
Move();
x = 0;
}
}
if (Input.GetKeyDown(KeyCode.RightArrow))
{
Vector3 right = transform.TransformDirection(Vector3.right);
if (Physics.Raycast(transform.position, right, 3f))
{
print("There is something in front of the object!");
}
else
{
canmove = false;
x = movespeed;
Move();
x = 0;
}
}
}
}
public void FixedUpdate()
{
timer--;
if(timer == 0)
{
canmove = true;
}
if(timer < 0)
{
timer = 20;
}
}
private void Move ()
{
transform.Translate(new Vector3(x, 0, z));
}
The issue im facing is the objective has to have a collider on it for collision to work and go to the next level but if it has a collider then the player can not move onto the square with the objective on it.
Here is the code for the endlevel objective
// Update is called once per frame
void Update () {
}
public void OnTriggerEnter(Collider other)
{
Scene scene = SceneManager.GetActiveScene();
if(scene.name == "Level1")
{
SceneManager.LoadScene("Level2");
}
}
Comment
Answer by melsy · Feb 02, 2018 at 05:09 AM
Set the collider to isTrigger. Then the player can move into and the OnTriggerEnter() call will work correctly
Your answer
Follow this Question
Related Questions
Player movement boudaries in 2D 1 Answer
Need help setting up Simple_Swordman by Black Hammer 0 Answers
Manual Player Physics Help (C#) 0 Answers