I’m going to need a lot of help on this one. I’m relatively new to scripting with a bit of previous experience. I’ve been doing all the scripting for my game on my own so far (reading tutorials and gleaning what I can from forum posts and such). But this latest problem has been driving me crazy. I’ve spent forever trying to get it to work properly
I’m trying to make a ball roll using physics. Easy enough when you use AddForce functions. However, adding force on the X axis causes it to roll the ball only on the X-Global Axis. Same thing For the Z and Y. Which is bad if you’re using a camera that rotates around the character, it becomes very disorienting, As your camera can be facing in one direction, while the character rolls along in the direction of the X Axis.
I’ve tried adding the force relatively, but then when the object is rolling, the X, Y, and Z axis are constantly changing, which means I cannot use this method either.
So, the goal I’m trying to reach is:
-Character is a rolling ball and is moved by physics calculations.
-The controls need to be relative to the camera direction (i.e. “W” is always away from the camera)
So please, if anyone can offer help, it would be greatly appreciated!
So your camera is following the ball around from behind and you want to add forces to the ball according to how camera is oriented? Get direction from camera and remove the up-vector (was it y or z) and normalize to get the direction you want.
//edit
err
this gives the forward vector, that is.
Just translate to left or right to get the needed directions when moving to the side
//Edit2
I think i misunderstood your question a little. What you want is a more active camera script so that it follows ball’s movements more dynamically
You can find examples from the lerpz tutorial on how to get the camera’s direction and use it to add forces to the character.
Regarding the camera, it would be best in this instance to find out which way the ball is speeding and translate camera’s target position accordingly, basically. There are some quirks you will have to consider as always. For example ball standing still or very slowly moving backwards should not make the camera turn so quickly… I have no ready made code for you on this.
The ball will probably need a little bit of fine tuning in turning since you probably want to make it turn in spot if it is still btw, but nevermind that for now.
I’ve tried to follow your suggestion and have spent about 2 hours digging through the code for the Lerpz Character Controller. But the code in there is really advanced and convoluted and deals with more things than just finding the direction for the character to walk in. Also, none of the code deals with rigidbodies or adding forces.
I still require additional help. I’m not sure where to go from here
var cameraTransform = Camera.main.transform;
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
yes, The character controller does not make use of rigid bodies and physics the way you want, sorry about possibly confusing you.
But the basics should still be quite the same, and simpler to execute than the lerpz character movement. Just make the ball rigid body and refer to
on how to use it. I would probably prefer adding torque to the ball to make it move naturally.
var speed = 10.0;
var gravitypull = 0;
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravitypull, 0));
var cameraTransform = Camera.main.transform;
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
if (Input.GetKey ("w")) {
rigidbody.AddForce(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey("s")) {
rigidbody.AddForce(Vector3.forward * -speed * Time.deltaTime);
}
if (Input.GetKey ("d")) {
rigidbody.AddForce(Vector3.right * speed * Time.deltaTime);
}
if (Input.GetKey ("a")) {
rigidbody.AddForce(Vector3.right * -speed * Time.deltaTime);
}
}
Thanks for all your help so far Medice. And I apologize for the amateur level questions. But that’s my level, haha.
Okay, so I’m clearly doing something wrong here. (P.S., I realize the code isn’t exactly optimized)
So. As you can see from the code above, right now, I’m adding the forces based on the X and Z world coordinates. Where “W” adds forces in the X+ and S is X-.
What I need is for the code to add the force in the direction the camera is facing.
So I suppose this is a case of knowing WHAT to do, but not necessarily HOW to do it. Surely some of you codemasters out there would be able to point an amateur in the right direction?
Oh wow! That worked absolutely perfectly Medice!
It’s funny how the simplest answer is always the best and usually the RIGHT answer. Thanks so so much for your help in this getting this code together, really appreciate your time and help.
For anyone else that might need to learn a bit from my experiences, the final script now looks like this :
var speed = 10.0;
var gravitypull = 0;
function FixedUpdate() {
rigidbody.AddForce (Vector3(0, gravitypull, 0));
var cameraTransform = Camera.main.transform;
// Forward vector relative to the camera along the x-z plane
var forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0;
forward = forward.normalized;
// Right vector relative to the camera
// Always orthogonal to the forward vector
var right = Vector3(forward.z, 0, -forward.x);
if (Input.GetKey ("w")) {
rigidbody.AddForce(forward * speed * Time.deltaTime);
}
if (Input.GetKey("s")) {
rigidbody.AddForce(forward * -speed * Time.deltaTime);
}
if (Input.GetKey ("d")) {
rigidbody.AddForce(right * speed * Time.deltaTime);
}
if (Input.GetKey ("a")) {
rigidbody.AddForce(right * -speed * Time.deltaTime);
}
}
I have a similar game where the player controls a ball which is rolling down a tilting winding terrain. Ive got the ball to roll with gravity but it rolls slowly even if i change gravity, and pressing the arrow keys has no effect on the ball. I have used the code from the last post. Any help?
You could add a collision check to check if the ball is colliding with the floor…
Jus incase someone else has the same problem and reads this post…
I got my problm solved by this post.
If it is colliding, and the jump key is pressed the thing jumps
////////////////////////////////////
heyy i7Gam3r, i attached the code to the ball. The line if(HitThefloor.gameObject.tag == “Floor”) the floor tag is the ball tag right?
but it stills do nothing and it doesnt jump!
ive just change some capitals (F to the “function” and F to the “HitTheFloor” that you have there