Question by
szabotibor0123456789 · Apr 14, 2020 at 02:27 PM ·
collisionmovementrigid body
Rigid body goes flying on colliding
I am having problems creating a basic character movement script. My main idea is to control the the force of the player.When the rigidbody collides with walls or other objects the rigid body goes flying. The hierarchy is simple Character ->Texture,Camera i have the colider and the rigidbody and the script on the Character Empty. The code what i have is:
horizontal = Input.GetAxis("Horizontal");
vertical = Input.GetAxis("Vertical");
float movSpeed = walkSpeed;
//calculate target force
Vector3 dir = new Vector3(horizontal,0, vertical);
dir = transform.TransformDirection(dir);
Quaternion rot = Quaternion.AngleAxis(cam.eulerAngles.y, Vector3.up);
dir *= movSpeed;
Vector3 velocity = rb.velocity;
Vector3 velocityChange = (dir - velocity);
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxSpeed, maxSpeed);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxSpeed, maxSpeed);
velocityChange.y = 0;
//aplply force
rb.AddForce(velocityChange , ForceMode.VelocityChange);
rb.AddForce(Physics.gravity * rb.mass);
i copied the part of the code what i have found on http://wiki.unity3d.com/index.php/RigidbodyFPSWalker and i must use forces i can't use Transform
Comment