i want super mario galaxy kind of gravity Character are suposed to move around a small planet.planet may b a sphere a cube cylinder any thing.
then implement it. where is the issue? what is your question? what does not work in your code? why are you posting? i remember that there has been some posts on this topic so search the forum.
http://forum.unity3d.com/threads/122237-gravity-making-me-mad check this i tried on some post about it but it didn't under stand this why just in rotation why am not able to implement it in cube? they used Quaternion and freezeRotation i studied these topic from net but i didn't get how to change :s
and how should one be able to help when you dont give any usefull information? describe detailed what you have done and what is not working then someone may have an idea. but when you just state "you want" nobody is going to do it for you.
if your gravity point is at 5, and you are at 10, then your gravity direction is 5 - 10..... If you move to -10 then your gravity direction is 5 - (-10). Normalize the direction and you get a straight direction of 1 or -1. I know... why post something stupid like this.... World Center is 0,0,0.... My position is 100,100,50. Gravity direction is (World Center - Current Position).normalized. Even better... transform.up = -Gravity Direction.normalized I don't think that a character controller uses an up vector, so what your asking for may be a created character controller or a physics controlled object. This way, you can specify your up vector and lots of other things.
how to normalize the directions? where i do this normalization of position? can you specify me in t he code?
http://unity3d.com/support/documentation/ScriptReference/Vector3.html Those are the methods you have available to you using Unities built in Vector class.
No. Off the top of my head, the easiest way to do this is to apply a Spherical Trigger around the planet body, make it the size of the Planetoid + Air. When your player body is within the spherical trigger, Apply a Force Vector towards the center of the planet, psuedocode: Code (csharp): float gravityConstant = -5.0f; Vector3 toCenter = PlayerTransform.position - PlanetoidTransform.position; toCenter.Normalize(); PlayRigidbody.AddForce(toCenter * gravityConstant); Use your triggers to select which Planetoid you are using for your toCenter. This *should* work but might need a bit of tweaking depending on the scale of things and how your players colliders behaves on top of the spherical collider of the planet. You're also going to want to add in code so that your player is standing on his feet when he jumps onto a planet, you could probably do this as soon as you enter the trigger.
can you make it in form of code? and hey i made another code of kind of magnate but that is also not working properly you want to see that ? and let me know the problem and help me in correcting them. and can you implement you logic?
While folks here are more than willing to help you out, they are much less likely to write the code for you. You're also not helping your case by posting the same issue in 3 or 4 different threads. In order to get as much help as possible I'd suggest posting code (not in the form or a rar attachment) and asking specific questions about what's not working in your implementation. Then, when you get responses modify your code and if you still have issues repeat the process. Simply posting an attachment with your project and saying "please change my code" isn't going to garner a lot of support. If you'd rather have someone just write the code for you - I would suggest posting in the Collaboration or Commercial Work forums.
here is my another code i tried its working fine but when i just have a prefab to attract but i want some character to attract it look weird :s Code (csharp): using UnityEngine; using System.Collections; public class magScript : MonoBehaviour { Collider[] allObjectInField; public GameObject attractablePref; GameObject StageCamera; bool rigidBodyBool; public float jumpSpeed = 8.0F; public float gravity = 20.0F; private Vector3 moveDirection = Vector3.zero; public float speed; CharacterController controller; // Use this for initialization void Start () { StageCamera = GameObject.FindGameObjectWithTag("MainCamera"); } // Update is called once per frame void Update () { allObjectInField = Physics.OverlapSphere(transform.position, 20); if (allObjectInField.Length > 0) { for ( int i = 0; i < allObjectInField.Length;i++) { if (allObjectInField[i].gameObject.tag == "attractable") { Vector3 dir = transform.position - allObjectInField[i].transform.position; // for rigid body // allObjectInField[i].rigidbody.velocity = dir.normalized * (20.1f - dir.magnitude ); // for character Controller controller = allObjectInField[i].gameObject.GetComponent<CharacterController>(); //if (controller.isGrounded) // { moveDirection = dir; Debug.DrawLine(transform.position, transform.position + moveDirection, Color.red); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; if (Input.GetButton("Jump")) moveDirection.y = jumpSpeed; //} moveDirection.y -= gravity * Time.deltaTime; controller.Move(moveDirection * Time.deltaTime); } } } if (Input.GetKeyUp(KeyCode.Mouse0)) { Vector3 newpos = StageCamera.camera.ScreenToWorldPoint(Input.mousePosition); newpos = new Vector3(newpos.x, 0, newpos.z); GameObject instantiat = Instantiate(attractablePref, newpos, Quaternion.identity) as GameObject; } } can any one help me i correcting this?
First off, this is not going to work with a CharacterController. (Up is always up with a CharacterController) Do yourself a favor, start simple. Create an empty scene, make a big ball, call it "Earth" and put a cube above it. Give the cube a rigid body, and turn gravity off on it. Now, in a Fixed Update, apply this code: Code (csharp): Transform PlanetoidTransform = GameObject.Find("Earth").transform; float gravityConstant = -5.0f; Vector3 toCenter = transform.position - PlanetoidTransform.position; toCenter.Normalize(); rigidbody.AddForce(toCenter * gravityConstant); If you duplicate the cube all over the world, they will start falling towards the planet center.
works fine for me.... Code (csharp): using UnityEngine; using System.Collections; public class fauxGravity : MonoBehaviour { float gravityConstant = -5.0f; private Transform PlanetoidTransform; // Use this for initialization void Start () { PlanetoidTransform = GameObject.Find("Earth").transform; if(!rigidbody) gameObject.AddComponent<Rigidbody>(); rigidbody.useGravity = false; } // Update is called once per frame void FixedUpdate () { Vector3 toCenter = transform.position - PlanetoidTransform.position; toCenter.Normalize(); rigidbody.AddForce(toCenter * gravityConstant); } } Even a creator... Code (csharp): using UnityEngine; using System.Collections; public class Creator : MonoBehaviour { public GameObject objToSpawn; private float spawnIn=1.0f; // Update is called once per frame void Update () { if(!objToSpawn) return; spawnIn -= Time.deltaTime; if(spawnIn < 0){ Vector3 pos = Random.insideUnitSphere.normalized; pos *= Random.Range(10.0f, 40.0f); Instantiate(objToSpawn, pos, Random.rotation); spawnIn = Random.Range(1.0f, 3.0f); } } }
I just threw BigMisterB's code into a scene and it works perfectly. Just put different spheres in your universe (level, whatever) and apply gravity based on which sphere you're near. Boom, Mario Galaxy.
I'm trying to achieve a similar effect, but with a character controller. You say it wouldn't work with a character controller. Why? Because it's certainly not working with my character controller. The CC seems to stand on the sphere, but slowly slides towards the highest Y point on the sphere. If he's below the hemisphere, he'll find equilibrium somewhere and jitter until he shoots through the world to the max Y point where he sits happily. Is that the sort of behavior you expected a character controller to exhibit here?
For the character controller, 'up' is always World Up, not 'local up'. This works with Rigid Bodies, because you can rotate them around, where the character controller is always up == Vector3(0,1,0). Not sure exactly what to expect when you don't use things properly, I would have expected him to just drop off the sphere when he got to the sides.