Search Unity

Shoot +Animation+Locomotion Script

Discussion in 'Scripting' started by Vampire, Jan 18, 2011.

  1. Vampire

    Vampire

    Joined:
    Jan 16, 2011
    Posts:
    6
    Hello, i have a shoot script here that i need help with, what i am trying to do is add a part in the script to where when i press the "Fire1" button itll play an animation while it fires in the upperbody since the lower body is being processed with the locomotion. Can anyone help me with that?

    Code (csharp):
    1. var bullet : Transform;
    2. var Crosshairtex : Texture;
    3.  
    4. var target : Transform;
    5.  
    6. function Update () {
    7.  
    8.  
    9.     if (SimpleFPSWalker.toggleCombatMode){
    10.         if(Input.GetButtonDown("Fire1")){
    11.                 var hit: RaycastHit;
    12.            
    13.                 if (Physics.Raycast(target.position,target.forward, hit)) {
    14.                     var hitpoint = hit.point;
    15.                     transform.LookAt(hitpoint);
    16.                     var bullet1 = Instantiate(bullet, transform.position, Quaternion.identity);
    17.                     bullet1.transform.rotation = transform.rotation;
    18.                     bullet1.rigidbody.AddForce(transform.forward * 1000);
    19.             }
    20.         }
    21.     }
    22. }
    23.    
    24.  
    25.  
    26. function OnGUI(){
    27.     GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3
    28.     (Screen.width / 1024.0, Screen.height / 768.0, 1));
    29.     GUI.DrawTexture (Rect (500,372,24,24), Crosshairtex);
    30. }
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    Sure, you want to create a new animation clip from script that is based on existing clip, but add a mixing transform to it (adding a mixing transform to an animation clip makes that clip only effect the transform of the mixing transform and the transforms of the children of that transform).

    I generally just make an exposed var for the bone whose transform I want to use as the mixing transform, and drag it from the scene to the inspector of the object I attach this script to. In your case, you probably want to use whatever bone is acting as the upper spine (that has the torso, arms, head and hands as children). You would do something like this:
    Code (csharp):
    1.  
    2. var upperSpine : Transform;
    3.  
    4. function Start ()
    5. {
    6.     animation.AddClip(animation["shoot"].clip, "shootUpperBody");
    7.     animation["shootUpperBody"].AddMixingTransform(upperSpine);
    8. }
    From there, you just play the "shootUpperBody" animation clip whenever you want to play a shoot animation that only effects the upper body.
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Couple of things that I noticed...

    First, what I would do is create a dummy point at the end of the gun that you are firing from, call it firePoint. Make it a bullet and turn it's visibility off.

    Next, remove the Physics.raycast... it is not needed here.


    When you instantiate it, you will want it to be at the firePoint in the firePoint's rotation.

    var bullet1 = Instantiate(bullet, firePoint.position, firePoint.rotation);

    Then add velocity then in the direction forward of the bullet... (this way you dont have to build speed, its already there)

    bullet1.rigidbody.velocity = firePoint.TransformDirection(0,0,1000)-firePoint.position;


    Lastly, you will want to institute a fire rate.

    Code (csharp):
    1.  
    2. var bullet : Transform;
    3. var Crosshairtex : Texture;
    4. var firePoint : Transform;
    5. var fireSpeed = 1.0;
    6.  
    7. var target : Transform;
    8. private var nextFire=-fireSpeed;
    9.  
    10. function Update () {
    11.     if (SimpleFPSWalker.toggleCombatMode){
    12.         if(Input.GetButtonDown("Fire1")  nextFire<Time.time){
    13.             var bullet1 = Instantiate(bullet, firePoint.position, firePoint.rotation);
    14.             bullet1.rigidbody.velocity = firePoint.TransformDirection(0,0,1000)-firePoint.position;
    15.             nextFire=Time.time + fireSpeed;
    16.         }
    17.     }
    18. }
    19.    
    20.  
    21.  
    22. function OnGUI(){
    23.     GUI.matrix = Matrix4x4.TRS (Vector3.zero, Quaternion.identity, Vector3
    24.     (Screen.width / 1024.0, Screen.height / 768.0, 1));
    25.     GUI.DrawTexture (Rect (500,372,24,24), Crosshairtex);
    26. }
    27.  
     
  4. Vampire

    Vampire

    Joined:
    Jan 16, 2011
    Posts:
    6
    ok thank you, so far i am working on it, it doesnt work yet but im wondering in this line
    Code (csharp):
    1. AddMixingTransform(upperSpine);
    do you have to put at list of bones you want to of that part to animate?

    something like
    Code (csharp):
    1. AddMixingTransform(upperSpine/spine0/spine1/spine2);
     
  5. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    You could, I generally use a public var and drag it to the exposed var rather than combing the hierarchy of bones, it usually works for me. You can definitely do it that way though.

    I don't have Unity in front of me or I would find an example of how I did it, I could be wrong; haven't done it in a while.
     
  6. Vampire

    Vampire

    Joined:
    Jan 16, 2011
    Posts:
    6
    ok, i have the script now, but what im trying to do now is change this SimpleFPSWalker
    Code (csharp):
    1. if (SimpleFPSWalker.toggleCombatMode){
    into FPSCharacter Controller, but that script is a c sharp script, how can i make that work, so itll work with the locomotion. also what if I had the animation part of the code in this.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FPSCharacterController : MonoBehaviour {
    5.    
    6.     private CharacterMotor motor;
    7.    
    8.     public float walkMultiplier = 0.5f;
    9.     public bool defaultIsWalk = false;
    10.     public float runMultiplier = 5.0f;
    11.     public bool defaultIsRun = false;
    12.     // Use this for initialization
    13.     void Start () {
    14.         motor = GetComponent(typeof(CharacterMotor)) as CharacterMotor;
    15.         if (motor==null) Debug.Log("Motor is null!!");
    16.         motor.desiredFacingDirection = transform.forward;
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         // Get input vector from kayboard or analog stick and make it length 1 at most
    22.         Vector3 directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    23.         if (directionVector.magnitude>1) directionVector = directionVector.normalized;
    24.        
    25.         if (walkMultiplier!=1) {
    26.             if ( (Input.GetKey("left shift") || Input.GetKey("right shift")) != defaultIsWalk ) {
    27.                 directionVector *= walkMultiplier;
    28.             }
    29.         }
    30.        
    31.         if (runMultiplier!=2) {
    32.             if ( (Input.GetKey("left ctrl") || Input.GetKey("right ctrl")) != defaultIsRun ) {
    33.                 directionVector *= runMultiplier;
    34.             }
    35.         }
    36.         // Apply direction
    37.         motor.desiredMovementDirection = directionVector;
    38.     }
    39. }
    40.