Answer the question
In order to leave comments, you need to log in
Unity how to add offset here?
I have a script here, but it's made in such a way that it stretches the line purely from point to point. Tell me how to make the line stretch from the sprite to the click location (WorldPos) - for example, offset, that is, not to a point, but a little closer.
here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class w : MonoBehaviour
{
public GameObject arrow_end;
public GameObject arrow_line;
public GameObject player;
public float arrow_end_offset;
void Start()
{
arrow_end.SetActive(false);
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
var mousePos = Input.mousePosition; mousePos.z = 60f;
var worldPos = Camera.main.ScreenToWorldPoint(mousePos);
var sprite_pos = player.transform.position;
var rot_diff = sprite_pos - worldPos;
var rotation = (Mathf.Atan2(rot_diff.y, rot_diff.x) * Mathf.Rad2Deg) - 0f;
float dist = Vector2.Distance(sprite_pos, worldPos);
arrow_end.transform.position = worldPos;
arrow_end.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotation);
arrow_end.transform.position += arrow_end_offset * - arrow_end.transform.up;
arrow_end.transform.gameObject.SetActive(true);
arrow_line.transform.position = (sprite_pos + worldPos) / 2f;
arrow_line.transform.right = Vector3.Normalize(sprite_pos - worldPos);
Vector3 scale = new Vector3(Vector3.Distance(worldPos, sprite_pos), 1f, 1f);
arrow_line.transform.localScale = scale;
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question