0
votes

I am desperately looking everywhere for a solution to this problem but did not find any possibility yet, maybe you can help me out: I am making a simple test in which a kind of cylinder can rotate itself and moving on a flat platform by friction. I have rigid body, mesh collider attached already and a script to control the rotation. It can be moving fast or slow depends on its rotation speed. The script is as follow:

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

public class PlayerController : MonoBehaviour {

public float rollingSpeed;
public float gravity;

// Update is called once per frame
void FixedUpdate()
{
    transform.Rotate(rollingSpeed, 0.0f, 0.0f);
    GetComponent<Rigidbody>().AddForce(0.0f, -gravity, 0.0f);
}
}

The problem is when I run the Play mode, this cylinder is rotating itself but staying in the same position! (in fact it is moving back and forth very slightly), I really dont know what is the cause, I tried to increase the friction parameters, add physical materials, and even adding a second force downward (like gravity) but it doesnt work. Can anyone please have a solution for this? Thank you guys so much!

enter image description here

1

1 Answers

5
votes

Rotate is just transform eular coordinate without any force, use AddTorque instead for physical movement.

For example,

GetComponent<Rigidbody>().AddTorque(new Vector3(1, 0, 0) * rollingSpeed * Time.deltatime);

Read more, Unity Doc.

To add a tips about basic physics on Unity.

Please remember when you are working with Transform, it means NO physics. You are just changing its position and rotation.

To work with physics, is always Rigidbody.