Quantcast
Channel: Answers for "Link gun dynamics!"
Viewing all articles
Browse latest Browse all 6

Answer by Bunny83

$
0
0
var currentTarget : Transform = null; var weaponRange : float = 20.0; function Update() { if (Input.GetMouseButton(0)) { if (currentTarget == null) { var ray = Camera.ViewportPointToRay(Vector3(0.5,0.5,0)); var hit : RaycastHit; if (Physics.Raycast(ray,hit,weaponRange)) { if (hit.transform.tag == "Player") { currentTarget = hit.transform; } else { // maybe do normal damage to other objects you can't lock on. // hit.transform refers to the object you hit } } } else { // We are locked on a target var vectorTotarget = currentTarget.position - transform.position; if (vectorTotarget.sqrMagnitude > weaponRange*weaponRange) // is the target out of range? { currentTarget = null; // clear target } ` `else { // Do damage to the target } } } else if(Input.GetMouseButtonUp(0)) { // when you release the fire button clear the target currentTarget = null; } } This script will shoot like every normal weapon, straight forward. If it hits a gmaeObject that is tagged "Player" it will lock to this target until you release the fire button. It's just the concept. You have to change/extend it to your needs...

Viewing all articles
Browse latest Browse all 6

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>