Tuesday, 15 September 2015
rotate a 2d arrow with mouse unity c#
About Arrow :-
- Create Empty Game Object.. Rename it like 'Arrow'.
- Select 'Arrow'. Add Component ( Sprite Renderer ) to Inspector.
- Import 'Arrow.png' to Project. Select arrow.png and change Texture Type to Sprite (2D and UI)
- 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.
- 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.y, lookPos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
Debug.Log ("== "+Input.GetAxis("Mouse X"));
}
}
You should change your pivot point using sprite editor.
model rotate with the mouse drag unity c#
using UnityEngine;
using System.Collections;
public class test : MonoBehaviour {
int speed = 12;
float friction = 0.5f;
float lerpSpeed = 1.5f;
float xDeg;
float yDeg;
Quaternion fromRotation;
Quaternion toRotation;
void Start () {
}
void Update () {
if(Input.GetMouseButton(0)) {
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,0);
transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
Debug.Log ("== "+toRotation);
}
}
}
using System.Collections;
public class test : MonoBehaviour {
int speed = 12;
float friction = 0.5f;
float lerpSpeed = 1.5f;
float xDeg;
float yDeg;
Quaternion fromRotation;
Quaternion toRotation;
void Start () {
}
void Update () {
if(Input.GetMouseButton(0)) {
xDeg -= Input.GetAxis("Mouse X") * speed * friction;
yDeg += Input.GetAxis("Mouse Y") * speed * friction;
fromRotation = transform.rotation;
toRotation = Quaternion.Euler(yDeg,xDeg,0);
transform.rotation = Quaternion.Lerp(fromRotation,toRotation,Time.deltaTime * lerpSpeed);
Debug.Log ("== "+toRotation);
}
}
}
how to display 3d text dynamically at unity
using UnityEngine;
using System.Collections;
public class PlayerNameScript : MonoBehaviour {
string playerName = "SatyaDev";
// Use this for initialization
void Start () {
photonView = this.GetComponent<PhotonView>();
}
// Update is called once per frame
void Update () {
GetComponent<TextMesh> ().text = playerName;
}
}
using System.Collections;
public class PlayerNameScript : MonoBehaviour {
string playerName = "SatyaDev";
// Use this for initialization
void Start () {
photonView = this.GetComponent<PhotonView>();
}
// Update is called once per frame
void Update () {
GetComponent<TextMesh> ().text = playerName;
}
}
Click to find the hit object RaycastHit unity
using UnityEngine;
using System.Collections;
public class ClickScript : MonoBehaviour {
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Input.GetButtonDown ("Fire2"))
{
if (Physics.Raycast(ray, out hit, 100))
{
Debug.Log("hit.tranform = "+hit.transform);
if (hit.collider.CompareTag("a"))
{
//Debug.Log("hit.tranform = "+hit.transform);
//Debug.Log("hit.tranform = "+hit.transform.tag);
}
else
{
//Debug.Log("hit.point = "+hit.point);
}
}
}
}
}
using System.Collections;
public class ClickScript : MonoBehaviour {
void Update ()
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Input.GetButtonDown ("Fire2"))
{
if (Physics.Raycast(ray, out hit, 100))
{
Debug.Log("hit.tranform = "+hit.transform);
if (hit.collider.CompareTag("a"))
{
//Debug.Log("hit.tranform = "+hit.transform);
//Debug.Log("hit.tranform = "+hit.transform.tag);
}
else
{
//Debug.Log("hit.point = "+hit.point);
}
}
}
}
}
int to string, string to int conversion c# unity
Code :-
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ConversionScript : MonoBehaviour {
int inumber = 12;
float fnumber = 24.5f;
string sname = "Satya";
int TestNumber;
float TestFnumber;
string TestName;
public InputField ScoreField;
int score;
public Text scoreText;
void Start () {
TestName = inumber.ToString(); // int to string conversion
TestNumber = int.Parse(sname); // string to int conversion
TestNumber = (int)fnumber; // float to int conversion
TestFnumber = inumber; // int to float conversion
TestName = fnumber.ToString(); // float to string conversion
}
void Update()
{
score = int.Parse(ScoreField.text);
Debug.Log ("Input field Score :"+score);
scoreText.text = score.ToString();
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ConversionScript : MonoBehaviour {
int inumber = 12;
float fnumber = 24.5f;
string sname = "Satya";
int TestNumber;
float TestFnumber;
string TestName;
public InputField ScoreField;
int score;
public Text scoreText;
void Start () {
TestName = inumber.ToString(); // int to string conversion
TestNumber = int.Parse(sname); // string to int conversion
TestNumber = (int)fnumber; // float to int conversion
TestFnumber = inumber; // int to float conversion
TestName = fnumber.ToString(); // float to string conversion
}
void Update()
{
score = int.Parse(ScoreField.text);
Debug.Log ("Input field Score :"+score);
scoreText.text = score.ToString();
}
}
Saturday, 12 September 2015
Simple Pendulum Movement unity
By using below script you can move the pendulum (mobile swipes)
Script :-
using UnityEngine;
using System.Collections;
public class TouchControl : MonoBehaviour {
public Rigidbody ball; // Drag your ball here
private Vector2 fp; // first finger position
private Vector2 lp; // last finger position
void Start() {
ball = GetComponent<Rigidbody>();
}
void FixedUpdate () {
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{
if((fp.x - lp.x) > 80) // left swipe
{
ball.AddForce(transform.forward * 150f);
}
else if((fp.x - lp.x) < -80) // right swipe
{
ball.AddForce(transform.forward * -150f);
}
}
}
}
}
Subscribe to:
Posts (Atom)










