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
1
Question by bardhlohaj · Jan 27, 2013 at 08:30 PM · rotationcollisionbugcharacter controllerthird person controller

Character Controller strange collision bug

Hey,

I have a character controller on my main character on a third-person controller game. I managed to control the character while moving and jumping but a colliding bug remains.

The problem is that when my character collides with other colliding objects like terrain collider or capsule collider, he immediately rotates to 90 or -90 degrees on Y axis. How can I solve this so my character gets some smooth collision and not this irritating auto-rotation?

Thanks in advance.

Comment
Add comment · Show 3
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 Anxo · Jan 27, 2013 at 09:40 PM 0
Share

this is not a bug within unity. There is something along the way of your setup that would be causing this issue. What method are you using to control the character, post code. Are there any other colliders in your character besides the charactercontroller's capsule collider.

avatar image bardhlohaj · Jan 27, 2013 at 09:52 PM 0
Share

I am using the Camera Relative Control script (I modified it but the core is the same) which uses character.$$anonymous$$ove() method to move based on joystick direction and speed. I have a box collider on the weapon but I don't think that it is causing this problem because I disabled the hole weapon and it is still happening. Can "Generate Colliders" on FBX Import Settings trigger this issue? I think I let it unchecked when I added the object on scene..

Thanks for the answer.

avatar image Anxo · Jan 27, 2013 at 09:58 PM 0
Share

No, if you left it unchecked then it would not use colliders from the FBX. Go into the example projects and and load the Camera Relative Control scene with the original script. Place a box in the scene and ad a texture to the character so you can tell if he is rotating, then run the default character from that scene into the box. If it does not happen, add your character ins$$anonymous$$d, if it still does not happe, add your control script ins$$anonymous$$d of the default one, if it happens post your code.

2 Replies

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

Answer by Anxo · Jan 27, 2013 at 11:01 PM

Oh well year, if you are always facing the direction you are moving and then moving the direction you are facing.... You will make turns. When you are colliding with a diagonal surface and you are containing to try to move forward even just one frame, you will move slightly sideways right? so if you are moving slightly sideways you will rotate into that direction if you are using your FaceMoveDirection(); function.

If you do not want to alter your script too much, change this variable to a higher number and see. Otherwise you might want to consider a different approach.

if ( horizontalVelocity.magnitude > 0.1 )

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 bardhlohaj · Jan 27, 2013 at 11:07 PM 0
Share

Thanks a lot for clearing this up for me. No, changing the variable doesn't work for my case so what would you suggest as the best approach?

avatar image Anxo · Jan 27, 2013 at 11:11 PM 0
Share

Well I am not sure what your game is about but if you need it to face direction only when you are telling it to change direction then you can add a bool that checks if the left right arrow are pressed before it applies face direction. It would be very relative to your project.

avatar image bardhlohaj · Jan 27, 2013 at 11:21 PM 0
Share

I don't think it will solve the problem in my game. The issue is that my character is going to do some melee combat which means that it will collide with enemies while running, changing direction and attacking. How can I synchronize the characters direction with left joystick position so I change characters direction only when the joystick has changed the position? How would I calculate this direction?

I think this will solve the problem because I won't change the direction of my character just because the character controller is going to move on a different direction (because of the collision)..

avatar image Anxo · Jan 27, 2013 at 11:29 PM 0
Share

