Converting my Character Controller Movement to Rigidbody
Thinking it might be better for my game, I'v started converting my player controls using the Character Controller component to a Rigidbody component. Almost everything for the player movement is converted except for one line of code. As seen below (at the very bottom) it is the line with controller.Move, because I don't know the Rigidbody equivalent to controller.Move. If anyone knows what I can put to get this script to work, please let me know.
public class PlayerController : MonoBehaviour
{
public CharacterController controller
public Transform cam
public float turnSmoothTime = 0.07f;
float turnSmoothVelocity;
void Update()
{
//Character Movement
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
inputVector = new Vector3(horizontal * 50f, rb.velocity.y, vertical * 50f).normalized;
rb.velocity = inputVector;
//Character Rotation
if (inputVector.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(inputVector.x, inputVector.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * movementSpeed * Time.deltaTime);
}
}
}
Answer by diegohartmann · Mar 14, 2021 at 03:39 PM
found a solution:
Vector3 moveDir = Quaternion.Euler(0, targetAngle, 0) times Vector3.forward;
// controller.Move(moveDir.normalized times speed times Time.deltaTime);
rb.velocity = moveDir.normalized times speed times 20 times Time.deltaTime;
Your answer
Follow this Question
Related Questions
i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers
i am using a code to make my cube walk with character controller ,but when i play the cube spins. 0 Answers
Control speed of a rigidbody that move to a fixed distance 2 Answers
Help converting a character controller script to rigidbody 1 Answer
Problems with First Person Controller and Character Model 0 Answers