Making a torque-based sphere controller by simple mouse inputs

I’m new to C# and Unity but progressing quickly. I have been able to get most of it under control but this is taking too much time so… I’m throwing up the help Flag :slight_smile:

The idea is the player will control a Sphere in a Roller style game via mouse.

Mouse Control: starts with left mouse Press. Records first Point. A GUI helper will appear on that point as visual feedback for input.

2364-rolleruiscreenshot.jpg

Player Drags mouse from start point. Vectors are created on the fly using the angle and distance from the center translates as magnitude (and hopefully speed of the force applied) of the UI drawn vector for imparting that exact vector force on the sphere. The player should be able to command torque from this UI in any direction and most velocities easily and on demand.

So, the trouble comes in a number of ways so far.

The first is: how do I create the corresponding torque in ALL directions? I I have managed to create a prototype that works “half-way” but it wont perform correctly in some quadrants. multipliers were giving me grief in this area as well.

I tried to pull back from the purist approach above and try some alternatives thinking i could run the torque along the X axis like a wheel and just try to “Steer” or rotate it as needed… nope that was a mess.

my code has been altered and tweaked so many times, but I was able to scrape out my original code below.

I apologize now for inefficient or bad coding :slight_smile: I’m a designer who likes to dig in…(code)

Initial Code attempt: Drop onto sphere

using UnityEngine;
using System.Collections;

public class addTorque : MonoBehaviour
{
private Vector3 spinVector;

void Update()
{

if (Input.GetMouseButton(0))
{
float vectValueX = (Input.GetAxis(“Mouse X”)) * spinVector.magnitude;
float vectValueY = (Input.GetAxis(“Mouse Y”)) * spinVector.magnitude;
rigidbody.AddTorque(vectValueX, 0 , vectValueY);
}

}

}

Appreciate any help I can get! I look forward to your replies :slight_smile:

-Paul

I was my own worst enemy in this case. I used code from too many iterations of my experimentation to rebuild my first attempt.

Well here is the correct code This code works as expected for a milestone. Multiplier and the UI are the next stage.

Thanks

using UnityEngine;
using System.Collections;

public class torqueController : MonoBehaviour
  { 
    private Vector3 startPoint;
    private Vector3 endPoint; 
    private Vector3 driveDirection;
 
    void Update()
 {
     if(Input.GetButtonDown("Fire1"))
       {
 startPoint = Input.mousePosition; 
       }
 if (Input.GetMouseButton(0))
 {
 endPoint = Input.mousePosition; 
 driveDirection = endPoint - startPoint;
 rigidbody.AddTorque(driveDirection.x, 0, driveDirection.y );
 } 
    }
}
`
`

Can Someone please select my previous post as the answer to this question :slight_smile: wont lemme do it :confused:

thanks