You are getting the X and Y intake of the left Joystick right? You can take the X value and say

 if(xvalue > number || xvalue < -number){
 {
       facedirection code
 }

That way it will only rotate the character when the joystick is moved left or right.

avatar image bardhlohaj · Jan 27, 2013 at 11:34 PM 0
Share

Yes, I have moveJoystick.position.x and moveJoystick.position.y but how do I calculate the direction that the character needs to be. Lets say that moveJoystick.position.x is -0.5 (left), and I have thisTransform.forward which represents the forward direction of the character. How do I calculate the left direction of the character based on moveJoystick.position.x?

Show more comments
avatar image
0

Answer by bardhlohaj · Jan 27, 2013 at 10:54 PM

I've been testing until now and as far as I can see the problem is FaceMovementDirection() that is causing this.

I simplified my script for faster approach:

 #pragma strict
 
 // This script must be attached to a GameObject that has a CharacterController
 @script RequireComponent( CharacterController )
 
 var moveJoystick : EasyMobileJoystick;
 var rotateJoystick : EasyMobileJoystick;
 
 var cameraPivot : Transform;                        // The transform used for camera rotation
 var cameraTransform : Transform;                    // The actual transform of the camera
 
 var speed : float = 5;                                // Ground speed
 var jumpSpeed : float = 8;
 var inAirMultiplier : float = 0.25;                 // Limiter for ground speed while jumping
 var rotationSpeed : Vector2 = Vector2( 50, 25 );    // Camera rotation speed for each axis
 
 private var thisTransform : Transform;
 private var character : CharacterController;
 private var velocity : Vector3;                        // Used for continuing momentum while in air
 private var canJump = true;
 
 function Start()
 {
     // Cache component lookup at startup instead of doing this every frame    
     thisTransform = GetComponent( Transform );
     character = GetComponent( CharacterController );    
     
     // Move the character to the correct start position in the level, if one exists
     var spawn = GameObject.Find( "PlayerSpawn" );
     if ( spawn )
         thisTransform.position = spawn.transform.position;    
 }
 
 function FaceMovementDirection()
 {    
     var horizontalVelocity : Vector3 = character.velocity;
     horizontalVelocity.y = 0; // Ignore vertical movement
     
     // If moving significantly in a new direction, point that character in that direction
     if ( horizontalVelocity.magnitude > 0.1 )
         thisTransform.forward = horizontalVelocity.normalized;
 }
 
 function OnEndGame()
 {
     // Disable joystick when the game ends    
     moveJoystick.Disable();
     rotateJoystick.Disable();
     
     // Don't allow any more control changes when the game ends
     this.enabled = false;
 }
 
 function Update()
 {
     var movement = cameraTransform.TransformDirection( Vector3( moveJoystick.position.x, 0, moveJoystick.position.y ) );
     // We only want the camera-space horizontal direction
     movement.y = 0;
     movement.Normalize(); // Adjust magnitude after ignoring vertical movement
     
     // Let's use the largest component of the joystick position for the speed.
     var absJoyPos = Vector2( Mathf.Abs( moveJoystick.position.x ), Mathf.Abs( moveJoystick.position.y ) );
     movement *= speed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
     
     // Check for jump
     if ( character.isGrounded )
     {
     
     }
     else
     {            
         // Apply gravity to our velocity to diminish it over time
         velocity.y += Physics.gravity.y * Time.deltaTime;
         
         // Adjust additional movement while in-air
         movement.x *= inAirMultiplier;
         movement.z *= inAirMultiplier;
     }
     
     movement += velocity;
     movement += Physics.gravity;
     movement *= Time.deltaTime;
     
     // Actually move the character
     character.Move( movement );
     
     if ( character.isGrounded )
         // Remove any persistent velocity after landing
         velocity = Vector3.zero;
     
     // Face the character to match with where she is moving
     FaceMovementDirection();    
     
     // Scale joystick input with rotation speed
     var camRotation = rotateJoystick.position;
     camRotation.x *= rotationSpeed.x;
     camRotation.y *= rotationSpeed.y;
     camRotation *= Time.deltaTime;
     
     // Rotate around the character horizontally in world, but use local space
     // for vertical rotation
     cameraPivot.Rotate( 0, camRotation.x, 0, Space.World );
     cameraPivot.Rotate( camRotation.y, 0, 0 );
 }
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

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

Rotate a rigidbody that has frozen rotation 1 Answer

Unity Asset Prefab Bug ThirdPersonController (left rotating bug / Snapping forward Always) 3 Answers

How do I stop my enemy from falling over? 3 Answers

Delayed Collisions Bug continued... 0 Answers

Player Rotating after Collision... 0 Answers


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