I am back yet again with an issue regarding the character control in my little project. The main method of movement control in the game is done via jumping between orbits around spheres in the world. Until now you have not been able to alter the orbit around the spheres, but this is a feature that needs to be added as without it the player ends up stuck in awkward orbits. This is a pretty specific problem, so I’ll try to be descriptive and include as much of my code as possible. To get an idea of what I’m talking about, here is a video showing some rough movement I already have set up.
EDIT: To be more clear, I’m essentially trying to rotate the orbit orthagonally…I want to rotate on the axis perpendicular to the player’s orbit.
Here is the code that controls the player orbit: (anchors are the spheres in the scene)
normal = (anchor.transform.position - rigidbody.position).normalized;
diff = (anchor.transform.position - rigidbody.position).magnitude - orbitRadius;
cross = Vector3.Cross(normal, rigidbody.velocity.normalized);
function Orbit(){
rigidbody.AddForce(normal * diff * forceMultiplier);
rigidbody.position = anchor.transform.position - normal * orbitRadius;
var newRot : Quaternion = Quaternion.LookRotation(rigidbody.velocity, normal);
rigidbody.rotation = newRot;
}
The behavior that I am trying to obtain is for the player to start orbiting at an angle to the right, instead of just forward. This way the trajectory of the orbit can be altered.
Right now I am simply adding the cross product (var cross) of the direction from the player to the current sphere and the player’s forward velocity to attempt to adjust the angle of his orbit, like so:
var xDir : Vector3;
if(Input.GetButton("right")){
xDir = cross * steerAmount;
}else{
xDir = Vector3.zero;
}
if(Input.GetButton("left")){
xDir = -cross * steerAmount;
}else{
xDir = Vector3.zero;
}
function FixedUpdate(){
if(anchor){
Orbit();
var dir : Vector3 = (rigidbody.velocity.normalized + xDir).normalized;
rigidbody.AddForce(dir * speed);
}
}
Unfortunately, this results in unrealistic behavior. The player’s rotation is adjusted to the current direction of the player’s velocity, so when adding force only to the player’s right like this it simply moves in a small hemisphere around the end of the current sphere. When moving forward and steering, the player’s position is forced slightly to the right instead of adjusting the angle of the orbit as I would like.
Sorry for the extremely long winded and convoluted question, but I’ve been poring over this for a day or two now and can’t find a solution.