• 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
Question by RaiderA528 · Sep 23, 2011 at 11:57 PM · colliderontriggerenterspeedjavaslow

OnTriggerEnter (Slow Player Speed) Need Help!

Okay so I'm trying to code an OnTriggerEnter event for a "SlowingSludge" of sorts, so I have my own CharacterController script called "Character_Controls" and I have a plane with a sludge diffuse texture and a box collider attached thats set as a trigger but I dont know where to begin on referencing my walkspeed variable and lowering the float value from my controller when the player enters. (Feel free to use my controller code if you like) Thanks!!!

private var walkSpeed : float = 0.5;

private var gravity = 50.0; //New variable for code involving gravity private var moveDirection : Vector3 = Vector3.zero; private var charController : CharacterController;

private var strength = 0.5; //New variable for code involing objects the player collides with

//function that enables the keyboard cursors to transform the player direction + play associated annimations function Start(){ charController = GetComponent(CharacterController); }

function Update () { if(charController.isGrounded == true) //using t$$anonymous$$s code, the character animations should only play w$$anonymous$$le the character is grounded { if(Input.GetAxis("Vertical") > .1)//input = upward cursor key or W key { walkSpeed = 0.6; } if(Input.GetAxis("Vertical") < -.1)//input = downward cursor key or S key { walkSpeed = 0.5; } transform.eulerAngles.y += Input.GetAxis("Horizontal"); moveDirection = Vector3(0,0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); } moveDirection.y -= gravity * Time.deltaTime; charController.Move(moveDirection*walkSpeed); }

function OnControllerColliderHit ($$anonymous$$t : ControllerColliderHit) //moves any game object with a rigidbody+collider component based on the strength variable { var body : Rigidbody = $$anonymous$$t.collider.attachedRigidbody; var pushDir = Vector3 ($$anonymous$$t.moveDirection.x, 0, $$anonymous$$t.moveDirection.z);

 body.velocity = pushDir*strength*10;

}

Comment

People who like this

0 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by aldonaletto · Sep 24, 2011 at 12:56 AM

You could place OnTriggerEnter/Exit functions in your character script - triggers call t$$anonymous$$s function in the trigger script and in the object that entered the trigger:

function OnTriggerEnter(col: Collider){ walkSpeed = 0.3; // slow down when entering }

function OnTriggerExit(col: Collider){ walkSpeed = 0.5; // restore speed when exiting } NOTE: You should check in your function OnControllerColliderHit if the object has a rigidbody before using it to avoid Null Reference errors at runtime:

    var body : Rigidbody = $$anonymous$$t.collider.attachedRigidbody;
    if (body){
        var pushDir = Vector3 ($$anonymous$$t.moveDirection.x, 0, $$anonymous$$t.moveDirection.z);
        body.velocity = pushDir*strength*10;
    }
Comment

People who like this

0 Show 0 · 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

Answer by RaiderA528 · Sep 24, 2011 at 03:49 PM

Thank you very much, I'm still having a little bit of trouble with the slowing sludge, it just refuses to work, but I t$$anonymous$$nk that's just my mac$$anonymous$$ne or the way my code is layed out, t$$anonymous$$nk I'll need to use tags so only specific colliders that the code can call on so it will slow down. But I was bewildered by that Null reference error so thank you very much. : ) the reply time was fantastic, I'll be on here more often. :P

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Increase speed of object that enters OnTriggerEnter 1 Answer

Ball getting slow down in collision 0 Answers

The ball getting slow down and slicing. 0 Answers

Character collider falls away unless "isTrigger" selected 1 Answer

How to make gameObject make only one collision 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