- Home /
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!
@hypnoticmeteor But everything else works fine with $$anonymous$$oveRotation, why is just one direction busted?
Well you can do it anyway but when you use a rigidbody you should use addTorque() addForce() addRelativeForce(). It is realistic and accurate physics.
Try the character controller in Unity $$anonymous$$obile Assets.
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.
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.
Of course it depends on how you're animator is setup.
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.
O$$anonymous$$ well the animator only works when on the ground so this is how I rotate when swim$$anonymous$$g, 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 swim$$anonymous$$g rotation working!
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
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!
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. :¬)
Your answer
Follow this Question
Related Questions
My Objects are falling through the floor.. 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
In need of help with OnTriggerEnter 0 Answers
How do I control the speed of a rotating rigidbody? 0 Answers