Problems with movement

Hey there, this is my first time trying to make a game using Unity. I know a small bit of C#, and decided to make a simple game, but I have a problem with the movement. I’m trying to make a cube to only move left and right while rotating. I got that right but it ends up speeding up and rotating out of the little map I made.

MelodicEvenArthropods

I’ve been looking around for an hour now and I cannot seem to find a solution. Do you guys have any ideas?

The code I’m using is:

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

public class PlayerMvmnt : MonoBehaviour {

    public float hF = 10f;
    private Rigidbody rb;

    void Start () {

        rb = GetComponent<Rigidbody>();

    }

    void Update () {
        if (Input.GetKey("d"))
        {
            rb.AddForce(hF * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
        if (Input.GetKey("a"))
        {
            rb.AddForce(-1 * hF * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }
    }
}

Thanks in advance!

Well, for one thing, you’re using physics. Why are you doing that? Are you intending to knock down towers of blocks, or something?

If not, save yourself a lot of grief and just move your transform.position how you want it to move.

If you must use the physics engine, then just expect this kind of thing — adding force to an object accelerates it, and it will quickly get out of control unless you crank up the friction or apply some counter-forces.

If you have to use forces then you maybe be able to cap the amount of force applied using a customforce:

and limit/prevent additional force being applied once the object reaches a certain velocity :

I am no unity expert but I imagine you can test the velocity and only apply force if it is under a certain amount, and then scale the force applied so it lessens as you get closer to max velocity… this still wouldn’t be perfect but you may be able to balance it. However physics for this sort of thing is never going to be perfect.

1 Like

So I’m basically imagining things falling down from above and you just have to get out of the way to dodge them, I guess physics aren’t needed. Is there a way I can make movement like this without including physics?

Thanks for your input, I’ll look into what you said.
I might look for a way to do this without physics.

Just out of curiosity, at what (other) points do you consider physics, personally or recommend them to others?
I mean, beyond knocking boxes or towers?

I’m all for using the transform.position, too, at times. However, I’ve been wondering what you’d recommend when it comes to Trigger enter/exit and collisions (stopping mostly/at least). From the posts I’ve read, I haven’t seen a deep discussion of these aspects.

Sorry to partially interject/hijack :wink:

Sure — you just write code that moves things the way you want to. Sometimes this means doing a very simple physics calculation yourself, along the lines of:

void Update() {
    velocity += gravity * Time.deltaTime;
    transform.position += velocity * Time.deltaTime;
}

That’s basically Newton’s laws, and they’re quite simple. (Note that velocity here would be a Vector3 property on your class.) This would be enough to make your things fall down realistically. But for a game you might not want them to fall too realistically, so you might add something to limit the magnitude of velocity, for example.

You’d be amazed how rarely I’ve needed Trigger enter/exit. :slight_smile: I tend to instead (1) check the distance between things, (2) do bounds-bounds overlap tests, or (3) use Physics.RayCast. Basically I look for collisions when & where I need to know, rather than having the physics engine notify me when they happen.

Well I managed to make the movement better, but I ran into a problem again. Now whenever I change direction the cube seems to glide before it starts rotating to it.

OddballTerrificAsiaticlesserfreshwaterclam

Code:

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

public class PlayerMvmnt : MonoBehaviour {

    [SerializeField]
    public float speed = 0.0f;
    public Rigidbody rb;

    void FixedUpdate()
    {
        Move();
    }

    void Start ()
    {
        rb = GetComponent<Rigidbody>();

    }

    void Update ()
    {


    }

    private void Move()
    {
        if (Input.GetAxis("Horizontal") != 0)
        {
            float hAxis = Input.GetAxis("Horizontal");
            Vector3 moveDir = new Vector3(hAxis, 0, 0);
            rb.velocity = moveDir * speed * Time.deltaTime;
        }
    }
}

Fair enough :slight_smile: I figured the 3 points you mentioned would be included. I guess most of my efforts to move a character around have been a little more “lazy” lol. Though, I have used those 3 options in parts of code, too.

That’s because your still relying on physics to do the tumble effect. There is nothing in your code to rotate the cube. What is happening is the cube is being dragged across the ground and friction is being applied. Once the velocity is greater than the friction the cube will tumble. Your particular problem comes into play when changing the direction and the cube is not flat on the ground. With little or no friction the cube ‘flies’ for a short distance.

You’ve painted yourself into a corner by relying on physics to do your animation. There are ways to compensate and get what you want for the most part, but the physics engine is always going to do what it’s going to do and you give up control.

Tried using non physics movement which rotated perfectly, but I couldn’t stop it from going through walls.

Well welcome to game development and coding! Every solution has it’s own set of problems. So you fixed the rotation and it is what you want, now fix the wall collision issue. Game development is nothing more than a series of seemingly never-ending small problem solving. Get use to that right away and figure out how to solve your current problem. Once you do, there will be another right behind, that’s just the way it works.

Happy coding…