Tuesday, September 15, 2015

rotate a 2d arrow with mouse unity c#

iklan

About Arrow :-

  1. Create Empty Game Object.. Rename it  like 'Arrow'.
  2. Select 'Arrow'. Add Component ( Sprite Renderer ) to Inspector.
  3. Import 'Arrow.png' to Project. Select arrow.png and change Texture Type to Sprite (2D and UI)
  4. Change Pivot  to Bottom Left (According to my Sprite). You can change your pivot according to your Sprite (image). you should keep your pivot point to arrow last end.
  5. To arrange custom pivot point. You should click sprite editor and change point manually as like picture below. 

Code :-


using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {

    
    void Update () {
        var mousePos = Input.mousePosition;
        mousePos.z = 10.0f//The distance from the camera to the player object
        Vector3 lookPos  = Camera.main.ScreenToWorldPoint(mousePos);
        lookPos = lookPos - transform.position;
        float angle = Mathf.Atan2(lookPos.ylookPos.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angleVector3.forward);

        Debug.Log ("== "+Input.GetAxis("Mouse X"));

    }
    
}


You should change your pivot point using sprite editor.






rotate a 2d arrow with mouse unity c#
4/ 5
Oleh