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 fjalla · Nov 08, 2012 at 01:11 PM · rigidbodycharactercontrollercharactermotorconstant force

Applying constant force to charcter when it goes inside a trigger

I've been searching the web to find an answer to this one for ages. What I'm trying to do: when my character goes inside a trigger, I want him to go in a certain direction (let's say up), no matter what direction he moves in (well, he can alter it a bit, but he can't stop going in the direction of the force, like if someone upturned the gravity), but when he comes out of the trigger, the force will stop (like if someone put the gravity normal again, you would still have the speed you were going in). I don't want the triggers to move each other, but only to move objects with a rigidbody and the character. Can anyone help? Please. I'm using Character Controller and Character Motor on my character.

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

1 Reply

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

Answer by Seth-Bergman · Nov 08, 2012 at 01:49 PM

http://docs.unity3d.com/Documentation/ScriptReference/CharacterController.Move.html

since you are not providing your movement code, I will use the example code:

 var speed : float = 6.0;
 var jumpSpeed : float = 8.0;
 var gravity : float = 20.0;
 var upwardSpeed : float = 5.0;  
 var inTriggerZone : boolean;
 
 private var moveDirection : Vector3 = Vector3.zero;
 
 function Update() {
     var controller : CharacterController = GetComponent(CharacterController);
     if (controller.isGrounded  || inTriggerZone) {
         // We are grounded, so recalculate
         // move direction directly from axes
         moveDirection = Vector3(Input.GetAxis("Horizontal"), 0,
                                 Input.GetAxis("Vertical"));
         moveDirection = transform.TransformDirection(moveDirection);
         moveDirection *= speed;
         
         if (Input.GetButton ("Jump")) {
             moveDirection.y = jumpSpeed;
         }
     }
 
     // Apply gravity
     moveDirection.y -= gravity * Time.deltaTime;
 
     //APPLY UPWARD CONSTANT MOVEMENT
     if(inTriggerZone)
         moveDirection.y = upwardSpeed;
  
     // Move the controller
     controller.Move(moveDirection * Time.deltaTime);
 }
 
 function OnTriggerEnter(){
 inTriggerZone = true;
 }
 
 function OnTriggerExit(){
 inTriggerZone = false;
 }

this should give you an idea

EDIT: TO ROTATE THE PLAYER.. let's assume the above script is named "CharMove"

add this second script to the same player object:

 private var target : float;
 private var moveScript : CharMove;    // ADJUST SCRIPT NAME HERE
 var rotateSpeed : int = 4;
 
 function Start(){
 moveScript = GetComponent(CharMove);    // AND HERE
 }
 
 function FixedUpdate () {
 
 var up = moveScript.inTriggerZone;
 
 if(up){
    if(target < 180){
        transform.Rotate(Vector3.right * rotateSpeed );
        target += rotateSpeed ;
    }
    else{
        transform.Rotate(-Vector3.right * (target - 180));  
        target -= (target - 180);  
     }
 }
 else{
      if(target > 0){
        transform.Rotate(-Vector3.right * rotateSpeed );
        target -= rotateSpeed ;
      }
       else{
  transform.Rotate(Vector3.right * -target);
  target += -target;
     }
   }
 }

Hope this helps! :) (there is surely a better way to do this part, or at least this can be improved.. but I'm not thinking of it just now.. The rotation here will not reset quite perfectly.. but it's pretty close..)

Comment
Add comment · Show 9 · 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 fjalla · Nov 08, 2012 at 02:53 PM 0
Share

It doesn't work :( It finds out that it's ina trigger but it doesn't move :(

avatar image Seth-Bergman · Nov 08, 2012 at 02:57 PM 0
Share

are you using another movement script?

This example is a modification of the script found at the link at the top of my answer.

IF that is the movement script you attach to your player, it will work.. CLEARLY, since my code is identical to the jump code already contained therein..

avatar image fjalla · Nov 11, 2012 at 06:13 AM 0
Share

Just tried the script. It works, now only I have to implement the part into my Character$$anonymous$$otor :D Btw, could you make it to work ins$$anonymous$$d pushing the player upward that it would change the gravity so that it would be upside-down?

avatar image Seth-Bergman · Nov 11, 2012 at 08:42 AM 0
Share

Unfortunately I don't know the first thing about Character$$anonymous$$otor, I will need to look into that!

Now, if you mean just the player would be upside down (like so they can walk on the ceiling, for example), you would simply need to rotate the player.. There may be a simpler way than $$anonymous$$e, but I will update my answer to show you a way..

Also, if this or any other answer is satisfactory to you, please accept it by clicking the check mark, thanks!

(answer updated)

If you actually want to change the gravity for all rigidbodies as well, that should be easy enough, just give Physics.gravity a positive y value:

http://docs.unity3d.com/Documentation/ScriptReference/Physics-gravity.html

avatar image fjalla · Nov 13, 2012 at 12:49 PM 0
Share

$$anonymous$$an, it's like AL$$anonymous$$OST. The problem is that sometimes it rotates the camera into wierd angles and you can't walk on the ceiling :( But ins$$anonymous$$d of that, it's perfect! :D

Show more comments

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

10 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

Related Questions

Can't use Rigidbody or Character Controller 0 Answers

Stop objects from going through each other with rigidbodies 1 Answer

How Character controller can push another character controller? 0 Answers

Question about pushing objects, "Animate Physics" and Rigidbodies 0 Answers

Which is more demanding on a computer, lots of CCcharacters or lots of Rigidcharacters? 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