Question by
AnnieLunnaby · Mar 26, 2020 at 02:09 PM ·
collisionrigidbodyscript.controllerjoint
My character doesn't detect collisions or Hinge joint.
Hi, I'm developing a game and I use a motion script from Sebastian Lague's channel. But whenever I come in contact with an object that has a Rigidbody or an object that has a Hingle Joint, the collision doesn't work. The object I collided with doesn't even move. I really need this help a lot.
This is the controller script:
public float walkSpeed = 2;
public float runSpeed = 6;
public float gravity = -12;
public float jumpHeight = 1;
[Range(0,1)]
public float airControlPercent;
public float turnSmoothTime = 0.05f;
float turnSmoothVelocity;
public float speedSmoothTime = 0.1f;
float speedSmoothVelocity;
float currentSpeed;
float velocityY;
Animator animator;
Transform cameraT;
CharacterController controller;
// Use this for initialization
void Start() {
animator = GetComponent<Animator>();
cameraT = Camera.main.transform;
controller = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update() {
Vector2 input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
Vector2 inputDir = input.normalized;
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
if (inputDir != Vector2.zero) {
float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
}
bool running = Input.GetKey(KeyCode.LeftShift);
float targetSpeed = ((running) ? runSpeed : walkSpeed) * inputDir.magnitude;
currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));
velocityY += Time.deltaTime * gravity;
Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;
controller.Move(velocity * Time.deltaTime);
if (controller.isGrounded) {
velocityY = 0;
}
float animationSpeedPercent = ((running) ? 1 : .5f) * inputDir.magnitude;
animator.SetFloat("SpeedPercent", animationSpeedPercent, speedSmoothTime, Time.deltaTime);
}
void Jump () {
if (controller.isGrounded) {
float jumpVelocity = Mathf.Sqrt (-2 * gravity * jumpHeight);
velocityY = jumpVelocity;
}
}
float GetModifiedSmoothTime(float smoothTime) {
if (controller.isGrounded) {
return smoothTime;
}
if (airControlPercent ==0.1) {
return float.MaxValue;
}
return smoothTime / airControlPercent;
}
Comment