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.