why its not working !!!

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
	
	public float maxSpeed = 30f;
	public int facingLeft = 0;
	
	Animator anim;
	public Transform RaycastPos;
	public LayerMask Obstacle;
	public Vector2 raycastDirection = -Vector2.right;
	public bool raycast = false;
	public RaycastHit2D hit

	void Start ()
	{
		anim = GetComponent<Animator> ();
	}
	 
	void Update ()
	{
		hit = Physics2D.Raycast (RaycastPos.position, raycastDirection, 0.1f)

		anim.SetFloat ("SpeedX", Mathf.Abs (rigidbody2D.velocity.x));
		anim.SetFloat ("SpeedY", Mathf.Abs (rigidbody2D.velocity.y));

		if(rigidbody2D.velocity.x != 0 || rigidbody2D.velocity.y != 0)
			rigidbody2D.AddForce(new Vector2(rigidbody2D.velocity.x*maxSpeed,rigidbody2D.velocity.y*maxSpeed));

		Raycast ();

	}
	void Right()
	{
		raycast = false;
		Raycast ();
		rigidbody2D.velocity = new Vector2 (maxSpeed, 0);
		transform.eulerAngles = new Vector3 (0, 180, 0); 
		facingLeft = 180; 
		raycastDirection = Vector2.right;
	}
	void Left()
	{
		raycast = false;
		Raycast ();
		rigidbody2D.velocity = new Vector2 (-maxSpeed, 0);
		transform.eulerAngles = new Vector3(0,0,0);
		facingLeft = 0;
		raycastDirection = -Vector2.right;
	}
	void Up()
	{
		raycast = false;
		Raycast ();
		rigidbody2D.velocity = new Vector2 (0, maxSpeed);
		transform.eulerAngles = new Vector3(0,0,-90);
		raycastDirection = Vector2.up;
	}
	void Down()
	{
		raycast = false;
		Raycast ();
		rigidbody2D.velocity = new Vector2 (0, -maxSpeed);
		transform.eulerAngles = new Vector3 (0, 0, 90);
		raycastDirection = -Vector2.up;
	}
	void Raycast()
	{
		if (hit.collider = "Obstacles" && !raycast)
		{
			rigidbody2D.velocity = new Vector2 (0, 0);
			raycast = true;
		}
		else
			Move ();
	}
	void Move()
	{
		if (anim.GetFloat ("SpeedX") == 0 && anim.GetFloat ("SpeedY") == 0) 
		{
			transform.eulerAngles = new Vector3(0,facingLeft,0);
			if (Input.GetKeyDown (KeyCode.D))
				Right();
			if (Input.GetKeyDown (KeyCode.A))
				Left ();
			if (Input.GetKeyDown (KeyCode.W))
				Up ();
			if (Input.GetKeyDown (KeyCode.S))
				Down ();
		}
		else
			return;
	}
}

the "Move ()" don't be applicated after the raycast (sorry for the English i am french)

–

> why it's dont work This is not a proper question for this site, please include a description of the problem you're having, including any exceptions/errors that may have been displayed when using this script. You can attach screen shots to your post for extra clarity if necessary.

–

1 Answer

1

You would get an exception/error in the RayCast method, you are setting a variable in the if, not comparing. If statements must include conditions that result in a true or false.

Your Code:

void Raycast()
{
    if (hit.collider = "Obstacles" && !raycast)
    {
        rigidbody2D.velocity = new Vector2 (0, 0);
        raycast = true;
    }
    else
        Move ();
}

Fixed(missing double equals):

void Raycast()
{
    if (hit.collider.name == "Obstacles" && !raycast)
    {
        rigidbody2D.velocity = new Vector2 (0, 0);
        raycast = true;
    }
    else
        Move ();
}

Furthermore, the collider object on hit is an object, you’re testing it’s equality to a string, did you mean to test “hit.collider.name” or “hit.collider.tag” against that string?

@Landern: Your suggestion at the end should be implemented in your fixed code, since your fixed code doesn't work as well. Also don't forget to check the collider for null before reading it's name or you get a null-ref-exception.

–

Thank you very much but its not working even if i put that

–

You need to include any error messages, honestly, that would have thrown an exception because you can't set in an if statement. Include the errors that you are recieving in FULL DETAIL, including the lines that it occurs. Otherwise it's a guessing game and you may not get any help. So help me help you.

–

@Bunny83, you're correct, i noticed it after my first submit, my bad.

–

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() i have found this in the console

–