Newb questions

Hi, I’m new to Unity but am enjoying it immensely so far. :slight_smile: Currently, I’m trying to set up a simple basketball game where there is a first person perspective player, a ball, and a simple basketball court. My goal is to have it so the player can shoot the ball from any position on the court. My first question is, how do I get the ball to shoot from the player’s position? Next question is how do I get the ball to shoot along the player’s Z axis? At the moment, I have a pretty sad script that will shoot the ball from a static location in the court. The player holds space bar to determine the power of the shot. Longer you hold, harder it shoots. Here is the script:

public class ShootBall : MonoBehaviour {
	
	Vector3 basePos = new Vector3(4, 4, 0);
	Vector3 shotPow = new Vector3(1, 4, 0);
	//Vector3 resetShotPow = new Vector3(270, 400, 0);	// change this when figure incrementing out
	Vector3 resetShotPow = new Vector3(0, 0, 0);	// change this when figure incrementing out
	
//	int score;
//	public GUIText countScore;
	
	void Start()
	{
		gameObject.transform.position = basePos;
		//score = 0;
		//setScoreText ();
	}

	void Update () 
	{		
		if(Input.GetKey (KeyCode.Space))
		{
			chargeShot();
		}	
		
		if(Input.GetKeyUp (KeyCode.Space))
		{
			Shoot ();
		}
	}
	
	void chargeShot()
	{
		if(shotPow.x < 500)
		{			
			shotPow.x = shotPow.x + (Time.deltaTime * 500.0f);
			shotPow.y = shotPow.y + (Time.deltaTime * 500.0f);
		}
	}
	
	void Shoot()	
	{
		gameObject.transform.position = basePos;		// return ball to starting/shooting location
		rigidbody.velocity = Vector3.zero;				// clear any velocity
		rigidbody.AddForce (shotPow);					// shoot the ball
		shotPow = resetShotPow;							// reset shotPow to original values
	}

Also, as you could probably tell from the script, I am using C#. I really only know C++ and so C# is much easier for me to understand than javascript. But I’ll take anything I can get!. Any other tips tricks and comments would be greatly appreciated. Thanks for your help! :smile:

you could attach this script to the player gameobject. or if it has its own gameobject create a public gameobject variable where you drag your player onto in the editor. this way you have a reference and can say player.transfrom.position.

transform.position.foward

This should be transform.forward

Hey guys, I received from help from a friend. Here is the modified code. It’s not perfect, but it’s a start for anyone who might run into a similar situation…

using UnityEngine;
using System.Collections;


public class ShootBall : MonoBehaviour {
	
	Vector3 basePos = new Vector3(4, 4, 0);
	
	float shotPow = 0.0f;
	
	void Start()
	{
		gameObject.transform.position = basePos;
		//score = 0;
		//setScoreText ();
	}

	void Update () 
	{		
		// Reposition the ball the first time the space key is pressed
		if(Input.GetKeyDown (KeyCode.Space))
		{
			// Added a tag to the Player_Controller called "Player"
			// Get access to player object and set position of the ball based on the position plus one unit in the
			// forward direction // this should be changed to update with the mousemove script so the ball follows the player
			UnityEngine.GameObject player  = GameObject.FindGameObjectWithTag("Player");
			rigidbody.rigidbody.useGravity = false;
			rigidbody.velocity = Vector3.zero;	
			gameObject.transform.position = player.transform.position + player.transform.forward;
		}
		
		if(Input.GetKey (KeyCode.Space))
		{
			chargeShot();
		}	
		
		if(Input.GetKeyUp (KeyCode.Space))
		{
			Shoot ();
		}
	}
	
	void chargeShot()
	{
		if(shotPow < 500)
		{			
			shotPow += Time.deltaTime * 500.0f;
		}
	}
	
	void Shoot()	
	{
		// Added a tag to the Player_Controller called "Player"
		UnityEngine.GameObject player = GameObject.FindGameObjectWithTag("Player");
		
		// return ball to starting/shooting location
		rigidbody.velocity = Vector3.zero;				// clear any velocity
		
		// Calculate the force based on the forward vector of the Player object // Scaling y with a magic number for now you should change thsi
		Vector3 force = new Vector3(shotPow * player.transform.forward.x, 500.0f + (shotPow * player.transform.forward.y), shotPow * player.transform.forward.z);
		rigidbody.AddForce (force);	// shoot the ball
		rigidbody.rigidbody.useGravity = true;
		shotPow = 0.0f;							// reset shotPow to original values
	}
/*	
	void setScoreText()
	{
		countScore.text = "Score: " + score.ToString;
	}
*/
}