- Home /
Scaling in the forward direction not working.
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
Assu$$anonymous$$g you are shooting the ray from the same point as the grapple, the calculation will be something like:
endScale = (hit.point - transfrom.position).magnitude;
In order to make this work, you likely have to start with a local.Z scale of the grapple of near 0.
Answer by robertbu · Jun 11, 2013 at 09:24 PM
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;
}
}
That line moves the cube in the forward direction to match the scaling so that the rear of the cube stays where it started. If I did not have this line, the cube would appear to grow/shoot both forward and backward. If you are creating your grapple in a 3D program, you can place your pivot at the rear end of the object and get rid of this line. It is only necessary for the standard cube that has the pivot in the center of the cube.
Answer by ChangeMe12345 · Aug 16, 2019 at 11:00 AM
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;
}
}
Your answer
Follow this Question
Related Questions
Dynamic line-drawing between objects (nGUI) 1 Answer
is it possible to scale from one side (not axe) ? 1 Answer
change the length of the vector 1 Answer
Scale Objects become flat. 1 Answer
Bake mesh scale. 1 Answer