- Home /
The question is answered, right answer was accepted
Setting Without Altering Object Scale and Collider Scale
I am making a weapon/object pickup script and when I pickup the object and throw it the collider scale gets wacked out. It gets worse every time you pick up the object and throw it as well. I also tried scaling the object to be smaller but when I picked it up it reverted the scale. I am not sure what is causing this. Any help would be appreciated, thanks!
using UnityEngine;
using System.Collections;
public class GunControl : MonoBehaviour {
public bool auto = false;
public bool enabled = false;
public bool showGUI;
public float fireRate;
//float curTime;
public Transform _camera;
public float yPos;
public float xPos;
public float zPos;
float xRot;
float yRot;
float zRot;
float xrotv;
float yrotv;
float zrotv;
void Update () {
if(enabled == true){
xRot = Mathf.SmoothDamp(xRot, _camera.GetComponent<CameraControl>().currentXRot, ref xrotv, 0.1f);
yRot = Mathf.SmoothDamp(yRot, _camera.GetComponent<CameraControl>().currentYRot, ref yrotv, 0.1f);
//transform.forward = _camera.forward;
transform.rotation = Quaternion.Euler(xRot, yRot, 0);
//currentXRot = Mathf.SmoothDamp(currentXRot, verticalRot, ref xRotV, smoothDamp);
if(Input.GetButtonDown("Fire2")){
enabled = false;
transform.SetParent(null);
transform.rigidbody.isKinematic = false;
transform.collider.enabled = true;
rigidbody.AddForce(transform.forward * 1000f);
}
}else{
if(Vector3.Distance(transform.position, _camera.position) <= 3f){
showGUI = true;
}else{
if(Vector3.Distance(transform.position, _camera.position) > 3f){
showGUI = false;
}
}
}
if(showGUI && Input.GetKeyDown(KeyCode.E)){
enabled = true;
showGUI = false;
transform.SetParent(_camera);
transform.localPosition = new Vector3(_camera.localPosition.x + xPos, _camera.localPosition.y + yPos, _camera.localPosition.z + zPos);
transform.rigidbody.isKinematic = true;
transform.collider.enabled = false;
}
}
void OnGUI(){
if(showGUI){
GUI.Box (new Rect(Screen.width / 2, Screen.height / 2, 300, 50), "Pick up " + transform.name);
}
}
}
I guess this would happen if the scale of _camera wasn't 1:1:1. $$anonymous$$aybe setting the worldPositionStays flag to true would solve the issue.
Well I feel incredibly dumb right now, the scale of the camera got wacked out for some reason. And of course it fixed the problem! Thanks!
Follow this Question
Related Questions
Move hit.point 1 Answer
Can't Return the Tag of a Child Object 4 Answers
Merging Object 1 Answer
help hiding object? 2 Answers