PowenKo, Unity 3D, Input, Input.GetButtonUp ,Input.GetAxis

September 7, 2011   ·   0 Comments

using UnityEngine;
using System.Collections;

public class Input_GetButtonC : MonoBehaviour {

    private  string[] line=new string[5];
	public  GameObject[] thing=new GameObject[5];

	// Use this for initialization
	void Start () {

	}

	float fireRate = 0.5f;
	private float nextFire;// : float = 0.0;

	// Update is called once per frame
	void Update () {

		//http://unity3d.com/support/documentation/ScriptReference/KeyCode.Space.html?from=Input
    	//if (Input.GetKeyDown (KeyCode.Space ))
		/*
		if (Input.GetButton (KeyCode.UpArrow)){       Fun_Rotare(0,"UpArrow");
		}else if (Input.GetButtonUp  (KeyCode.UpArrow)){  Fun_Rotare(0,"");
		}else if (Input.GetButton (KeyCode.DownArrow)){  Fun_Rotare(1,"DownArrow");
		}else if (Input.GetButtonUp   (KeyCode.DownArrow)){  Fun_Rotare(1,"");
		}else if (Input.GetButton (KeyCode.LeftArrow)){  Fun_Rotare(2,"LeftArrow");
		}else if (Input.GetButtonUp   (KeyCode.LeftArrow)){  Fun_Rotare(2,"");
		}else if (Input.GetButton (KeyCode.RightArrow)){  Fun_Rotare(3,"RightArrow");
		}else if (Input.GetButtonUp   (KeyCode.RightArrow)){  Fun_Rotare(3,"");
		}else if (Input.GetButtonDown    (KeyCode.RightArrow)){  Fun_Rotare(3,"");
		}
		*/

		if (Input.GetButtonUp("Fire1") && Time.time > nextFire)
		{
			Fun_Add_bulet();
		}
		///////////
		//  left , right
		float speed=4f;
		float rotationSpeed=40f;
		line[0]=Input.GetAxis("Vertical").ToString();
		line[1]=Input.GetAxis("Horizontal").ToString();
		float translation = Input.GetAxis("Vertical") * speed;
        float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
        thing[0].transform.Translate(0, 0, translation);
        thing[0].transform.Rotate(0, rotation, 0);

		//////////
		line[2]=Input.GetAxis("Mouse X").ToString();
		line[3]=Input.GetAxis("Mouse Y").ToString();
		float h = rotationSpeed * Input.GetAxis("Mouse X");
        float v = rotationSpeed * Input.GetAxis("Mouse Y");
        thing[1].transform.Rotate(v, h, 0);

	}
	void Fun_Rotare(int i_objNo,string i_String){

		line[i_objNo]=i_String;
		if(i_String==""){
			thing[i_objNo].transform.Rotate(0,0,-60); //flip the turtle
		}else{
			thing[i_objNo].transform.Rotate(0,0,60); //flip the turtle
		}

	}
	void Fun_Add_bulet(){
		GameObject bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
        bullet.AddComponent("Rigidbody");

   	    Vector3 t1= bullet.transform.position = transform.position;
	   	t1.x=t1.x+4.0f;
		bullet.transform.position=t1;
  	 	bullet.transform.rotation= transform.rotation;
		bullet. rigidbody.velocity =new  Vector3(0,1f,0);
		bullet.rigidbody.AddForce (20, 0, 0);
   		bullet.rigidbody.mass = 0.5f;

		  bullet.rigidbody.velocity = transform.TransformDirection(Vector3.forward * 10);

	}

	void OnGUI () {
	GUI.color =new  Color(0,0.9f,0);
	GUI.Label (new Rect (0, 0, 500, 20), "PowenKo.com");
	GUI.Label (new Rect (20, 35, 500, 20),"  Vertical:"+ line[0]);
	GUI.Label (new Rect (20, 50, 500, 20),"Horizontal:"+ line[1]);
	GUI.Label (new Rect (20, 65, 500, 20),"   Mouse X:"+ line[2]);
	GUI.Label (new Rect (20, 80, 500, 20),"   Mouse Y:"+ line[3]);
	GUI.Label (new Rect (20, 95, 500, 20), line[4]);
	}
}

reference:

sample code:

b39_input_Input_GetButton

shareShare on FacebookShare on Google+Share on LinkedInEmail this to someonePrint this pageBuffer this pageDigg thisFlattr the authorShare on RedditPin on PinterestShare on StumbleUponShare on TumblrTweet about this on Twitter

By


Readers Comments (0)


You must be logged in to post a comment.

PowenKo’s Online Store

powenko_arduinoBook
Unity3D4

Amazon

adsbygoogle

Recent Posts

Categories

Amazon