Some FPS aiming code issues *solved*

got scripts? Need scripts?
Post Reply
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Some FPS aiming code issues *solved*

Post by Callan S. »

I messed around with unity awhile ago trying to make the basic elements of a FPS.

I managed to get a guy working who can run around and has a gun. Except the bullets dont fire out in line with the barrel. I think the issue would be in this line:

Code: Select all

var bullet = Instantiate(bulletfab, GameObject.Find("Barrel").transform.position, Quaternion.identity);
Also I made an enemy turret type thing, which rotates towards your position and fires. Okay, but as it gets closer to aiming at you, it slows down it's turning speed! This is really annoying - I want it to turn at a fixed rate and then when perfectly lined up with the player, just stay still. This is part of the code I'm using (if you need more I'll post the whole fire control code):

Code: Select all

if (LookAtTarget)
{
	var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

	//transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
	
	transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
	
	//var seconds : int = Time.time;
	//var oddeven = (seconds % 2);
	
	if (Time.time>=lastshottime+1) {lastshottime=Time.time;Shoot();}
}
Last edited by Callan S. on Tue Mar 20, 2012 1:28 am, edited 1 time in total.
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Shihonoryu
Posts: 255
Joined: Sun Dec 12, 2010 7:30 am

Re: Some FPS aiming code issues

Post by Shihonoryu »

Well i don't do javascript, but with your first problem, have you tried maybe making a empty game object at the tip of the barrel and making it spawn from there?

I
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Some FPS aiming code issues

Post by Callan S. »

Oh I do that - bullets come out. But when I aim at the sky, the bullets don't fire into the sky - they just fire directly forward of the characters heading. I need the bullets to use the barrels angle for the angle they come out at.

Okay, I looked up stuff and messed around and actually it was with where I added velocity, which is the next line in the code. I had

Code: Select all

var bullet = Instantiate(bulletfab, GameObject.Find("Barrel").transform.position, Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward*800);
But when I made it look for the barrel and transform it's particular direction 'forward'

Code: Select all

var bullet = Instantiate(bulletfab, GameObject.Find("Barrel").transform.position, Quaternion.identity);
bullet.rigidbody.AddForce(GameObject.Find("Barrel").transform.forward*800);
That worked! Now I can shoot up and down, sending a hail of fiery ping pong balls at my enemies! (yeah, I'm using visable bullets! I'll maybe do invisible ones in some future FPS if I ever make a bunch)



Now I just have the robot turrets rotation speed to deal with!!
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Shihonoryu
Posts: 255
Joined: Sun Dec 12, 2010 7:30 am

Re: Some FPS aiming code issues

Post by Shihonoryu »

I did those tutorials. Turrets were probably biggest pain.


But a excellent tut
User avatar
Callan S.
Posts: 2043
Joined: Sun Jan 24, 2010 5:43 am

Re: Some FPS aiming code issues

Post by Callan S. »

I went to unity answers and got this line, which makes it turn at a constant rate

Code: Select all

transform.rotation = Quaternion.RotateTowards(transform.rotation, rotate, rotationSpeed * Time.smoothDeltaTime);
Here's the entire code with that in it (including all my comments and crud I can't be bothered clearing out!)

Code: Select all

var LookAtTarget: Transform;
var blastPrefab: Transform;
private var lastshottime : int=0;
var rotationSpeed=0; // set in inspector to 20

function Update () {

if (LookAtTarget)
{
	
	//var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
	var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

	//transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
	
	//transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
	
	transform.rotation = Quaternion.RotateTowards(transform.rotation, rotate, rotationSpeed * Time.smoothDeltaTime);

	
	
	//var seconds : int = Time.time;
	//var oddeven = (seconds % 2);
	
	if (Time.time>=lastshottime+1) {lastshottime=Time.time;Shoot();}
}

//transform.LookAt(LookAtTarget);

}


function Shoot()
{
	var blast = Instantiate(blastPrefab, transform.Find("spawnpoint").transform.position, Quaternion.identity);
	
	blast.rigidbody.AddForce(transform.forward*1000);
}
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Post Reply

Return to “Scripting/Coding”