- Home /
Question by
DemonJAZ · Jun 27, 2019 at 10:36 PM ·
collisioncontrollerraycasting2d-physicscharacter movement
Issues with 2d Player controller(Kinematic)
Hi guys,
I am facing issues with my player controller.
When i jump upon landing my character goes inside the ground.
marked in red are my rays. https://ibb.co/6b1pcTK
even the jump is to fast in up and down.
Link to project https://github.com/DemonJAZ/WalkInForest2DGame
@highpockets Below is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private bool grounded = false;
public float gravitymodifier = 1f;
public float jumpspeed = 1f;
public float movementSpeed;
private Vector2 Velocity;
private Rigidbody2D Pico;
// Start is called before the first frame update
void Start()
{
Pico = gameObject.GetComponent<Rigidbody2D>();
}
private void Update()
{
Debug.DrawRay(Pico.position + new Vector2(0.3f,-0.6f), Vector2.down*0.1f,Color.red);
Debug.DrawRay(Pico.position + new Vector2(-0.3f,-0.6f), Vector2.down * 0.1f, Color.red);
Debug.DrawRay(Pico.position + new Vector2(0f, -0.7f), Vector2.down * 0.1f, Color.red);
Debug.DrawRay(Pico.position + new Vector2(0.34f, 0.6f), Vector2.right * 0.1f, Color.red);
Debug.DrawRay(Pico.position + new Vector2(0.34f, 0f), Vector2.right * 0.1f, Color.red);
Debug.DrawRay(Pico.position + new Vector2(0.34f, -0.6f), Vector2.right * 0.1f, Color.red);
if (Input.GetKeyDown(KeyCode.Space) && grounded)
{
Velocity.y = jumpspeed * Time.deltaTime;
Pico.position += Velocity;
grounded = false;
}
}
// Update is called once per frame
private void FixedUpdate()
{
if (Input.GetKey(KeyCode.A))
{
Velocity.x = -movementSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.D))
{
Velocity.x = movementSpeed * Time.deltaTime;
}
else
{
Velocity.x = Vector2.zero.x;
}
if ( (Velocity.x != 0.0f) && HorizontalCollision() )
{
Velocity.x = Vector2.zero.x;
}
if (!grounded)
{
Velocity.y += gravitymodifier * Time.deltaTime;
}
if (VerticalCollisions() && !grounded)
{
Velocity.y = 0;
grounded = true;
}
Pico.position += Velocity;
}
private bool VerticalCollisions()
{
return Physics2D.Raycast(Pico.position + new Vector2(0.3f, -0.6f), Vector2.down, 0.1f) ||
Physics2D.Raycast(Pico.position + new Vector2(-0.3f, -0.6f), Vector2.down, 0.1f) ||
Physics2D.Raycast(Pico.position + new Vector2(0f, -0.7f), Vector2.down, 0.1f);
}
private bool HorizontalCollision()
{
float directionX = Mathf.Sign(Velocity.x);
bool v = (directionX == -1.0f);
Vector2 castDirection = v ? Vector2.left : Vector2.right;
return Physics2D.Raycast(Pico.position + new Vector2(directionX *.34f, -0.6f), castDirection, 0.1f) ||
Physics2D.Raycast(Pico.position + new Vector2(directionX * 0.34f, -0.6f), castDirection, 0.1f) ||
Physics2D.Raycast(Pico.position + new Vector2(directionX * 0.34f, -0.7f), castDirection, 0.1f);
}
}
Comment
Your answer
Follow this Question
Related Questions
Obstacle avoidance 1 Answer
How to make char animation idle 0 Answers
Linecast is always blocked. 2 Answers
CharacterController is stuck [video and screenshots] 0 Answers