Can I ignore part of a vector3 for physics reasons?
Hi! Long time reader, first time asker.
I'm coding my own 3rd person character controller that pivots around and object and has a set velocity (for a 1v1 Arena fighter) - so of course when I set a velocity for this 3D character I need a Vector3.
Currently, my velocity is set to be clamped as well as follows: me.velocity = Vector3.ClampMagnitude (move, speed ) * Time.deltaTime ;
the problem with that being that the Y value of the move vector is 0, and calls for 0 on every fixed update, which has made figuring out how to jump very difficult.
If I change it to me.velocity += Vector3.ClampMagnitude (move, speed ) * Time.deltaTime ;
I can easily make a nice jumping motion, but my ground movement becomes very fast and sliding, not the snappy movement I want, and have to move a lot of other code around.
This entire controller was initially coded through transformations, but I realized I needed to use physics based movement because there's a lot of collision involved.
It's entirely possible that the solution is simple and that I've been banging my head against it too long to see it, but is there any way to straight up ignore the 0 Y value being set through that vector so that gravity behaves normally, or is there a way to apply velocity in an additive, but not cumulative way (so it acts as if I just set the velocity... I guess without actually setting it?)
the following is all the code from the relevant area of the script:
Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
forward.y = 0f;
forward = forward.normalized;
Vector3 right = new Vector3(forward.z, me.velocity.y - Physics.gravity.y, -forward.x);
float h = Input.GetAxis ("Horizontal");
float v = Input.GetAxis ("Vertical");
Vector3 walkDirection = (h * right + v * forward);
float movex = walkDirection.x * speed;
float movez = walkDirection.z * speed;
Vector3 move = new Vector3 (movex, 0, movez);
me.velocity = Vector3.ClampMagnitude (move, speed ) * Time.deltaTime ;
me.velocity += new Vector3 (0, me.velocity.y + gravforce , 0) * Time.deltaTime ;
if (Input.GetKey (KeyCode.Joystick1Button0) && isgrounded ) {
me.AddForce (Vector3.up * force);
isgrounded = false;}
if (isgrounded == false) {
speed = 200f;
}else if (isgrounded == true){
speed = defaultspeed;
force = defaultforce;
}
thanks so much for potential answers!
Your answer
Follow this Question
Related Questions
Unity2D Help with rigidbody character movement. 0 Answers
New Vector2 won't take effect when called from another script in the same GameObject. 0 Answers
how to dostraight ball throwing objects from returning? 0 Answers
I am interested in moving a ragdoll character only using Forces. 0 Answers
How to move whilst airborne (using standard third person character script)? 0 Answers