• 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 JTemple · Jan 10, 2015 at 01:05 PM · c#rigidbodyfalling-through-floor

Problem with rotating a rigidbody

Hello!

This is my first time posting here, sorry for any mistakes I make.

I have a problem with my C# script. I'm no programmer, unfortunately, I only know the basics I learned myself. I wrote a simple script to move my guy about and it works great, he walks, he runs, the turning is smooth... until I try to move him back. On both gamepad analog stick and the WASD keys as soon as I try to rotate and move the character towards me, he flips upside-down and falls through the floor. I have looked online and re-wrote the script a dozen times but it only made it worse.

Edit: Just wanted to clarify that my camera is old zelda-like, suspended above the character and the character moves in the direction the analog/WASDies point.

Here's the code (sorry if it didn't format properly, I've tried):

 using UnityEngine;
 using System.Collections;
 
 public class CharacterControl : MonoBehaviour {
 
     float xTilt;
     float yTilt;
 
     public Animator animator;
     public float speed = 0.0f;
     public Vector3 targetDirection;
     public Transform mcamera;
 
     // Use this for initialization
 
     void Start () {
         animator = GetComponent<Animator>();
         mcamera = GameObject.FindGameObjectWithTag("MainCamera").transform;
     }
 
     
 
 
     // Update is called once per frame
     void Update () {
     
     }
 
     void FixedUpdate (){
 
         xTilt = Input.GetAxis("Horizontal");
         yTilt = Input.GetAxis("Vertical");
     
     //rotation maths
             if (xTilt != 0 || yTilt != 0){
 
             Vector3 currentDirection = rigidbody.transform.forward;
             targetDirection = (xTilt * rigidbody.transform.right + yTilt * rigidbody.transform.forward.normalized);
 
 
             Debug.DrawRay(rigidbody.transform.position, currentDirection, Color.green, 20f);
             Debug.DrawRay(rigidbody.transform.position, targetDirection, Color.red, 15f);
 
 
             
 
             rigidbody.MoveRotation(Quaternion.FromToRotation(currentDirection, targetDirection));
             
 
             
         //animation control
             speed = targetDirection.magnitude;
             animator.SetFloat("Speed", speed);
 
 
             
         }else animator.SetFloat("Speed", 0.0f);
 
         if(Input.GetButton("Crouch")){
             animator.SetBool("Crouch", true);
         }
         else animator.SetBool("Crouch", false);
     }
 }

If anyone could tell me what am I doing wrong here, so I can learn for the future, I'd appreciate it greatly.

Thanks in advance for any help!

Comment
Add comment · Show 5
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 hypnoticmeteor · Jan 10, 2015 at 01:07 PM 0
Share

To rotate a rigidbody use addtorque()

avatar image JTemple · Jan 10, 2015 at 01:33 PM 0
Share

@hypnoticmeteor But everything else works fine with $$anonymous$$oveRotation, why is just one direction busted?

avatar image hypnoticmeteor · Jan 10, 2015 at 01:51 PM 0
Share

Well you can do it anyway but when you use a rigidbody you should use addTorque() addForce() addRelativeForce(). It is realistic and accurate physics.

avatar image hypnoticmeteor · Jan 10, 2015 at 02:00 PM 0
Share

Try the character controller in Unity $$anonymous$$obile Assets.

avatar image JTemple · Jan 10, 2015 at 02:54 PM 0
Share

I'm trying to stay away from pre-made solutions. I won't learn anything by using stuff someone else made and I'm already using animations from beta sample pack.

1 Reply

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

Answer by Mmmpies · Jan 10, 2015 at 02:15 PM

Shouldn't the animator control the AngularSpeed as well as Speed?

Mine does like this:

 //In fixedUpdate
 float h = Input.GetAxis ("Horizontal");
 // lots of other variables here but only h is need for your question
     
 MovementManagement(h, v, su, sd, boo, ad, aup);
     
 // then in MovementManagement
 void MovementManagement(float horizontal, float vertical, bool swimUp, bool swimDown, bool bladeOO, bool AttackButDown, bool AttackButUp)
     
 anim.SetFloat("AngularSpeed", horizontal * 3);

my player animator then handles it in the same way as it handles Speed.

PlayerAnimator

Of course it depends on how you're animator is setup.


playeranimator.png (47.4 kB)
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 JTemple · Jan 10, 2015 at 02:43 PM 0
Share

my animator only does walking forward. I think I can find animations that do the tight turns I need, but still, the rotation with rigidbody works well in all directions other that down and I don't know why. I thought solving that problem could help me understand coding better.

avatar image Mmmpies · Jan 10, 2015 at 02:51 PM 0
Share

O$$anonymous$$ well the animator only works when on the ground so this is how I rotate when swimming, the script is attached to the player:

 hero = GetComponent<Transform>();

 hero.Rotate (0, horizontal * Time.fixedDeltaTime * 3 * 55, 0, Space.Self);  

Yes that could just be ( 165) not ( 3 * 55) I think it was pretty late when I got the swimming rotation working!

avatar image Mmmpies · Jan 10, 2015 at 03:36 PM 1
Share

No problem,

look at the movement shown in the answer to this question:

[ZeldaCharacterController][1]

That's probably more what you're after. [1]: http://answers.unity3d.com/questions/559439/topdown-zelda-esque-character-controller-help-c.html

avatar image JTemple · Jan 10, 2015 at 09:50 PM 1
Share

Thank you! After taking some lines from that and modifying them a bit it works! There's still some problems, but the main issue is solved!

avatar image Mmmpies · Jan 10, 2015 at 10:07 PM 1
Share

Again no problem,

In future, no matter how obscure you think your problem is, it's probably not, Unity has been around for quite a few years so worth searching previous answers even if those are specific like "Zelda style controller".

If it solved your problem feel free to click correct answer, or at least click thumbs up to the bit that lead you to the answer.

Hmmm, feel I'm being a bit dry and serious about this but really, just glad it helped. :¬)

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

My Objects are falling through the floor.. 0 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Strange/Buggy code behavier 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges