x


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 :)

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.

alt text

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 :) 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 :)

-Paul

more ▼

asked Aug 01 '12 at 08:37 PM

I9ball gravatar image

I9ball
130 1 5 10

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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 );
 } 
    }
}

more ▼

answered Aug 02 '12 at 11:24 PM

I9ball gravatar image

I9ball
130 1 5 10

(comments are locked)
10|3000 characters needed characters left

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

thanks

more ▼

answered Aug 02 '12 at 11:26 PM

I9ball gravatar image

I9ball
130 1 5 10

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x986
x580
x526
x199
x1

asked: Aug 01 '12 at 08:37 PM

Seen: 413 times

Last Updated: Aug 04 '12 at 01:38 AM