- Home /
Character controller with Rigidbody to apply force
i am scripting a humanoid ploymesh player for soccer and i am getting issues of the player movement. In starting i applied character controller and it worked great but then i have to apply physics like when the player hits the ball or collide with it then the ball will displace in that direction. but the character controller thinks of the ball as a stair and it climbs over the ball instead of applying a force. i applied rigid body with it but it didn't work. then changed the character controller to rigid body only with capsule collider, unfortunately, capsule collider doesn't fill the whole human body i mean it leaves the feet of the player. i tried to move the player with the capsule collider above the feet so it doesnt touch the ground. so when i play the feets were in the ground instead of on the ground. so i add box colloder with rigid body to both the foot of player. but and then applied script it doesnt allow the player tomove instead the player only rotate its poistion and i didnt applied isKinematice=false. I will attach my script with it. And please if you have any advice according to my problem please share with me i would love to read it.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { CharacterController myControls;
public Transform cam;
[SerializeField]
private Rigidbody playerRB;
// [SerializeField] //private Rigidbody leftFootRB; //SerializeField] // Rigidbody rightFootRB;
public float speed = 5f;
public float turnSmoothTime = 0.1f;
public float turnSmoothVelocity;
// Start is called before the first frame update
void Start()
{
/// myControls = GetComponent<CharacterController>();
playerRB = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(horizontal, 0f, vertical).normalized;
//playerRB.AddForce(Vector3.forward * speed * vertical);
//playerRB.AddForce(Vector3.right * speed * horizontal);
if (movement.magnitude >= 0.1f) {
float angle = Mathf.Atan2(movement.x, movement.z)*Mathf.Rad2Deg +cam.eulerAngles.y;
float smoothRotate = Mathf.SmoothDampAngle(transform.eulerAngles.y,angle,ref turnSmoothVelocity,turnSmoothTime);
transform.rotation = Quaternion.Euler(0, smoothRotate, 0);
Vector3 mouseMoveDir = Quaternion.Euler(0, angle, 0)*Vector3.forward;
// playerRB.MovePosition(mouseMoveDir.normalized * Time.deltaTime * speed);
playerRB.AddForce(movement.normalized* speed);
//leftFootRB.AddForce(mouseMoveDir.normalized * Time.deltaTime * speed);
// rightFootRB.AddForce(mouseMoveDir.normalized * Time.deltaTime * speed);
//myControls.Move(mouseMoveDir.normalized * Time.deltaTime * speed);
//playerRB.AddForce(mouseMoveDir.normalized * vertical * speed * Time.deltaTime);
}
}
}
The commented lines are showing the experiments i have already worked on. Thank you
Your answer
Follow this Question
Related Questions
Having problems with animation and character controller velocity not matching 0 Answers
Character falls thru to his waist 11 Answers
Mecanim Rigidbody character slope limit issue 1 Answer
Why do CharacterController objects not come in different collision shapes? 0 Answers
CharacterController.move called on inactive controller 1 Answer