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 CryADsisAM · Nov 30, 2013 at 07:20 PM · rotationmovementcharactercontrollerflying

Character Controller - unknown rotation, flying

I have a character. I have two seperate scripts for movement and looking around. I am using character controller.

First script makes the head move around as you look (camera is attached to the head) which means that only the head moves. Body rotates after the head has reached the shoulders... to prevent the head going all around.

Second script is meant for player movement. I want the player to move in the direction the CAMERA IS FACING - not the direction that the body is facing. I am using the following code:

 public class MovementScript : MonoBehaviour {
     
     public float speed = 5.0f;
     public float jumpVelocity = 240.0f;
     CharacterController cc;
 
     void Start()
     {
         cc = GetComponent<CharacterController>();
     }
 
     void Update(){
         GameObject cm = GameObject.Find("camera_001");
 
         Vector3 direction = cm.transform.TransformDirection(Vector3.forward) * speed;
         Vector3 forward = Input.GetAxis("Vertical") * direction;                           
         cc.Move(forward * Time.deltaTime);
 
         if (Input.GetKeyDown(KeyCode.Space) && cc.isGrounded)
         {
             gameObject.rigidbody.AddForce(Vector3.up * jumpVelocity);
         }        
     }
 }

1.problem --> the body starts spinning around nonstop after some walking around. I can't seem to find the problem that is causing this rotation.

2.problem --> the direction is Vector3 - because of that - when looking down and moving backwards the player will fly up. (Or vice versa - looking up and going forward...). I know the problem but have no idea how to fix it.

If anyone has a better idea of doing this, please tell me.

Help would be greatly appreciated. Thanks.

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 AlejandroGorgal · Nov 30, 2013 at 07:27 PM

Judging by your code, it seems like you have a rigidbody on your character, you can't have one of those AND a character controller at the same time, Unity's not going to like that and it's likely the reason why you are having these problems.

My advice would be to get rid of the character controller and stick to a rigidbody + capsule collider for simplicity.

Comment
Add comment · Show 2 · 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 CryADsisAM · Nov 30, 2013 at 07:57 PM 0
Share

I removed the controller. Now I am using only the rigidbody and a box collider that I already created. The problem now becomes the code. What should I use for movement?

avatar image AlejandroGorgal · Nov 30, 2013 at 08:18 PM 0
Share

Ok, for problem 1 you should go to the rigidbody component and freeze rotation in all 3 axis, I think that should solve that.

For the second issue, look at this documentation regarding rigidbodies in first person games:

http://pastebin.com/111UHFWR

and:

http://wiki.unity3d.com/index.php?title=RigidbodyFPSWalker

I hope that helps, good luck!

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

17 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

Related Questions

Character Controller slides sideways when it hits objects are angles different from 90 degrees 1 Answer

How do i rotate based on direction character is moving in 1 Answer

How can I move a character to a point in a straight line with CharacterController.Move while rotating him? 2 Answers

AI Character not rotating when spawned in 0 Answers

How to Logically Match Ground Slope While Using This Code? 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