so could somebody make me a simple jump script in c# for an fps character without using rigidbody? i dont know why but rigidbody isnt working on my character. i also want to be able to jump using spacebar.
Answer by Tanoshimi2000 · Oct 30, 2018 at 02:54 PM
For a simple jump (before I had access to rigid bodies) I liked to use the Sine function. As in:
Y=Sin(cnt) cnt+=1
It could be more involved than that, with checking for ground collisions in case they're jumping up on a box or platform. But the basic premise is that Y is incremented in each iteration of the Update function with the Sine of cnt or Timer, which runs from 0 to 1 and back to 0 when cnt is run from 0 to 180.
Of course, you could also just do: jumpamount=1 y+=jumpamount if Y>jumpheight then jumpamount=-1 if Y
Basically, there are easy ways to do it without RigidBodies.... but I don't recommend it. You're better off figuring out why the RB isn't working.
TY bro i will take your advice do you have any idea why the RB wouldnt be working?
I have a thought or two. It's less important to know WHY it's NOT working, than it is to just get it to work. So...
First, make sure you have a collider. RigidBodies practically require them.
Second, try starting with a simple object, like a box, add a rigid body and see if you can get that to work. If so, then start adding things slowly, like replace the box with your model, then add in the animations, then other scripts. Next, why not just use the prefabs that come with Unity? Take the Third Person Controller prefab, drop it in your project and hit play. It should just work. Then you can change their model to yours, etc. Also, which functions are you using in Rigid Bodies? Like, if you try changing the transform, that will overrule the RB physics. For example, if you use RB.AddForce(Vector3.Up * 10) to get him to jump, but in another section or script have something that affects the transform, like transform.Y = transform.Y-Gravity, that's going to negate any forces. Physics is the world acting on things, Transform is God acting on things. Or the programmer/creator. It's the higher power (literally).
Hope this helps. If not, you can hit me up outside this stream via e-mail and we can figure it out.
so i am using a character controller for my player and it seems that rigidbody isnt compatable with the character controller it just keeps spinning and flying all over. is this supposed to be happening? and the character controller is neccesary for the script i have so if i could mabie just show you the script and you could show me where to put the simple jump scripts you have in the answer...this is the script can you fix it? using System.Collections; using System.Collections.Generic; using UnityEngine;
public class fpsController : $$anonymous$$onoBehaviour { public float speed = 2.0f; public float sensitivity = 2f; CharacterController player;
public GameObject eyes;
float moveFB;
float moveLB;
float rotX;
float rotY;
private void Awake()
{
LockCursor();
}
private void LockCursor()
{
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
}
// Use this for initialization
void Start()
{
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update()
{
moveFB = Input.GetAxis("Vertical") * speed;
moveLB = Input.GetAxis("Horizontal") * speed;
rotX = Input.GetAxis("$$anonymous$$ouse X") * sensitivity;
rotY = Input.GetAxis("$$anonymous$$ouse Y") * sensitivity;
Vector3 movement = new Vector3(moveLB, 0, moveFB);
transform.Rotate(0, rotX, 0);
eyes.transform.Rotate(-rotY, 0, 0);
movement = transform.rotation * movement;
player.$$anonymous$$ove(movement * Time.deltaTime);
}
}