- Home /
Question by
Rascanuvols · Jan 19, 2014 at 01:33 PM ·
colliderscontrolslopes
Problem with 2D slopes and walls
I'm trying to create my first controller script, trying to mimic the tutorials, and I'm having an issue when trying to go down a slope. Instead of walking smoothly down the slope, my character will just throw himself forward and then fall again, like this
I tried to modify the properties of my rigidbody, gravity and movement parameters, but even if I minimize it, this behaviour still appears to some degree.
I also found that when I touch a wall (vertical collider) midair, my characters stops falling as long as I press de directional button towards it.
I'll leave here my control script. I don't know what other information might be useful.
using UnityEngine;
using System.Collections;
public class moviment1: MonoBehaviour {
public float maxSpeed = 10f;
bool dreta = true;
public float jumpForce = 1000f;
bool firstJump = false;
private Transform groundCheck;
private bool grounded = false;
float groundRadius = 0.29f;
public LayerMask Terra;
void Awake()
{
groundCheck = transform.Find("groundCheck");
}
void FixedUpdate() {
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, Terra);
if (grounded) {
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
} else {
}
float move = Input.GetAxis("Horizontal");
if (move > 0 && !dreta)
flip ();
else if (move < 0 && dreta)
flip ();
rigidbody2D.velocity = new Vector2(move * maxSpeed, rigidbody2D.velocity.y);
if (Input.GetButtonDown ("Jump") && !grounded && firstJump) {
firstJump = false;
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
rigidbody2D.AddForce(new Vector2(0f, jumpForce));
}
if (Input.GetButtonDown ("Jump") && grounded) {
firstJump = true;
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
rigidbody2D.AddForce(new Vector2(0f, jumpForce));
}
}
void flip() {
dreta = !dreta;
Vector3 escala = transform.localScale;
escala.x = -escala.x;
transform.localScale = escala;
}
}
slope.jpg
(11.1 kB)
Comment