C
C#4mo ago
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;
}
}
6 Replies
BluDay
BluDay4mo 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
BluDay
BluDay4mo 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 ...
nissemanen
nissemanen4mo 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!
BluDay
BluDay4mo 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();
}
}
}
nissemanen
nissemanen4mo 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
BluDay
BluDay4mo ago
Good! Good luck
Want results from more Discord servers?
Add your server
More Posts