I’m trying to make a collision detect from my character to the ground using Raycast2D but I keep getting errors and I don’t know what to change to make it work.
Below I’ve included the characters controller script and the errors.
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
//Floats and bools
public float jumpSpeed = 12f;
public float walkSpeed = 6f;
public bool isJumping = false;
float distToGround;
//Component calls
public Animator animator;
void Start () {
animator = GetComponent<Animator>();
distToGround = collider2D.bounds.extents.y;
}
//Class handler more or less
void Update () {
Movement ();
}
void IsGrounded(){
return Physics2D.Raycast(transform.position, -Vector2.up, distToGround + 0.1);
}
//Player movement handler
void Movement(){
if (Input.GetKey (KeyCode.D)) {
transform.Translate(Vector2.right * walkSpeed * Time.deltaTime);
//Turn 0 degrees when moving with the D key
transform.eulerAngles = new Vector2(0, 0);
//Set Speed float in animator to walkSpeed float
animator.SetFloat("Speed", walkSpeed);
}
//Reset animator float "Speed" to 0 when D key is released
if (Input.GetKeyUp (KeyCode.D)) {
animator.SetFloat("Speed", 0);
}
if (Input.GetKey (KeyCode.A)) {
transform.Translate (Vector2.right * walkSpeed * Time.deltaTime);
//turn 180 degrees when moving with the A key
transform.eulerAngles = new Vector2 (0, 180);
//Set Speed float in animator to walkSpeed float
animator.SetFloat ("Speed", walkSpeed);
}
//Reset animator float "Speed" to 0 when A key is released
if (Input.GetKeyUp (KeyCode.A)) {
animator.SetFloat("Speed", 0);
}
if (Input.GetKeyDown (KeyCode.W)) {
if(!isJumping && IsGrounded()){
//Jump handler + setting isJumping to true, no inf jumps here
rigidbody2D.AddForce(new Vector2(0, jumpSpeed), ForceMode2D.Impulse);
isJumping = true;
//Tells the animator that I'm jumping and loads the animation
animator.SetBool("playerJumping", true);
}
}
}
void OnCollisionEnter2D(Collision2D col){
if(col.gameObject.tag == "Ground"){
isJumping = false;
//Tells the animator that I'm grounded and to unload the animation
animator.SetBool("playerJumping", false);
}
}
}
Errors:
Assets/Scripts/PlayerController.cs(26,17): error CS0127: `PlayerController.IsGrounded()': A return keyword must not be followed by any expression when method returns void
Assets/Scripts/PlayerController.cs(26,34): error CS1502: The best overloaded method match for `UnityEngine.Physics2D.Raycast(UnityEngine.Vector2, UnityEngine.Vector2, float)’ has some invalid arguments
Assets/Scripts/PlayerController.cs(26,34): error CS1503: Argument #3' cannot convert
double’ expression to type `float’
Assets/Scripts/PlayerController.cs(56,29): error CS0019: Operator &&' cannot be applied to operands of type
bool’ and `void’