Hi All,
Please excuse what are probably silly questions, but I’m very new to Unity and 3d game engines in general.
I really have two questions that are somewhat related.
- In my game I have a GUI Texture that ‘represents’ a sphere but in 2d - this is used for the player to select an impact point on a sphere in the real 3d scene. So the impact point is indicated on the 2d object and then later a linear impulse (or equivalent) is applied to a sphere in the 3d scene. I use code like so to acheive this:
void Update ()
{
if (Input.GetButtonDown ("shoot")) {
SphereCollider sc = GetComponent<SphereCollider> ();
float size = sc.radius * transform.localScale.x;
//
// TODO: there is no science here, the size is multiplied with an arbitrary number 10 because it just seemed better.... :(
//
float yOffset = (shotPlacement.offset.y * size * 2);
float zOffset = (shotPlacement.offset.x * size * 2);
Vector3 strikePosition = new Vector3 (rigidbody.worldCenterOfMass.x, rigidbody.worldCenterOfMass.y + yOffset, rigidbody.worldCenterOfMass.z + zOffset);
//
// PowerBar.power gives a value in the range 0..256 - for now just multiply by 2 to give us a range 0..512 - adujust the multiplier as needed.
//
Vector3 force = new Vector3 (PowerBar.power * Constants.POWER_MULTIPLIER, 0, 0);
force = Quaternion.Euler(0, ((-Weapon.angle) * Mathf.Rad2Deg)+180, 0) * force;
rigidbody.AddForceAtPosition (force , strikePosition);
}
}
So you’ll see that I’m using shotPlacement.offset.x/y to determine where on the sphere we’ll hit, then I attempt to rotate that based on where the user has chosen to fire the weapon (the user moved the weapon around the sphere on the Y axis to choose the strike position on that axis).
So this seems to work correctly for determining the vector for the force applied, but I also need to rotate the strikePosition to apply the force at the correct location. Doing the same Quaternion rotation on the strike position gives odd results - obviously I’m doing it wrong.
Any hints on where I’m going wrong here?
- In question 1 I state that the user can choose where to file the weapon at the object by pointing the weapon at the object rotating around the Y axis. My code is something like this:
if (shotPlacement.isActive == false (Input.GetMouseButtonDown (0) || Input.GetMouseButton (0))) {
Vector3 v = camera.ScreenToWorldPoint (Input.mousePosition);
transform.position = new Vector3 (v.x, transform.position.y, v.z);
transform.position = Limit (transform.position, target.transform.position.x, target.transform.position.z, distanceFromSpherel);
transform.LookAt (target.transform);
transform.Rotate (0, 90, 0);
}
So what I’m trying to do here is allow the user to grab the weapon and rotate it around the sphere object keeping the weapon point at a fixed distance from the center of the sphere. I’m using camera.ScreenToWorldPoint (Input.mousePosition) to convert the mouse position to the world point and transform.LookAt(…) to keep the weapon oriented looking at the sphere.
This works great in Orthographic projection but doesn’t work at all in Perspective projection - what am I missing here, how would I do similar when using perspective projection?
Most likely I’m just going about this the wrong way and I’m sure I’m showing my ignorance here ![]()
Any help, ideas, suggestions, sample code, pointers to what I should be reading etc will be greatly appreciated…
Cheers.