4 Replies - 304 Views - Last Post: 19 September 2013 - 11:42 AM Rate Topic: -----

#1 canopyfive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-May 10

Turret firing help

Posted 19 September 2013 - 06:34 AM

I am trying to make this turret, which currently fires in a slightly random manner toward a player, instead lead the target. The help I've received thus far has suggested declaring a float time variable and calling the .magnitude function. I have absolutely no idea how the time or .magnitude function works.

I know to do this I need to find the player's speed, to tell the turret how far to lead the player but the syntax is totally breaking my soul here.

Any help?

Is This A Good Question/Topic? 0
  • +

Replies To: Turret firing help

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9059
  • View blog
  • Posts: 34,017
  • Joined: 12-June 08

Re: Turret firing help

Posted 19 September 2013 - 06:41 AM

Whatcha got for syntax so far?
Was This Post Helpful? 0
  • +
  • -

#3 canopyfive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 06-May 10

Re: Turret firing help

Posted 19 September 2013 - 06:48 AM

Well this is what my code looks like thus far, with the slightly random range of hits. Keep in mind I'm not a programmer and the little bit of code I have thus far has been a LOOOOONG painful process of trial and much more error.

void tryToshoot() {
		if(canSeePlayer && Time.time>=nextShotTime) {
			
			Vector3 playerRange = thePlayer.position;
			
			playerRange.x += Random.Range(-.5f, .5f);
			playerRange.z += Random.Range(-.5f, .5f);
			
			Vector3 bPos = transform.Find("tip").position;
			Transform newbullet = Instantiate(bulletPF, bPos, Quaternion.LookRotation(playerRange - bPos)) as Transform;
			
			const float BULLET_SPD = 20.0f;
			
			newbullet.rigidbody.velocity = newbullet.forward * BULLET_SPD;
			
			Destroy(newbullet.gameObject, 4.0f);
			
			nextShotTime = Time.time + 0.15f;
		}
	}



What I can't figure at this point is how to look up the player speed and then make the turret adjust to lead it.
Was This Post Helpful? 0
  • +
  • -

#4 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Turret firing help

Posted 19 September 2013 - 11:01 AM

View Postcanopyfive, on 19 September 2013 - 06:48 AM, said:

What I can't figure at this point is how to look up the player speed and then make the turret adjust to lead it.



You need to pass the Turret a reference to the Player object, so it can see the player's speed and direction.
Was This Post Helpful? 0
  • +
  • -

#5 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 271
  • View blog
  • Posts: 550
  • Joined: 07-July 10

Re: Turret firing help

Posted 19 September 2013 - 11:42 AM

He uses the Player object on line 4.

My personal tip not related to your question:
Spoiler


Your question is not a programming question. Your question is a game developer (math) question. That is why we always suggest you learn to program before you attempt game programming.

So you have two Positions in space, and know the current Vector and Velocity of one point, and the Velocity of the other point. "Simple" vector calculus will give you the the desired Vector for interception. Google the term "vector interception" to travel down the path of fun math.

My personal tip not related to your question:
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1