How to cancel a if statement once touching a collider
I created a walk script where it starts off on an original speed which is 10. I created a couple of if statements where if the player walks into Trigger number one, his speed becomes faster. I tried testing it but once the player enters the first trigger, he keeps his original speed. Is there any way to cancel the previous if statements?
Move Script: {
public float speed = 10.0F;
public float speed1 = 10.0F;
public float speed2 = 20.0F;
public float speed3 = 30.0F;
public float speed4 = 40.0F;
public float speed5 = 50.0F;
void Start()
{
}
// Update is called once per frame
public void Update()
{
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate(straffe, 0, translation);
}
public void OnTriggerEnter(Collider col)
{
if(col.name == "Change Speed Trigger 0")
{
float translation = Input.GetAxis("Vertical") * speed1;
float straffe = Input.GetAxis("Horizontal") * speed1;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
Debug.Log("Speed 1");
}
if (col.name == "Change Speed Trigger")
{
float translation = Input.GetAxis("Vertical") * speed2;
float straffe = Input.GetAxis("Horizontal") * speed2;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
Debug.Log("Speed 2");
}
if (col.name == "Change Speed Trigger 2")
{
float translation = Input.GetAxis("Vertical") * speed3;
float straffe = Input.GetAxis("Horizontal") * speed3;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
Debug.Log("Speed 3");
}
if (col.name == "Change Speed Trigger 3")
{
float translation = Input.GetAxis("Vertical") * speed4;
float straffe = Input.GetAxis("Horizontal") * speed4;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
Debug.Log("Speed 4");
}
if (col.name == "Change Speed Trigger 4")
{
float translation = Input.GetAxis("Vertical") * speed5;
float straffe = Input.GetAxis("Horizontal") * speed5;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
Debug.Log("Speed 5");
}
}
}
I think you want to change your 2nd IF statement to be if (col.name == "Change Speed Trigger 1")
you forgot the number in the string. This might be your problem
Also you should use a function for the code inside the if statements. It's a better coding practice.
void $$anonymous$$ovePlayer(float speed)
{
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
Debug.Log("Speed = " + speed);
}
then you can do
if(col.name == "Change Speed Trigger 0")
$$anonymous$$odePlayer(speed0)
Answer by Brogan89 · Jan 12, 2018 at 02:22 AM
I think the issue is that you are changing the speed in update which is every frame but OnTriggerEnter is a called once event, so you will be moving your player faster but then it will return to speed
the very next frame...
this is what you should do
public float speed = 10.0F;
public float speed1 = 10.0F;
public float speed2 = 20.0F;
public float speed3 = 30.0F;
public float speed4 = 40.0F;
public float speed5 = 50.0F;
public void Update()
{
MovePlayer(speed);
}
public void OnTriggerEnter(Collider col)
{
if(col.name == "Change Speed Trigger 0")
speed = speed1;
else if (col.name == "Change Speed Trigger 1")
speed = speed2;
else if (col.name == "Change Speed Trigger 2")
speed = speed3;
else if (col.name == "Change Speed Trigger 3")
speed = speed4;
else if (col.name == "Change Speed Trigger 4")
speed = speed5;
}
void MovePlayer(float speed)
{
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
Debug.Log("Speed = " + speed);
transform.Translate(straffe, 0, translation);
}
hope this helps
Im getting an error that the name 'straffe' (17,29) and 'translation' (17,41) don't exist in the current context
oh sorry, put the line transform.Translate(straffe, 0, translation);
from Update()
into` $$anonymous$$ovePlayer()`
I'd use a switch ins$$anonymous$$d of all the if statements.
Also, I think it would be more efficient to create a script that changes the player's speed when its trigger is entered so you don't have to check against all speed triggers in your $$anonymous$$oveScript. Especially, if you add more...
This is true. I thought about doing a switch statement but I didn't want to throw too much at them, its obvious darthvader is just learning.
@DarthVader1234 Having another script on the "Change Speed Trigger" objects in question would actually be a far better approach as @ranso$$anonymous$$k mentioned. This would mean your Player script doesn't need to know anything about the triggers. Which it technically shouldn't. It also allows you to have many more triggers without adding any more addition code. You can just add the script and change the speed variable to suit. So you player script would just contain the actually moving logic like so:
public class Player$$anonymous$$ovement: $$anonymous$$onoBehaviour
{
public float speed;
void Update()
{
float translation = Input.GetAxis("Vertical") * speed;
float straffe = Input.GetAxis("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
Debug.Log("Speed = " + speed);
transform.Translate(straffe, 0, translation);
}
}
The your trigger scripts will:
public class ChangeSpeed: $$anonymous$$onoBehaviour
{
public float newSpeed;
public void OnTriggerEnter(Collider col)
{
if (col.CompareTag("Player"))
{
Player$$anonymous$$ovement player = col.GetComponent<Player$$anonymous$$ovement>();
player.speed = newSpeed;
}
}
}
Note you will need to tag your player gameobject as "Player". Then just drag the change speed script onto all the objects which have the trigger, set the speed variable and BOO$$anonymous$$! You're done. Now you can have 100 triggers in a scene and you don't have to write any more code to do so :)
This would be a better practice. (Which should always be the goal)
Your answer
Follow this Question
Related Questions
Adding time to game clock when item picked up? 0 Answers
Linking Trigger to Input (or directly into Character Control script) 0 Answers
How to move an object on a moving platform with the platform 0 Answers
Stepping on game object moves quickly to react 0 Answers
What is the best way to script a trigger that moves a object from point a to point b in C# (unity 5) 1 Answer