C
C#nissemanen

why is my player floating, twitching and can almost not go up a ramp

so im learning c# for unity and i made a (very) litle scene to test my first person controls i made. but the wierd thing is that the gravity is acting wierd. ill send a video and the code i use.
// public's
public float speed;
public float jumpForce;

// private's
private Rigidbody rb;
private bool isJumping = false;

void Start()
{
transform.position = new Vector3(0,1,0);
rb = GetComponent<Rigidbody>();
}


void Update()
{
if(!isJumping)
{
Move();

if(Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
}

private void Move()
{
Vector3 direction = Vector3.zero;

if (Input.GetKey(KeyCode.W))
{
direction += transform.forward;
}

if (Input.GetKey(KeyCode.S))
{
direction -= transform.forward;
}

if (Input.GetKey(KeyCode.D))
{
direction += transform.right;
}

if (Input.GetKey(KeyCode.A))
{
direction -= transform.right;
}

direction.Normalize();


rb.velocity = direction * speed;
}

private void Jump()
{
if (isJumping == false)
{
isJumping = true;
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}

private void OnCollisionEnter(Collision collision)
{
if(isJumping == true)
{
isJumping= false;
}
}
// public's
public float speed;
public float jumpForce;

// private's
private Rigidbody rb;
private bool isJumping = false;

void Start()
{
transform.position = new Vector3(0,1,0);
rb = GetComponent<Rigidbody>();
}


void Update()
{
if(!isJumping)
{
Move();

if(Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
}
}

private void Move()
{
Vector3 direction = Vector3.zero;

if (Input.GetKey(KeyCode.W))
{
direction += transform.forward;
}

if (Input.GetKey(KeyCode.S))
{
direction -= transform.forward;
}

if (Input.GetKey(KeyCode.D))
{
direction += transform.right;
}

if (Input.GetKey(KeyCode.A))
{
direction -= transform.right;
}

direction.Normalize();


rb.velocity = direction * speed;
}

private void Jump()
{
if (isJumping == false)
{
isJumping = true;
rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
}
}

private void OnCollisionEnter(Collision collision)
{
if(isJumping == true)
{
isJumping= false;
}
}
B
BluDay16d ago
What would happen if you don't try setting the velocity property of the rigidbody directly? In Jump(), you are using the AddForce() method to change the velocity of rb In Move(), you are updating the velocity directly
B
BluDay16d ago
Unity Discussions
Player falls slowly like a feather
Hello everyone! I’m working on a 2D game wherein there is involvement of slopes. Now, within the case of jumping, the player jumps, reaches a height but falls back slowly like a feather. Similar behavior is seen in the case of player movement as well, when player moves, and at the time of falling, it falls down slowly. Note:- I’m using apply ...
N
nissemanen16d ago
thank you. its late for me so i cant read this now but in Move() ive already tryed to change the rb.velocity =... to rb.AddForce(...) but none of the setings gave me what i wanted. i gues ill try tomorow. thank you anyways!
B
BluDay16d ago
Sure thing! Try this when you have time:
c#
void Update()
{
if (!isJumping)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
else
{
Move();
}
}
}
c#
void Update()
{
if (!isJumping)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
else
{
Move();
}
}
}
N
nissemanen16d ago
thank you for the help! but i fixed it myself. i just had to add a "isGrounded" bool to see if i am tuching the gound. thanks anyways
B
BluDay16d ago
Good! Good luck
Want results from more Discord servers?
Add your server
More Posts
Model, Dto, Entity - Id?Hi, I'm messing with a simple CRUD application. My PatientEntity looks like this: public claRequest header field content-type is not allowedI have a third party API which uses SOAP + xml. I want to send a post request. Using Postman I can mRectangle intersection not workinUsing the code provided, I've tried the ways I knew to make the tripods remove once they're hit by aFeeding Random into itself - does it "corrupt the randomness"?If I am to reinitialize an instance of System.Random after every .NextDouble() using the return valuUpdate and add to Resource.resx file via code.We are adding localization to our product and want to automate the process of updating the resx fileWinform project saves the Image with a black backgroundIt works perfectly except it creates a black background which isn't shown in the picturebox preview,✅ Theoretical question. MediatR and DTO and onion structureHello, THe following is just theoretical and I do not need any code In the programming world, transfStack overflow exception when closing one formCan't seem to find the issue.. I have a startscreen, and when any key is pressed on startscreen it tAdvice / Guidance on VM application services (Windows Server 2016)Has anyone every come across a virtual machine (3 virtual 2.6Ghz processors, 12GB Ram) where a singlCan anyone help me with the odd prime numbers program?Facing difficulty in college assignment.IndexOutOfRange Leetcode helpHey, having an issue with my leetcode problem. I know that its supposed to be a bitwise XOR operatiovisual studio .net core ironPDFi have problem with export to pdf files. It's works perfectly( create a files and change keyword), bVisual Studio + WPF: "Could Not Load Assembly" despite dlls being available and reference correctThis has been driving me nuts for a day now. It's an intermittent error as had this working fine at how to export datas to json file from mysqlI have datas in mysql I want to move them to a json file. How can I do that?Licensing my programHi, i’m developing a Point Of Sale software and would like to find out possible solutions to manage ✅ Unhandled exception. Cannot print exception string because Exception.ToString() failed.justwhy this error always come in dotnet webapi application version ----- dotnet --version ✅ How to use Dapper with Query ParametersI work a lot with SQL databases such as MySQL, SQLite and PSQL and usually I write my Queries like t✅ transaction in transaction issueI have this error: `The connection is already in a transaction and cannot participate in another trHelp making login function c#Im currently trying to code a login function I have the registration my user is getting saved in thProblems with docker and my app imageI already posted a question that more or less manage to solve but now i have other problem, the thi