hello I have this script Code (CSharp): using UnityEngine; using System.Collections; public class PlayerControl : MonoBehaviour { public Transform sightStart, sightEnd; public Animation idle; RaycastHit2D interacted; public RaycastHit hit; public float speed = 1.0f; float jumpTime, jumpDelay = .3f; bool jumped; Animator anim; public bool animation = true; public bool spottedenemy = false; void Start() { anim = GetComponent<Animator>(); } void Update() { transform.Translate(Vector3.right * speed * Time.deltaTime); Debug.DrawLine(Vector3.zero, Vector3.right, Color.red); Movement(); //call the function every frame RaycastStuff(); //call the function every frame if(Physics.Raycast (transform.position, transform.right, out hit, 100)) { Debug.DrawLine(transform.position, transform.forward); if (hit.collider.gameObject.tag == "Enemy") { speed = 0; Debug.Log("geraakt"); hit.transform.SendMessageUpwards("ApplyDamageToEnemy", 5); } } } void RaycastStuff() { //Debug.DrawLine(sightStart.position, sightEnd.position, Color.green); //spottedenemy = Physics2D.Linecast(sightStart.position, sightEnd.position,1<< LayerMask.NameToLayer("Enemy")); } void Movement() //function that stores all the movement { if(animation == true && spottedenemy == false) { speed = 2; // anim.SetFloat("speed", Mathf.Abs (-10F)); //transform.eulerAngles = new Vector2(0, 0); //this sets the rotation of the gameobject } else if(spottedenemy == true) { //animation = false; speed = 0; //anim.SetFloat("speed", 0); } else if(spottedenemy == false) { animation = true; } } } but for some reason it doesnt work i am sure i have the right tag but also the debug.log when it sees Enemy doesnt work when i debug the raycast i have a weird problem maybe that is the cause but i dont know how to fix it the red line stays there it doesnt go with the grey cube that is the player this script above is on the player
I'm not entirely sure wheat you are trying to achieve here, sorry But this line confuses me: Code (csharp): Debug.DrawLine(transform.position, transform.forward); thansform.forward is not a position, but a direction (local z axis direction in worldspace). So if you want to draw a line from the transform position, and then in the direction of the forward vector, you would write something like: Code (csharp): Debug.DrawLine(transform.position, transform.position+transform.forward*lengthOfLine); Now, as said I don't know your end goal here, but your debug line doesn't really make any sense to me, as it will always just draw a line from the position of the object to a position one unit from the origin of your worldspace. Which just has to be unintended
i want that my player so the grey cube is looking forward with a raycast so my player moves and a raycast needs to move out to that direction to see if there is any object in front of it and when it sees a object with the tag "Enemy" it shoot set the speed to 0 i know that forward it the right direction but for some reason it doesnt see the Enemy (red cube)
Ok, now I get your intentions That still doesn't really make your debug lines make any sense. 1) Your first debug line will always draw the same place, and will not move with your grey block. Is that your intention? If not, you have to make it relate to the objects position. 2) Are your SURE your object isn't rotated, and the axis you are looking at is the world axis? Check if your pivot is set to Global or Local. You are using the objects local x-axis (in world space), and so you have to make sure that is the right direction. 3) Your second debug line will always draw a line from the object's position, and to a position near the origin of world space. NOT forward. If you want it to draw a line forward, you have to add the objects position to the forward vector.