- Home /
A Cube That I Pick Up Acts Kinematic, Although it Isn't.
I have a script set up that allows the player to click once to pick up an object that has an empty "marker" script attached. It then floats in front of you, and if you move, it follows you. You can click again to drop it. But the problem is that when I move the cube near a wall or the floor, it goes right through, as though it is Kinematic, although I have checked and double-checked that it isn't. I only turn off gravity for the object. Another really weird thing is that when I commented out the lines turning off gravity, it still floated in front of me when I tried to pick it up. This may turn out to be a really simple fix, but I can't figure it out.
Thanks in advance. `
Here is my complete code:
using UnityEngine;
using System.Collections;
public class PickupObject : $$anonymous$$onoBehaviour {
GameObject mainCamera;
bool carrying;
GameObject carriedObject;
public float distance;
public float smooth;
// Use this for initialization
void Start () {
mainCamera = GameObject.FindWithTag("$$anonymous$$ainCamera");
}
// Update is called once per frame
void Update () {
if(carrying) {
carry(carriedObject);
checkDrop();
} else {
pickup();
}
}
void rotateObject() {
carriedObject.transform.Rotate(5,10,15);
}
void carry(GameObject o) {
o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
o.transform.rotation = Quaternion.identity;
}
void pickup() {
if(Input.Get$$anonymous$$ouseButtonDown (0)) {
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x,y));
RaycastHit hit;
if(Physics.Raycast(ray, out hit)) {
Pickupable p = hit.collider.GetComponent<Pickupable>();
if(p != null) {
carrying = true;
carriedObject = p.gameObject;
p.gameObject.GetComponent<Rigidbody>().useGravity = false;
}
}
}
}
void checkDrop() {
if(Input.Get$$anonymous$$ouseButtonDown (0)) {
dropObject();
}
if(Input.Get$$anonymous$$ouseButtonDown(1)) {
carrying = false;
carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
carriedObject.GetComponent<Rigidbody>().velocity = transform.TransformDirection(Vector3.forward * 25);
carriedObject = null;
}
}
void dropObject() {
carrying = false;
carriedObject.gameObject.GetComponent<Rigidbody>().useGravity = true;
carriedObject = null;
}
}
Answer by J-R-Wood · Apr 14, 2015 at 12:23 AM
Best guess is it's "transform.forward" on row 33 of your code, from what i remember that would sometimes cause it to bust thru any collider.
Try using Rigidbody.Addforce maybe.
Well, when I throw it, it acts normally. Only while I'm holding it does it pass through objects.
Ok what about ins$$anonymous$$d of using this script when you first pick it up have the script set it's position to an empty Gameobject that is in front of the player but then ins$$anonymous$$d of having this script make it a child object to the player keep the rigidbody and collider on it when you want to set it down just make a script to detach it from the player
Answer by Ludwant · Apr 16, 2015 at 05:19 AM
Wow, it was a really stupid mistake: I had the smooth variable on the transform line set to 500 which completely made everything not work :D. Figured it out now, set it to 7..... Thank you so much for your suggestions though!
Your answer
Follow this Question
Related Questions
Kinematic rigidbodies collision 0 Answers
Can I detect a trigger message between a trigger and a kinematic rigidbody? 1 Answer
Rigidbody and Parenting - Kinematic Rigidbodies? 2 Answers
How to perform grasp action on a cloth? 3 Answers
Kinematic Rigidbody Collider not colliding with Static Trigger Collider 3 Answers