1

UNITY 2D C#

Hi everyone. I have a script that allows the arrow to show where the point is .

My problem:

1) I would like the arrow to show the way to a point with a specific tag or name.

How to do it?

Because now, shows the way to the point (-50,0).

Could anyone help me transform the script?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CodeMonkey.Utils;


public class Window_QuestPointer : MonoBehaviour {

[SerializeField] private Camera uiCamera;



private Vector3 targetPosition;
private RectTransform pointerRectTransform;

private void Awake ()
{
    targetPosition = new Vector3 (-50, 0);
    pointerRectTransform = transform.Find ("Pointer").GetComponent<RectTransform> ();
}
private void Update (){
    Vector3 toPosition = targetPosition;
    Vector3 fromPosition = Camera.main.transform.position;
    fromPosition.z = 0f;
    Vector3 dir = (toPosition - fromPosition).normalized;
    float angle = UtilsClass.GetAngleFromVectorFloat(dir);
    pointerRectTransform.localEulerAngles = new Vector3 (0, 0, angle);

    float borderSize = 40f;

    Vector3 targetPositionScreenPoint = Camera.main.WorldToScreenPoint (targetPosition);
    bool isOffscreen = targetPositionScreenPoint.x <= borderSize || targetPositionScreenPoint.x >= Screen.width - borderSize || targetPositionScreenPoint.y <= borderSize || targetPositionScreenPoint.y >= Screen.height - borderSize;
    Debug.Log (isOffscreen + " " + targetPositionScreenPoint);

    if(isOffscreen){
        Vector3 cappedTargetScreenPosition = targetPositionScreenPoint;
        cappedTargetScreenPosition.x = Mathf.Clamp (cappedTargetScreenPosition.x, borderSize, Screen.width - borderSize);
        cappedTargetScreenPosition.y = Mathf.Clamp (cappedTargetScreenPosition.y, borderSize, Screen.height - borderSize);

        Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (cappedTargetScreenPosition);
        pointerRectTransform.position = pointerWorldPosition;
        pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);

    }
    else{
        Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (targetPositionScreenPoint);
        pointerRectTransform.position = pointerWorldPosition;
        pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);

}

} }

Polan31
  • 11
  • 1
  • 5
  • 1
    Your object targetPosition is set to (-50,0) and never change. You will likely just have to change this value during runtime. – Skdy Oct 12 '18 at 12:18
  • and this can not be changed to find object with tag? – Polan31 Oct 12 '18 at 12:36

2 Answers2

0
  1. Tag the item "target"

  2. Add this to end of Awake method to fetch target

    Void Awake(){ Transform offscreen = GameObject.FindGameObjectWithTag("target").GetComponent(); TargetPosition=offscreen.position; }

Should do the trick. I typed this on my phone so it should be close pseudo. Let me know!

Technivorous
  • 1,682
  • 2
  • 16
  • 22
  • Thanks for the answer. Unfortunately, it does not work: Assets / Window_QuestPointer.cs (21.18): error CS0029: Can not implicitly convert type `UnityEngine.Transform 'to` UnityEngine.Vector3' Assets / Window_QuestPointer.cs (20.36): error CS0117: `UnityEngine.GameObject 'does not contain a definition for` FindObjectWithTag' But thanks :) – Polan31 Oct 12 '18 at 13:47
  • Sorry I'm on my phone. I forgot to get the vector three from transform set it to transform.position as for the tag.... – Technivorous Oct 12 '18 at 13:53
  • = GameObject.FindGameObjectsWithTag. I forgot the word game, again I'm on my phone. Apologies. I updated the answer and it should work now. – Technivorous Oct 12 '18 at 13:55
  • Still have little problem : Type `UnityEngine.GameObject' does not contain a definition for `getComponent' and no extension method `getComponent' of type `UnityEngine.GameObject' could be found. Are you missing an assembly reference? But I am already closer to solving the problem :) Bigthanks for the help :) – Polan31 Oct 12 '18 at 14:07
  • Just a typo hang on lemme get the right syntax. My wife is sleeping next to my computer and my keyboard is ridiculously loud. One second capitalize the 'g' in get – Technivorous Oct 12 '18 at 14:09
  • Yes, I know, I corrected it, but it did not do anything :) I'll sit over it a little bit and maybe I will find something :) again, thanks for the answer :) – Polan31 Oct 12 '18 at 14:22
  • Did u add the "target" tag to the object your looking for? Because everything is working on my end. – Technivorous Oct 12 '18 at 14:26
  • Yes :) But with an error I can not even press PLAY :) – Polan31 Oct 12 '18 at 14:31
  • This is why the documentation is so useful. [Here's the method you want](https://docs.unity3d.com/ScriptReference/GameObject.FindWithTag.html) – Draco18s no longer trusts SE Oct 12 '18 at 14:45
  • Try gameObject instead of GameObject – Technivorous Oct 12 '18 at 14:45
  • see i thought it was capitalized. thats the exact syntax i used before i edited. lol. thanks @draco – Technivorous Oct 12 '18 at 14:55
  • 1
    Ok guys, it was so easy...I admit that I'm ashamed. It was so simple that I should solve it immediately. I just changed: targetPosition = new Vector3 (-50, 0); to: targetPosition = GameObject.FindWithTag("Target").transform.position; – Polan31 Oct 12 '18 at 17:45
-1

Problem solving. Good script below :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using CodeMonkey.Utils;


public class Window_QuestPointer : MonoBehaviour {

[SerializeField] private Camera uiCamera;



private Vector3 targetPosition;
private RectTransform pointerRectTransform;

private void Awake ()
{
    targetPosition = GameObject.FindWithTag("Target").transform.position;
    pointerRectTransform = transform.Find ("Pointer").GetComponent<RectTransform> ();
}
private void Update (){
    Vector3 toPosition = targetPosition;
    Vector3 fromPosition = Camera.main.transform.position;
    fromPosition.z = 0f;
    Vector3 dir = (toPosition - fromPosition).normalized;
    float angle = UtilsClass.GetAngleFromVectorFloat(dir);
    pointerRectTransform.localEulerAngles = new Vector3 (0, 0, angle);

    float borderSize = 40f;

    Vector3 targetPositionScreenPoint = Camera.main.WorldToScreenPoint (targetPosition);
    bool isOffscreen = targetPositionScreenPoint.x <= borderSize || targetPositionScreenPoint.x >= Screen.width - borderSize || targetPositionScreenPoint.y <= borderSize || targetPositionScreenPoint.y >= Screen.height - borderSize;
    Debug.Log (isOffscreen + " " + targetPositionScreenPoint);

    if(isOffscreen){
        Vector3 cappedTargetScreenPosition = targetPositionScreenPoint;
        cappedTargetScreenPosition.x = Mathf.Clamp (cappedTargetScreenPosition.x, borderSize, Screen.width - borderSize);
        cappedTargetScreenPosition.y = Mathf.Clamp (cappedTargetScreenPosition.y, borderSize, Screen.height - borderSize);

        Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (cappedTargetScreenPosition);
        pointerRectTransform.position = pointerWorldPosition;
        pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);

    }
    else{
        Vector3 pointerWorldPosition = uiCamera.ScreenToWorldPoint (targetPositionScreenPoint);
        pointerRectTransform.position = pointerWorldPosition;
        pointerRectTransform.localPosition = new Vector3 (pointerRectTransform.localPosition.x, pointerRectTransform.localPosition.y, 0f);

}

} }

Polan31
  • 11
  • 1
  • 5