I am trying to make a grapple gun for my 2.5D game. I have a cube that I will use as the rope. How can I scale this cube to make it look like it is shooting out the front of my gun, no matter how the gun is rotated? It should also stop when it reaches the target. Thanks
EDIT:
Here is my new code @robertbu
public class GrappleGunScript : MonoBehaviour {
public float maxScale = 7.0f;
public float speed = 45.0f;
public Transform ropeSpawn;
public GameObject rope;
private bool hasShot = false;
private float orgScale;
private float endScale;
void Start () {
orgScale = transform.localScale.z;
endScale = orgScale;
}
void Update () {
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
if(!hasShot){
if(Input.GetMouseButtonDown(0)){
if(Physics.Raycast(ray, out hit, 15f)){
if(hit.collider.tag == "desk"){
hasShot = true;
Instantiate(rope, ropeSpawn.position, transform.rotation);
endScale = hit.point;
}
}
}
}
if(hasShot)
rope.transform.localScale = new Vector3(rope.transform.localScale.x, rope.transform.localScale.y, Mathf.MoveTowards(rope.transform.localScale.z, endScale, Time.deltaTime * speed));
}
}
At the point where it says endscale = hit.point.z
, this part is not working. It does not scale to the end where the raycast hits. How do I fix this? I’m not sure if it’s supposed to be on the z axis anyways. What do I do? Also, I only removed best answer so that others can answer as well but I will put it back once it has been answered. Thanks
This “fires” the cube in the direction of the positive ‘z’ for the cube. Hit ‘s’ for shoot, and ‘r’ for rewind. This does not stop when it reaches the target. I’m not sure how you want to handle that situation. You can Physics.Raycast() and reset endScale each frame, or you could do a Physics.SphereCheck() at the position of the point and stop it when it hits something:
#pragma strict
var maxScale = 7.0;
var speed = 15.0;
private var v3OrgPos : Vector3;
private var orgScale : float;
private var endScale : float;
function Start () {
v3OrgPos = transform.position;
orgScale = transform.localScale.z;
endScale = orgScale;
}
function Update () {
transform.localScale.z = Mathf.MoveTowards(transform.localScale.z, endScale, Time.deltaTime * speed);
transform.position = v3OrgPos + transform.forward * (transform.localScale.z / 2.0 + orgScale / 2.0);
if(Input.GetKeyDown(KeyCode.S)) {
endScale = maxScale;
}
else if (Input.GetKeyDown(KeyCode.R)) {
endScale = orgScale;
}
}
Thanks to the base, I update it 2019.
public float maxScale = 10f;
public float speed = 1f;
private Vector3 v3OrgPos;
private float orgScale;
private float endScale;
void Awake()
{
v3OrgPos = transform.position - transform.forward;
orgScale = transform.localScale.z;
endScale = orgScale;
}
void Update()
{
ResizeOn();
}
private void ResizeOn()
{
transform.localScale = new Vector3(transform.localScale.x, transform.localScale.y, Mathf.MoveTowards(transform.localScale.z, endScale, Time.deltaTime * speed));
transform.position = v3OrgPos + (transform.forward) * (transform.localScale.z / 2.0f + orgScale / 2.0f);
if (Input.GetKeyDown(KeyCode.S))
{
endScale = maxScale;
}
else if (Input.GetKeyDown(KeyCode.R))
{
endScale = orgScale;
}
}