0
\$\begingroup\$

I am new to Unity and I am just experimenting with a player controller script (which I got off youtube) and another object which doesn't move. Both my player and the stand-alone object have colliders and rigidbodies but when I move the player it goes straight through the other object.

My player controller script is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayController : MonoBehaviour
{
    public float speed;

    private Rigidbody2D rb;
    private Vector2 moveVelocity;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        Vector2 moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
        moveVelocity = moveInput.normalized * speed;
    }

    private void FixedUpdate()
    {
        rb.MovePosition(rb.position + moveVelocity * Time.fixedDeltaTime);
    }
}

Nothing has been changed on the colliders (nothing ticked), and the rigidbody2D has been changed to kinematic.

\$\endgroup\$
  • \$\begingroup\$ How do you move them? What settings do your colliders have? \$\endgroup\$ – trollingchar Jul 29 '19 at 10:00
  • \$\begingroup\$ I edited the question to add more information \$\endgroup\$ – user130389 Jul 29 '19 at 10:05
  • \$\begingroup\$ Does this reproduce if we just set rigidbody's velocity in Update and let it move on its own? \$\endgroup\$ – trollingchar Jul 29 '19 at 10:17
  • \$\begingroup\$ Sorry, but I literally just started in Unity and got this script off YouTube. Could you further explain what you mean? \$\endgroup\$ – user130389 Jul 29 '19 at 10:24
  • 1
    \$\begingroup\$ "kinematic" means "don't resolve collisions for me with this object - I'll handle it myself" so, do you really intend to handle it yourself? If you want the physics engine to handle it, the object should be dynamic, not kinematic. \$\endgroup\$ – DMGregory Jul 29 '19 at 12:00
1
\$\begingroup\$

In physics engines, "kinematic" means "I'll handle steering this object around collisions myself — so engine, don't do any collision resolution on my behalf"

It's often useful for things like moving platforms, which should absolutely never stray from their assigned path — even if it goes through a solid floor — or get jostled when something lands on them. We'll also use it for animated characters, where in the event of a collision we want to play a dedicated contact or reaction animation, and ensure the animated foot positions stay accurately planted on the ground, without physics bouncing and sliding our character around.

But if you want the engine to handle collisions for you, you should uncheck the kinematic box and treat the object as dynamic instead. That tells the physic engine it's allowed to bounce it around in the event of collisions.

One other thing: I notice you're calculating a velocity. Rather than integrate that velocity yourself and use MovePosition to teleport to the next location, you might as well just give the velocity to your Rigidbody2D so the physics engine can do the integration for you. It can also use that velocity information to get more accurate collision responses, since it knows how everything was moving before the impact.

I wouldn't do it in Update as trollingchar recommended though. This runs after our physics step, so this adds an unnecessary frame of input latency. Instead I'd do it like this:

void FixedUpdate() {
    var input = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
    input = Vector2.ClampMagnitude(input, 1f);
    rb.velocity = input * speed;
}
|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.