nissemanen
nissemanen
CC#
Created by nissemanen on 4/7/2024 in #help
Unity ray casting giving weird results
so I need ground checking in my game to see if you are touching the ground or not. but when I do the ray cast it says that I'm only grounded when I look in a specific place. like if it only checks when I'm looking in a perfect number of degrees. i am very new to coding in unity and i have no knowledge on what culd be the problem. here is a snipet of my code. just ask me if you need everything:
c#
void checkForGround()
{
Ray ray = new Ray(transform.position, new Vector3(0f, -1.1f, 0f));
RaycastHit hit;
Debug.DrawRay(transform.position, new Vector3(0f, -1.1f, 0f), Color.green);

if(Physics.Raycast(ray, out hit, 1, ~RayLayerMask))
{
Debug.Log("grounded " + hit.collider.gameObject.name);
isGrounded = true;
}else
{
Debug.Log("not grounded");
isGrounded = false;
}
}
c#
void checkForGround()
{
Ray ray = new Ray(transform.position, new Vector3(0f, -1.1f, 0f));
RaycastHit hit;
Debug.DrawRay(transform.position, new Vector3(0f, -1.1f, 0f), Color.green);

if(Physics.Raycast(ray, out hit, 1, ~RayLayerMask))
{
Debug.Log("grounded " + hit.collider.gameObject.name);
isGrounded = true;
}else
{
Debug.Log("not grounded");
isGrounded = false;
}
}
(this is run in Update())
12 replies
CC#
Created by nissemanen on 4/3/2024 in #help
minor glitch, player twitching
so i have a glitch where the player starts "twitching" when you walk beside a block or just on a block thats not a plane. ive tried a bunch of things and now im getting brain bleed. i have no idea why this is happening (plus ive got a automatic parkour movement without implementing it!). ill leave the code and a vid of me showcasing it down below. thanks in advance
c#
// public's
public float speed;
public float jumpForce;

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

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


void Update()
{
if (!isJumping && isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
else
{
Move();
}
}
}

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();

if (Input.GetKey(KeyCode.LeftShift))
{
rb.velocity = direction * (speed * 2);
}
else
{
rb.velocity = direction * speed;
}
}

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

}

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

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

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


void Update()
{
if (!isJumping && isGrounded)
{
if (Input.GetKeyDown(KeyCode.Space))
{
Jump();
}
else
{
Move();
}
}
}

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();

if (Input.GetKey(KeyCode.LeftShift))
{
rb.velocity = direction * (speed * 2);
}
else
{
rb.velocity = direction * speed;
}
}

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

}

private void OnCollisionEnter(Collision collision)
{
if(isJumping == true)
{
isJumping= false;
}
}
private void OnCollisionExit(Collision collision)
{
isGrounded= false;
}
private void OnCollisionStay(Collision collision)
{
isGrounded= true;
}
1 replies
CC#
Created by nissemanen on 4/2/2024 in #help
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;
}
}
10 replies