Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by YouZaDoneski · Apr 13, 2013 at 03:29 AM · collisionontriggerenterspeedplane

Can I instantiate a collision with a plane?

Ok,

I'll make this simple:

I am making a simple 2d driving based runner clone. I want to change the speed of my player vehicle when it hits a hazard. The hazard is a plane that is on the same Y position as the vehicle. Im using OnTriggerEnter to make it work, but so far no luck. I'm not sure if it is a problem in the editor or in the code. Ill post the code below but basically when the player hits the hazard it will slow down by a float rate. If anyone could figure this out, I would be most appreciative.

Here is the player script, the part that doesn't work is near the bottom.

 using UnityEngine;
 using System.Collections;
 
 public class sPlayer : MonoBehaviour {
     
     public float speed = 30;
     public float maxspeed = 40;
     public float minspeed = 20;
     public float accelrate = .099f;
     public float crashslowrate = -5f;
     
     public AudioClip EngineLoop;
     public AudioClip VanAccelSound;
     
 
     // Use this for initialization
 void Start ()
     {
         
     }
     
     // Update is called once per frame
     void Update () 
     {
         
         
         bool applyDefault = true;
         
         //if (Input.GetKey (KeyCode.D) == true)
         //{
             //AudioSource.PlayClipAtPoint(VanAccelSound, Camera.mainCamera.transform.position);
             
             //transform.Translate(transform.right * maxspeed * Time.deltaTime);
             
             //applyDefault = false;
         //}
         
         if (Input.GetKey (KeyCode.W) == true)
         {
             
             
             transform.Translate (Vector3.forward * -3f);
         }
         
         if(Input.GetKey (KeyCode.S)==true)
         {
             
             
             transform.Translate (Vector3.forward * 3f);        
             
         }
         
         //if(Input.GetKey (KeyCode.A)==true)
         //{
             
             
             //transform.Translate(transform.right * minspeed * Time.deltaTime);
         
             //applyDefault = false;
         //}
         
         if(applyDefault)
         {
             AudioSource.PlayClipAtPoint(EngineLoop, Camera.mainCamera.transform.position);
                 
             transform.Translate(transform.right * speed * Time.deltaTime);
             
             speed = speed += accelrate;
             
         }
             
         
     }
     void OnTriggerEnter(Collision crashslow)
     {
         if(crashslow.gameObject.tag == "Hazards")
         {
             speed = speed += crashslowrate;
         }    
     }
     
     
     
     
 
 }





 
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 syclamoth · Apr 13, 2013 at 06:12 AM 0
Share

You seem to be using this idiom quite a lot:

 variable = variable += increment;

Are you sure that's what you want to do? The += operator gets expanded into this:

 variable = variable + increment;

which means that your line actually looks like this:

 variable = variable = variable + increment

While the result of that line is no different from the simpler version, it would be clearer if you simply used either

 variable += increment;

or

 variable = variable + increment;

not both.

avatar image whydoidoit · Apr 14, 2013 at 08:41 AM 0
Share

You say you are using a "Plane" why would you do that? I would have thought a box collider was much more likely to work - a plane will be one directional and easily passed through by a fast moving object.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Chronos-L · Apr 13, 2013 at 04:31 AM

Your code:

 //Incorrect, will not be called even if you
 //setup the trigger mechanism correctly
 void OnTriggerEnter(Collision crashslow) {
    ...
 }

Correction:

 void OnTriggerEnter( Collider other ) {
    ...
 }

Read the collision matrix table in this page to make sure that you have setup your trigger mechanism correctly.

Comment
Add comment · Show 8 · 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 Chronos-L · Apr 13, 2013 at 06:06 AM 1
Share

Note the parameter type, OnTrigger*() functions have a Collider as their parameter, while OnCollision*() have Collision

avatar image GibTreaty · Apr 13, 2013 at 08:49 PM 0
Share

And the name of the variable does not matter, only the type which in this case is Collider. So it could be (Collider whatchamacallit) and it would still be fine.

avatar image Chronos-L · Apr 14, 2013 at 12:31 AM 0
Share

However, you need to make sure not to use the same variable name as the $$anonymous$$onoBehaviour you are inheriting from, a typical mistake would be na$$anonymous$$g a Collider for the trigger collider: void OnTriggerEnter( Collider collider ), but that will conflict with the collider in the $$anonymous$$onoBehaviour

avatar image GibTreaty · Apr 14, 2013 at 07:25 AM 1
Share

It shouldn't conflict at all, at least not in C#. If you use collider as the parameter and you want to access the $$anonymous$$onoBehaviour's collider variable then you just put this.collider.

avatar image whydoidoit · Apr 14, 2013 at 08:41 AM 1
Share

I knew us English speakers had it rough :) Just think of our limits on variable names - which is why most of $$anonymous$$e look like:

  somethingThatIsActuallyQuiteImportant += 1;
Show more comments
avatar image
0

Answer by GibTreaty · Apr 13, 2013 at 05:59 AM

If Chronos-L's solution doesn't completely fix the problem then...

Try adding a rigidbody to the trigger object. If that doesn't work then set the rigidbody to Continuous or Continuous Dynamic. You would only need a rigidbody on the trigger object if it's actually moving. If your vehicle is moving and doesn't have a rigidbody, then add one to it.

Comment
Add comment · 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

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

14 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

Related Questions

AnimationEvent, Collider, OnTriggerEnter logic? 0 Answers

Whats wrong with this ladder climb script? 1 Answer

Delayed Collisions Bug (includes video demonstration) 1 Answer

How Can One Collider Recognize Contact With Another? 0 Answers

Time release Collectable 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