Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by abuah1999 · Dec 29, 2021 at 01:33 PM · event triggering

How to draw an arrow in Unity

Hello Everyone.

I am trying to create a simple Chess UI program in unity and would love if the players could draw arrows on the screen. I am currently following very closely the code In this blogpost for how to draw arrows. Unfortunately, when I run my code in unity, all I get is a few tiny pink pixels that don't go very far away from the initial mouse position no matter how much I drag. Here is the script I have on the game object "board" which I want to draw the arrows on. `

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Board : MonoBehaviour
 {
     public GameObject controller;
     public float arrowheadSize;
     private Vector3 startPosition, mouseWorld;
     GameObject arrow;
     private LineRenderer arrowLine = new LineRenderer();
 
     public void Start(){
         arrow = GameObject.FindGameObjectWithTag("Arrow");
         arrowLine = arrow.GetComponent<LineRenderer>();
         mouseWorld = new Vector3 ();
         arrowheadSize = 1.2f;
     }
 
    public void OnMouseDown(){
        // mostly not relevant I think. All of my current testing happens when "selecton" is off.
         controller = GameObject.FindGameObjectWithTag("GameController");
         if (controller.GetComponent<Game>().selecton){
             controller.GetComponent<Game>().selecton = false;
             controller.GetComponent<Game>().selected. GetComponent<SpriteRenderer>().size -= new Vector2(0.03f, 0.03f);
             controller.GetComponent<Game>().selected. GetComponent<SpriteRenderer>().drawMode = SpriteDrawMode.Simple;
             controller.GetComponent<Game>().selected. GetComponent<Chessman>().DestroyMovePlates();
         } if (true) /*eventually this will be an actual conditional. 
         For now though I want this behavior to occur everytime the mouse is clicked*/ {
             // Here seems to be the problem... somewhere
             mouseWorld = Camera.main.ScreenToWorldPoint (
                 new Vector3 (Input.mousePosition.x,
                             Input.mousePosition.y,
                             Camera.main.nearClipPlane
                 ));
             startPosition = mouseWorld;
         }
     }
 
     public void OnMouseDrag(){
         //Turn on the arrow
         arrowLine.enabled = true;
         DrawArrow ();
     }
 
     public void DrawArrow(){
         mouseWorld = Camera.main.ScreenToWorldPoint (
                 new Vector3 (Input.mousePosition.x,
                 Input.mousePosition.y,
                 Camera.main.nearClipPlane
                 ));
         //The longer the line gets, the smaller relative to the entire line the arrowhead should be
         float percentSize = (float) (arrowheadSize / Vector3.Distance (startPosition, mouseWorld));
         //h/t ShawnFeatherly (http://answers.unity.com/answers/1330338/view.html)
         arrowLine.SetPosition (0, startPosition);
         arrowLine.SetPosition (1, Vector3.Lerp(startPosition, mouseWorld, 0.999f - percentSize));
         arrowLine.SetPosition (2, Vector3.Lerp (startPosition, mouseWorld, 1 - percentSize));
         arrowLine.SetPosition (3, mouseWorld);
         arrowLine.widthCurve = new AnimationCurve (
                 new Keyframe (0, 0.2f),
                 new Keyframe (0.999f - percentSize, 0.2f),
                 new Keyframe (1 - percentSize, 0.7f),
                 new Keyframe (1 - percentSize, 0.7f),
                 new Keyframe (1, 0f));
     }
 
     void OnMouseUp(){
         //Turn off the arrow
         arrowLine.enabled = false;
         RaycastHit hit;
         Physics.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition), out hit, 100);
         transform.position = new Vector3(hit.point.x, transform.position.y, hit.point.z);
     }
 }
 

I believe the problem is in OnMouseDown as when I set the if(true) to if(false), meaning that startPosition is never set to mouseWorld and always remains new Vector3() the arrow works fine! It can only point away from the center of the board though.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

132 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to use PointerEnter on 3D Object 1 Answer

Custom Event System, Coroutines, Callbacks? 1 Answer

How can I Take axis and turn wheel using pointer event from UI button for mobile platform..? 0 Answers

I want a border limit from which an object can be moved, c# 2 Answers

How do I asign this script to a button successfully? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges