I need help with fixing script, I simply need basic ASDW movement and one hit SPACE jump (forbidden jump while in air) with public floats so I can test it and set the right values. (need to jump around 110cm fast).
What I have now work for movement but jump is more like jetpack, it does slowly speed upwards.
I would be glad if somebody would be so kind and helped me with jump issue as I am not programmer, just need this for level design setting/browsing.
Would prefer posting whole functioning code for the same reason.
Thanks in advance.
using UnityEngine;
using System.Collections;
public class CharacterController : MonoBehaviour {
private Rigidbody rg;
public float speed = 10.0F;
public float jumpspeed = 10.0F;
void Start () {
Cursor.lockState = CursorLockMode.Locked;
rg = GetComponent <Rigidbody> ();
}
void Update () {
float translation = Input.GetAxis ("Vertical") * speed;
float straffe = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate (straffe, 0, translation);
if (Input.GetKey (KeyCode.Space)) {
Vector3 atas = new Vector3 (0,100,0);
rg.AddForce(atas * speed);
}
if (Input.GetKeyDown ("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
You need to test if the player is on the ground or not. There are plenty of ways to do this from the simplest but the worst being casting a ray downward for some length, and if it hits, youāre on ground, to the best being a different collider that sends that analyzes the collisions he gets and if itās the ground heās colliding with, then it sets the players onGround variable to true
However Iām NOT going to write you a code, because a.) this forum is not for that, if you want a complete code, go to the asset store or the paid section b.) There are plenty of tutorials online or even code snippets that work. Or if you really donāt want to spend time on this, there is the built-in fps controller
I understand your point of view, could you tell me where I can set built in fps controller? I was actually looking for something like that in the first place⦠I am using free version tho, is it there too?
Yeah, if you right click in your assets folder, there is an import package menu and there in one of the packages (I canāt remember which one though, maybe Character, look it up with google) then you import it, and you should be able to use it
If you want the player to jump instantly rather than having a jetpack-esque motion, add ForceMode.Impulse to the rigidyboy.AddForce. Below is a copy of your script with the minor change, as well as a link to the Unity Docs for some extra information!
using UnityEngine;
using System.Collections;
public class CharacterController : MonoBehaviour {
private Rigidbody rg;
public float speed = 10.0F;
public float jumpspeed = 10.0F;
void Start () {
Cursor.lockState = CursorLockMode.Locked;
rg = GetComponent <Rigidbody> ();
}
void Update () {
float translation = Input.GetAxis ("Vertical") * speed;
float straffe = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate (straffe, 0, translation);
if (Input.GetKey (KeyCode.Space)) {
Vector3 atas = new Vector3 (0,100,0);
rg.AddForce(atas * speed, ForceMode.Impulse);
}
if (Input.GetKeyDown ("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
Oh! I did not catch that, thank you. I also just realized the name of the script is āCharacterControllerā which should be changed as this will conflict with Unityās built-in Character Controller component, so he should also change the name of the script.
Speaking of which, as long as he is using the default Character Controller he should just include this small bit of code.
if (Input.GetKey (KeyCode.Space) && gameObject.GetComponent(CharacterController).isGrounded == true) {
Vector3 atas = new Vector3 (0,100,0);
rg.AddForce(atas * speed, ForceMode.Impulse);
}
Otherwise, the method gorbit99 mentioned of using RayCast should be implemented.
Thank you, first script burst worked, but kept jumping in the air, second says there is unexpected symbol ā=ā And yes, there is character controller on capsule.
Yeah, there are problems with that, sorry @TBranflakes , but the code is just wrong
First of all, you wrote = instead of == in the if statement, secondly you donāt even need that, you can leave it out because it is a boolean, you can just leave the check out
Thatās what happens when you copy and paste and donāt check your work, haha, thank you @gorbit99 . I like to keep it spelled out when dealing with people whoāve never coded before.
Go through your code, and for each opening bracket, find the closing bracket. Make sure your code is within the correct pair and that you donāt have any extras.
This is something you need to learn to do on your own.
In the end I somehow bustled it and got it āworkingā how I want even tho I have some crazy values to achieve realistic feeling⦠Gravity -98ā¦Drag1, weight 80⦠Jump 3500 burst⦠Got one question tho, How to I display value for jump ā3500ā and why does not code values change when I change them in unity public float?
using UnityEngine;
using System.Collections;
public class Capsule : MonoBehaviour {
private Rigidbody rb;
public float speed = 100.0F;
public float raycastLength = 0.52F;
void Start () {
Cursor.lockState = CursorLockMode.Locked;
rb = GetComponent <Rigidbody> ();
}
void Update ()
{
float translation = Input.GetAxis ("Vertical") * speed;
float straffe = Input.GetAxis ("Horizontal") * speed;
translation *= Time.deltaTime;
straffe *= Time.deltaTime;
transform.Translate (straffe, 0, translation);
if(Physics.Raycast(transform.position, -transform.up, raycastLength))
if(Input.GetKeyDown(KeyCode.Space))
{
Vector3 atas = new Vector3 (0,3500,0);
rb.AddForce(atas, ForceMode.Impulse);
}
if (Input.GetKeyDown ("escape"))
Cursor.lockState = CursorLockMode.None;
}
}
If you assign a value to the public variable in the code, it only represents the ādefaultā value for that variable. When you make a new component of that type, it will get that value. Once you change it in the inspector, that new value overrides the default even if you change it in the script.
So when I create gravity float for capsule, it will modify gravity value affecting capsule while other objects follow system gravity or does it override overall gravity settings?
If you assign Physics.Gravity, it will affect overall gravity settings for all objects.
For 3D physics, the only way to set gravity for a single object is to disable Rigidbody.useGravity, and then add your own gravity using FixedUpdate, where you would use AddForce to add a gravity force of your choosing.