- Home /
Acceleration physically with Rotation
Hi,
Im pretty new so its probably easy for you guys to answer.
I used a Sphere as an PlayerObject and set a key to rotate the Sphere. The Goal i was aiming was that the Sphere physically starts moving caused by its gravitiy and collision.
Well its just rotating on the original position, at least under my command, but its not moving.
Is it actually possible or am i just thinking to realistic for unity ? Thanks for your Help guys !
What are you currently doing to rotate the sphere? A script example to show us what means you're currently using would tell us a lot.
Well, thats all i did ... next time i´ll post directly. Thanks for your response ;)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rotate : MonoBehaviour
{
private const string AxisName = "Horizontal";
public float speed = 5.0f;
private float rot = 0.0f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
rot -= Input.GetAxis("Horizontal") * speed;
transform.eulerAngles = new Vector3(0.0f, 0.0f, rot);
}
}
Answer by FlaSh-G · Sep 08, 2021 at 08:43 PM
Yep, that's possible. I'd have thought it would work out of the box for both 2d and 3d, but if it doesn't,
make sure that your ball and the ground have colliders.
make sure you're using a Rigidbody for the ball, and the Rigidbody's torque for rotation.
Increase the friction of the ball's physics material.
in that order.
Answer by Eno-Khaon · Sep 10, 2021 at 08:35 PM
To start, modifying rotation using Transform.rotation will not apply any changes recognizable by physics, so using Rigidbody.AddTorque() is the way to go in this scenario.
This example will be based on two-axis input as a point of reference, but can easily be adjusted to suit your specific needs:
private Vector2 playerInput;
private Rigidbody rb;
public float torque = 5.0f; // 5 revolutions-per-second per-second acceleration example
void Start()
{
rb = GetComponent<Rigidbody>();
rb.maxAngularVelocity = 500f; // Let's go higher than the default 7
}
void Update()
{
playerInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
}
void FixedUpdate()
{
Vector3 appliedTorque = new Vector3(playerInput.y, 0f, playerInput.x) * torque;
rb.AddTorque(appliedTorque, ForceMode.Acceleration); // This ForceMode ignores mass, as/if desired
}
Oh Yeah,
thank you so much. After I added the torque it still didnt work. Seems like it was all about the physics material which i didnt create yet - well i even didnt know it exists. Thanks FlaSh-G \m/
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Torque : MonoBehaviour
{
public float torque;
public Rigidbody rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float turn = Input.GetAxis("Horizontal");
rb.AddTorque(transform.forward * torque * turn);
}
}
But now I figured out that the torque is pretty much limited. how can i change the velocity ?
I tried but it still rotates on one position. Just added "rb.maxAngularVelocity = 500f" in the previous script and now it works. Thanks a lot ;) just ignore float spinforce etc. didnt delete it yet ^^
public class Torque : MonoBehaviour
{
public float torque;
public Rigidbody rb;
public float angDrag;
public float spinforce;
public float maxAngVel;
public float speed;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
rb.maxAngularVelocity = 500f;
}
// Update is called once per frame
void Update()
{
float turn = Input.GetAxis("Horizontal");
rb.AddTorque(transform.forward * torque * turn);
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Build menu missing Visual Studio 2013 1 Answer
LineRenderer.positionCount compilation error 0 Answers
Issue with my camera controls 0 Answers