Guardians – Unity3D Group Project

Tonight I have modified Archer prefab to shot instead of casting ray. well the idea is the same but at the moment we have actual object of “Arrow” on a screen that is flying from bow to location selected as a point in middle of screen.

Arrow Behavior Script

#pragma strict
var followThisObject : GameObject;

var timer : float  = 0;
var lifeTime : float = 10;
var alive : boolean = true;

var layerMask : LayerMask; //make sure we aren't in this layer
var skinWidth : float = 0.1; //probably doesn't need to be changed
private var minimumExtent : float;
private var partialExtent : float;
private var sqrMinimumExtent : float;
private var previousPosition : Vector3;
private var myRigidbody : Rigidbody;

//Add forces to make it fly
function Start()
{
  transform.position = transform.position + new Vector3(0,1.2,0);
  //Camera.main.transform.forward
  this.rigidbody.AddForce(Camera.main.transform.forward.normalized*100);
}

//initialize values
function Awake() {
  myRigidbody = rigidbody;
  previousPosition = myRigidbody.position;
  minimumExtent = Mathf.Min(Mathf.Min(collider.bounds.extents.x, collider.bounds.extents.y), collider.bounds.extents.z);
  partialExtent = minimumExtent*(1.0 - skinWidth);
  sqrMinimumExtent = minimumExtent*minimumExtent;
}

function OnCollisionEnter(collision : Collision) {
  // Debug-draw all contact points and normals
  for (var contact : ContactPoint in collision.contacts) {
    Debug.DrawRay(contact.point, contact.normal, Color.white);
    //add other colider to the follow attribute
    onHitObject(contact.otherCollider.gameObject);
  }
  Debug.Log(collision.gameObject);
}

function onHitObject (objectHit : GameObject){
  followThisObject = objectHit;
}

function Update()
{
  if( timer>= lifeTime){
    alive = false;
  }
  if (followThisObject != null){
    transform.position = followThisObject.transform.position;
    transform.rotation = followThisObject.transform.rotation;
  }
  if(!alive){
    Network.Destroy(gameObject);
  }
  timer += Time.deltaTime;
}

function FixedUpdate() {
  //have we moved more than our minimum extent?
  var movementThisStep : Vector3 = myRigidbody.position - previousPosition;
  var movementSqrMagnitude : float = movementThisStep.sqrMagnitude;
  if (movementSqrMagnitude > sqrMinimumExtent) {
    var movementMagnitude : float = Mathf.Sqrt(movementSqrMagnitude);
    var hitInfo : RaycastHit;
    //check for obstructions we might have missed
    if (Physics.Raycast(previousPosition, movementThisStep, hitInfo, movementMagnitude, layerMask.value)){
      myRigidbody.position = hitInfo.point - (movementThisStep/movementMagnitude)*partialExtent;
    }
  }
  previousPosition = myRigidbody.position;
}

/*
function Start()
{
  var startPoint = Camera.main.transform.forward.normalized;
  var ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width/2, Screen.height/1.6));
  var hit : RaycastHit;
  if (Physics.Raycast (ray, hit, 500)) {
    var endPoint = hit.point;
    Debug.DrawLine (startPoint, endPoint, Color.red);
   //  var projectile = Network.Instantiate(explosionPrefab, hit.point, Quaternion.identity, 0);
   //  hit.transform.SendMessage("ApplyDamage", 10);
   // hit.transform.SendMessage("ApplyForce", hit.point * 1000);
   // canFire = false;
   // yield WaitForSeconds(attackCoolDown);
   // canFire = true;

    var normal = Vector3.Cross(startPoint, endPoint);
    var side = Vector3.Cross(normal, endPoint - startPoint);
    side.Normalize();

    var a = startPoint + side * (0.5);
    var b = startPoint + side * ( - 0.5);
    var c = endPoint + side * (0.5);
    var d = endPoint + side * (-0.5);
  }
}

function Update () {
}
*/

At the moment arrow sticks with center of colliding object.. Need to investigate that and apply fix.
Potential feature at this stage: arrow have trace in the air.

Published by

Luke Iwanski

Senior Graphics Programmer @ CD Projekt RED

Leave a Reply

Your email address will not be published. Required fields are marked *