Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

help with firing projectile, character controller + child object

$
0
0
I have a character controller (with a plane childed for sprite texture), (This is a 2D platform game). I also added an empty gameobject called "shuriken_launcher" which I am using as a launch point for my projectile. What I am stuck on is how to get the projectile firing off in the direction my character is facing so when walking left and fire pressed projectile goes <===== and facing right projectile goes =====> I am using the script below at the moment this only fire's right all the time as I lack the wording to fire along the child objects axis (assuming the child rotates with the character object it is childed to? private var ray : Ray; private var rayCastHit : RaycastHit; var bulletPrefab:Transform; var spawnRightObject : GameObject; //var mySound: AudioClip; function Start() { spawnRightObject = GameObject.Find("shuriken_launcher"); } function Update(){ if(Input.GetMouseButtonDown(0)){ ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Physics.Raycast (ray, rayCastHit)){ if(rayCastHit.transform.name == "fire_button"){ //audio.clip = mySound; //audio.Play(); var rightBullet=Instantiate(bulletPrefab, spawnRightObject.transform.position,Quaternion.identity); rightBullet.rigidbody.AddForce(1000,0,0); } } } } any pointers or help would be appriciated, thankyou for looking, I'm using javascript as I am still trying to wrap my head around programming! this is my character controller/movement script, would your solution apply to this? (thankyou for your quick response!) public var skin : GUISkin; //GUI skin public var mesh : GameObject; //Mesh public var texMove : Texture2D[]; //Move texture public var texJump : Texture2D; //Jump texture public var audioJump : AudioClip; //Jump sound public var audioDead : AudioClip; //Dead sound private var selectedTex : int; //Selected texture public var texUpdateTime : float; //Texture update time private var tmpTexUpdateTime : float; //Tmp texture update time public var moveSpeed : float; //Move speed public var jumpSpeed : float; //Jump speed public var gravity : float; //Gravity private var dir : Vector3; //The direction the player is movin private var rightTouchPad : GameObject; //Right touchpad private var leftTouchPad : GameObject; //Left touchpad var dead = false; //Are we dead private var controller : CharacterController; //The character controller var respawn : Transform; //respawn point var Ninja : GameObject; //player //try to add in ducking later...public var crouch : Texture2D; function Start () { //Find the character controller controller = GetComponent(CharacterController); //Screen orientation to landscape left Screen.orientation = ScreenOrientation.LandscapeLeft; //Find left touchpad leftTouchPad = GameObject.Find("LeftTouchPad"); //Find right touchpad rightTouchPad = GameObject.Find("RightTouchPad"); //Start SetupJoysticks StartCoroutine("SetupJoysticks"); //Set sleep time to never Screen.sleepTimeout = SleepTimeout.NeverSleep; } function Update () { //If we are not dead if (!dead) { //Update MoveUpdate(); TexUpdate(); } } function MoveUpdate() { //If we hit a object var hit : RaycastHit; if (Physics.Raycast(transform.position, Vector3.up, hit, 0.5f)) { //If it is not the player if (hit.transform.gameObject.tag != "goal") { //Set dir y to -1 dir.y = -1; } } //If we are grounded if (controller.isGrounded) { //If the game is not running on a android device if (Application.platform != RuntimePlatform.Android) { //Set dir x to Horizontal dir.x = Input.GetAxis("Horizontal") * moveSpeed; //If we get Space key down if (Input.GetKeyDown(KeyCode.Space)) { //Set dir y to jumpSpeed dir.y = jumpSpeed; //Play jump sound audio.clip = audioJump; audio.Play(); } } //If the game is running on a android device else { //Get left touchpad position x var pX = leftTouchPad.GetComponent(Joystick).position.x; //Get left touchpad tap count var tC = rightTouchPad.GetComponent(Joystick).tapCount; //Set dir x to touchpad x position dir.x = pX * moveSpeed; //If touchpad tap count are not 0 if (tC != 0) { //Set dir y to jumpSpeed dir.y = jumpSpeed; //animation.Play("somersault"); //Play jump sound audio.clip = audioJump; audio.Play(); } } } //If we are not grounded else { //Set dir y to gravity dir.y -= gravity * Time.deltaTime; } //Move the player controller.Move(dir * Time.smoothDeltaTime); } function TexUpdate() { //If we are not grounded if (!controller.isGrounded) { //Set main texture to jump texture mesh.renderer.material.mainTexture = texJump; return; } //If the game is not running on a android device if (Application.platform != RuntimePlatform.Android) { //Get Horizontal var h = Input.GetAxis("Horizontal"); //If Horizontal is not 0 if (h != 0) { //If Horizontal is bigger than 0 if (h > 0) { //Set scale to 1,1,1 mesh.transform.localScale = Vector3(1,1,1); } //If Horizontal is less than 0 else { //Set scale to -1,1,1 mesh.transform.localScale = Vector3(-1,1,1); } } //If Horizontal is 0 else { //Set main texture to move texture mesh.renderer.material.mainTexture = texMove[0]; return; } } //If the game is running on a android device else { //Get left touchpad x position var pX = leftTouchPad.GetComponent(Joystick).position.x; //If touchpad x position is not 0 if (pX != 0) { //If touchpad x position is bigger than 0 if (pX > 0) { //Set scale to 1,1,1 mesh.transform.localScale = Vector3(1,1,1); } //If touchpad x position is less than 0 else { //Set scale to -1,1,1 mesh.transform.localScale = Vector3(-1,1,1); } } else { //Set main texture to move texture mesh.renderer.material.mainTexture = texMove[0]; return; } } //If tmpTexUpdateTime is bigger than texUpdateTime if (tmpTexUpdateTime > texUpdateTime) { //Set tmpTexUpdateTime to 0 tmpTexUpdateTime = 0; //Add one to selectedTex selectedTex++; //If selectedTex si bigger than texMove.Length - 1 if (selectedTex > texMove.Length - 1) { //Set selectedTex to 0 selectedTex = 0; } //Set main texture to move texture mesh.renderer.material.mainTexture = texMove[selectedTex]; } else { //Add 1 to tmpTexUpdateTime tmpTexUpdateTime += 1 * Time.deltaTime; } } function OnTriggerEnter(other : Collider) { //If we are in a enemy trigger if (other.tag == "Enemy") { //Play dead sound audio.clip = audioDead; audio.Play(); //Dont show renderer mesh.renderer.enabled = false; //Kill dead = true; //testing respawn //wait for 1 second yield WaitForSeconds(1); //take a life off lives lives_counter.Counter -= 1; //turn back on mesh renderer with a few flashes first mesh.renderer.enabled = true; yield WaitForSeconds(0.1); mesh.renderer.enabled = false; yield WaitForSeconds(0.1); mesh.renderer.enabled = true; mesh.renderer.enabled = true; yield WaitForSeconds(0.1); mesh.renderer.enabled = false; yield WaitForSeconds(0.1); mesh.renderer.enabled = true; //make dead false again dead = false; } } // function OnGUI() // { // GUI.skin = skin; //Menu Button // if(GUI.Button(new Rect(Screen.width - 120,0,120,40),"Menu")) // { // Application.LoadLevel("Menu"); // } //If we are dead // if (dead) // { //Play Again Button // if(GUI.Button(new Rect(Screen.width / 2 - 90,Screen.height / 2 - 60,180,50),"Play Again")) // { // Application.LoadLevel("Game 3"); // } //Menu Button // if(GUI.Button(new Rect(Screen.width / 2 - 90,Screen.height / 2,180,50),"Menu")) // { // Application.LoadLevel("Menu"); // } // } // } function SetupJoysticks() { //Set touchpad position leftTouchPad.transform.position = Vector3(0,0,0); rightTouchPad.transform.position = Vector3(1,0,0); //Wait 1 second yield WaitForSeconds(1); //Start the touchpads leftTouchPad.GetComponent(Joystick).StartGame(); rightTouchPad.GetComponent(Joystick).StartGame(); }

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>