Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Kek_Kek · Jan 12, 2018 at 01:56 AM · movementtriggerif-statements

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");
         }
 
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brogan89 · Jan 12, 2018 at 02:10 AM 0
Share

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

avatar image Brogan89 Brogan89 · Jan 12, 2018 at 02:14 AM 0
Share

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)


1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kek_Kek · Jan 12, 2018 at 03:12 AM 0
Share

Im getting an error that the name 'straffe' (17,29) and 'translation' (17,41) don't exist in the current context

avatar image Brogan89 · Jan 12, 2018 at 05:53 AM 0
Share

oh sorry, put the line transform.Translate(straffe, 0, translation); from Update() into` $$anonymous$$ovePlayer()`

avatar image Kek_Kek Brogan89 · Jan 13, 2018 at 03:07 PM 0
Share

Thank you so much the script works now.

avatar image Bunny83 Brogan89 · Jan 13, 2018 at 03:19 PM 0
Share

Uhm, you should edit your answer...

avatar image ransomink · Jan 13, 2018 at 04:32 PM 1
Share

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...

avatar image Brogan89 ransomink · Jan 13, 2018 at 11:11 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges