- 最后登录
- 2014-10-23
- 注册时间
- 2011-7-19
- 阅读权限
- 90
- 积分
- 81303
 
- 纳金币
- -1
- 精华
- 11
|
以上这段代码主要是运用四元数来确定子弹的角度,由鼠标的位置决定。下面是代码的详细讲解,直接看英文吧...
This code might be a bit tricky to understand but I hope you can bear with me. I wrote a tutorial about
Quaternions that would help explain this code you are welcome to view it here:
http://www.unitytutorials.com/document/280/understanding-quaternions-in-unity-3d
The first line is getting the inputRotation Vector, from the player's position to the mouse. By
multiplying the inputRotation Vector by the Quaternion.AngleAxis instruction I am effectively rotating
the vector by 8 degrees. I do this because I want to create the bullet slightly in front of the player, and
because the gun is slightly to the side I need to find the position which is the tip of the gun and I rotate
8 degrees to the side to find it. Alternatively you could attach an empty game object and make it a
child to your player, move it so it sits around the gun nozzle, create a public game object variable in the
player script and Instantiate all your bullets at this vector:
objGunNozzle.transform.position
Then as you move the gun nozzle object moves as well because it is a child of your player.
The second line of code says, get my position (transform.position), and add to it the direction that
tempVector is now pointing (8 degrees off from my initial position), and place it at 0.8 units from the
origin of the player, this position is the gun nozzel's position. Much simpler if you did it the first way
but it's a great exercise in understanding how you can rotate vectors.
切换到游戏模式下,单击左键,子弹并未发射出去,并与角色发生碰撞,下面继续完善代码。
Go ahead and run your game, run around and left click to create bullets. Right now the bullets don't
move and they collide with the player. Easy fix!
Find this line of code you just added:
Instantiate(ptrScriptVariable.objBullet, tempVector,
Quaternion.LookRotation(inputRotation) ); // create a bullet, and
rotate it based on the vector inputRotation
And replace it as follows:
GameObject objCreatedBullet = (GameObject)
Instantiate(ptrScriptVariable.objBullet, tempVector,
Quaternion.LookRotation(inputRotation) ); // create a bullet, and
rotate it based on the vector inputRotation
Physics.IgnoreCollision(objCreatedBullet.collider, collider);
Great you can build walls of bullets to stop that alien from getting to you! 'GameObject
objCreatedBullet = ' is the same as writing 'Float X = 1f' etc. Except when we call the instantiate
function, we must also specify the type of object being created because this is C# and that is why you
see the (GameObject) before the Instantiate function otherwise the instruction would not work.
下面的代码来实现子弹的运动,声明速度,生命周期等变量。
Let's get our bullets moving now, make the following changes to your BulletScript:
private float moveSpeed = 30f; // how fast the bullet moves
private float timeSpentAlive; // how long the bullet has stayed alive for
private GameObject objPlayer;
// Use this for initialization
void Start () {
objPlayer = (GameObject) GameObject.FindWithTag (" layer");
}
// Update is called once per frame
void Update () {
timeSpentAlive += Time.deltaTime;
if (timeSpentAlive > 1) // if we have been traveling for more than one
second remove the bullet
{
removeMe();
}
// move the bullet
transform.Translate(0, 0, moveSpeed * Time.deltaTime);
transform.position = new Vector3(transform.position.x,
0,transform.position.z); // because the bullet has a rigid body we don't want it
moving off it's Y axis
}
void removeMe ()
{
Destroy(gameObject);
}
void OnCollisionEnter(Collision Other)
{
if ( Other.gameObject.GetComponent( typeof(AIscript) ) != null &&
Other.gameObject != objPlayer ) // if we have hit a character and it is not the
player
{
removeMe();
}
removeMe(); // remove the bullet if it has hit something else apart from
an enemy character
}
Go ahead and see if your code works, if all is successful you should be running around shooting
bullets!
Let's see how we did this. Firstly we are counting the number of seconds our bullet has spent alive by
incrementing timeSpentAlive by Time.deltaTime. If the bullet has been alive for more than one second
we remove it. We do this to stop the bullet traveling forever.
transform.Translate is a great function. Each frame the bullet will move the same distance forward
regardless of what it collides with. We multiply moveSpeed by Time.deltaTime to make sure the
distance it moves is relative to the framerate. For example, if we were running at 30 frames a second
and moved it 1 unit every frame, in 1 second our object would have moved 30 units. But if the game
was running at 10 frames per second it would only move 10 units in 1 second. This is why we multiply
it by Time.deltaTime so that regardless of the framerate our object will always move the same amount
each frame.
The removeMe() function simply destroys the gameObject. The OnCollisionEnter function is a trigger
event that works for any object that owns a collider, whenever the collider (our bullet's box collider in
this case) hits another collider, it will call this function and the function will give us the name of the
collision we hit, in this case we are calling it Other. And we retrieve the gameObject by using
Other.gameObject. This way we can see if the game object we hit has the AIscript (if it does we know
we have hit a character), and we check to see if it is not the player, and now we remove the bullet. In
our next tutorial we will spawn particle effects when the bullet hits objects, apply damage to the alien,
create a death particle effect for the alien, and allow the player to take damage from the alien's melee
attack.
Don't Forget to save your scene Goto > File > Save Scene.
|
|