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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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.

Leave a Reply

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