Quantcast
Channel: Questions in topic: "error"
Viewing all articles
Browse latest Browse all 7934

why its not working !!!

$
0
0
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 (); } 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; } }

Viewing all articles
Browse latest Browse all 7934

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>