2
\$\begingroup\$

I have a planet and character. My player move perfect around planet with pressing key like 'w','s','a and 'D'. I want to move my player on every frame with same force around sphere. I try plane surface constant force code like this this.rigidbody.AddForce ( 3f,3f,3f);, when start playing game this code work fine but after some movement it stop my player on specific position of planet. I want to use this code for 3D sphere.

Any one can help me how can add constant force to my player on 3d sphere.?

I want add constant force to player when game start. I don't want any kind of planet's orbits, because my player is moving on all over the planet.

I try below link but it did not working for me. How can i accurately simulate orbits in unity.?

Here is my Sctipts

PlayerController.cs

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

public float moveSpeed = 10;
public Vector3 moveDir;
Rigidbody rigidbody;
// Update is called once per frame3

void Start(){
    rigidbody = GetComponent<Rigidbody> ();
}
void Update () {

    moveDir = new Vector3 (Input.GetAxisRaw("Horizontal")  ,0,Input.GetAxisRaw("Vertical"));
}
void FixedUpdate(){

    rigidbody.MovePosition (rigidbody.position + transform.TransformDirection(moveDir) * moveSpeed * Time.deltaTime);

 }
}

FauxGravityBody.cs

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Rigidbody))]
public class FauxGravityBody : MonoBehaviour {

public FauxGravityAttractor attractor;
public Transform myTransform;
Rigidbody rigidbody;

// Use this for initialization++
void Awake(){

    this.rigidbody = GetComponent<Rigidbody> ();

}
void Start () {
    myTransform = transform;
    rigidbody.useGravity = false;
    rigidbody.constraints = RigidbodyConstraints.FreezeRotation;
}

// Update is called once per frame
void Update () {

    attractor.Attract (myTransform);
  }
}

FauxGravityAttractor.cs

using UnityEngine;
using System.Collections;

public class FauxGravityAttractor : MonoBehaviour {

public float gravity = -10;
public void Attract(Transform body){

    Vector3 gravityUp = (body.position - transform.position).normalized;
    Vector3 bodyUp = body.up;

    body.GetComponent<Rigidbody>().AddForce (gravityUp * gravity);
    Quaternion targetRotation = Quaternion.FromToRotation (bodyUp, gravityUp) * body.rotation;
    body.rotation = Quaternion.Slerp (body.rotation, targetRotation, 50 * Time.deltaTime);
  }
}

I am working with this scripts, If any one help me? then please.

\$\endgroup\$
  • 1
    \$\begingroup\$ Possible duplicate of How can I accurately simulate orbits in Unity? \$\endgroup\$ – Philipp Jun 9 '16 at 14:49
  • \$\begingroup\$ This is not duplicate. You put link for planet orbit. I don't want to move my player with orbit. My player is playing and jumping in all over the planet. @Philipp \$\endgroup\$ – mahesh Jun 9 '16 at 16:09
  • \$\begingroup\$ I want to apply constant force to my player with game start. By using your linked question script i can't do that. @Philipp \$\endgroup\$ – mahesh Jun 9 '16 at 16:16
1
\$\begingroup\$

There is the Faux gravity system in Unity and it works with jumping as well. TThis is the youtube link - https://m.youtube.com/watch?v=gHeQ8Hr92P4&fulldescription=1&client=mv-google&gl=ZW&hl=en. The download link for the unity project seems to be dead though. Ive uploaded the project to my mediafire account. I'l also add it to github when i get on a PC. link - http://www.mediafire.com/download/o7w7mogd6w3a4jc/Faux_Gravity_Example.7z

Edit 1: Adding constand force- In order to add a constant force to the player object, in the Player Controller script, edit the update method. Replace all the code in update with this

moveDirection = new Vector3(1,0,1);

I suggest you take a look at some basic C# tutorials before jumping into Unity. If this is for a school project or assignment, you wont learn much doing things this way. good luck.

\$\endgroup\$
  • \$\begingroup\$ I see your tutorial and its not for unity 5. I change the scripts given in tutorial for unity 5, but still result is same. player not start moving around sphere with game start. I must have to press W,A,S and D key to move player. @zimspy \$\endgroup\$ – mahesh Jun 10 '16 at 7:32
  • \$\begingroup\$ I updated the scripts using Unity's Api updater before re-uploading them. \$\endgroup\$ – zimspy Jun 10 '16 at 12:31
  • \$\begingroup\$ Sorry for this, I forget this little thing. If I use Input.GetAxisRaw then i have to add value by pressing keys at runtim, and If i want to transform it directly with game start then I can specify value in Update() method and also I can set it at runtime by using variable. thanx for saving my time and brain. \$\endgroup\$ – mahesh Jun 10 '16 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.