Imported 3D Model doesn't react to Rigidbody Movement Script
I'm lost as to what's happening because this same script works on a 3D capsule, but won't with my imported .fbx model. Also I can't use a character controller because then I can't get physics on my character. I'd appreciate any ideas on how I could fix this problem. If I need to add anything else to make my problem more clear I'd be happy to.
using System.Collections.Generic;
using UnityEngine;
public class RigidbodyCharacter : MonoBehaviour
{
Rigidbody rb;
public Transform LookTransform;
public Vector3 Gravity = Vector3.down * 9.81f;
public float RotationRate = 0.1f;
public float Velocity = 8;
public float GroundControl = 1.0f;
public float AirControl = 0.2f;
public float JumpVelocity = 5;
public float GroundHeight = 1.1f;
private bool jump;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true;
rb.useGravity = false;
}
void Update()
{
jump = jump || Input.GetButtonDown("Jump");
}
void FixedUpdate()
{
// Cast a ray towards the ground to see if the Walker is grounded
bool grounded = Physics.Raycast(transform.position, Gravity.normalized, GroundHeight);
// Rotate the body to stay upright
Vector3 gravityForward = Vector3.Cross(Gravity, transform.right);
Quaternion targetRotation = Quaternion.LookRotation(gravityForward, -Gravity);
rb.rotation = Quaternion.Lerp(rb.rotation, targetRotation, RotationRate);
// Add velocity change for movement on the local horizontal plane
Vector3 forward = Vector3.Cross(transform.up, -LookTransform.right).normalized;
Vector3 right = Vector3.Cross(transform.up, LookTransform.forward).normalized;
Vector3 targetVelocity = (forward * Input.GetAxis("Vertical") + right * Input.GetAxis("Horizontal")) * Velocity;
Vector3 localVelocity = transform.InverseTransformDirection(rb.velocity);
Vector3 velocityChange = transform.InverseTransformDirection(targetVelocity) - localVelocity;
// The velocity change is clamped to the control velocity
// The vertical component is either removed or set to result in the absolute jump velocity
velocityChange = Vector3.ClampMagnitude(velocityChange, grounded ? GroundControl : AirControl);
velocityChange.y = jump && grounded ? -localVelocity.y + JumpVelocity : 0;
velocityChange = transform.TransformDirection(velocityChange);
rb.AddForce(velocityChange, ForceMode.VelocityChange);
// Add gravity
rb.AddForce(Gravity * rb.mass);
jump = false;
}
}
What comes to $$anonymous$$d off the top of my head, since your script doesn't have the command above the class definition to automatically acquire the rigid-body, is to make sure your object also has the rigidbody component on it or that the script is finding it properly. $$anonymous$$aking it "public Rigidbody rb;" will help you see in the scripts inspector if it's attaching properly. If you put "[RequireComponent(typeof(Rigidbody))]" above the beginning of the class it will automatically insure you have one on the object.