I am relatively new to game development and am having a huge problem in my game scripts. I was following this tutorial, ->
I've gotten it to work fine for that purpose but I have been trying to add functionality for throwing and seem to have hit a wall (pun intended). It's a literal wall in fact, since every time I throw the object it goes in the same direction no matter which direction I am facing.
I have tried numerous other tutorials to no avail. Either they are outdated or not relevant or broken in some way that I cannot fathom.
I have also tried writing my own from scratch with multiple different ideas on how to accomplish the task , also to no avail.
Nothing I do seems to work and somewhere along the line I did something that broke my game completely. SO much so that even removing every script in the scene still rendered it unplayable....So now I am remaking it and am scared to death of the idea I might break the engine again.
Here are the things that I have tried so far:
Locking the rotation on pickup
Setting a new rotation on throw
setting the rotation to (0,0,0) then throwing
and numerous other slight tweaks to get it to go the direction it is facing
Unfortunately I am a giant noob and can't get anything I try to actually do what I want it to even though I thought I had a pretty good grasp on what I had to do....
Also the object rotates freely in my "hand" and moves when it bumps other objects but this still does not affect it's throw direction. I would love to be able to sort this problem at the same time.
Here is my code from the tutorial, made to work in Unity 5
I am not a particularly good programmer or anything so please break it down for a newbie if you don't mind. Also some explanation of why it works would be awesome so that I can acctually learn from it and apply the concepts in other projects down the road without having to make a forum post everytime I get stuck.
The goal is to be able to pick up an object with a rigidbody (and hopefully make it static in relation to my player while holding it) and throw it in the direction I am facing.
Thanks for any help in advance.
using UnityEngine;
using System.Collections;
public class GrabandDrop : MonoBehaviour {
GameObject grabbedObject;
float grabbedObjectSize;
// Use this for initialization
void Start () {
}
GameObject GetMouseHoverObject(float range){
//Check for Collider with Raycast
Vector3 position = gameObject.transform.position;
RaycastHit raycastHit;
Vector3 target = position + Camera.main.transform.forward * range;
if (Physics.Linecast(position, target, out raycastHit ))
return raycastHit.collider.gameObject;
return null;
}
void TryGrabbedObject(GameObject grabObject){
//Check for Object if not null Grab it
if (grabObject == null || !CanGrab(grabObject))
return;
grabbedObject = grabObject;
grabbedObjectSize = grabObject.GetComponent<Renderer>().bounds.size.magnitude;
}
//Can grab condition (RigidBody)
bool CanGrab(GameObject candidate){
return candidate.GetComponent<Rigidbody> () != null;
}
void DropObject(){
//Release Direction and Velocity
//Set multiplier larger for more force
grabbedObject.GetComponent<Rigidbody> ().velocity = Vector3.forward * 50;
//Check if hands are empty
if (grabbedObject = null)
return;
//if holding object
if (grabbedObject.GetComponent<Rigidbody> () != null)
//stop object before release
grabbedObject.GetComponent<Rigidbody> ().velocity = Vector3.zero;
}
// UPDATE is called once per frame
void Update () {
//Assign Input for Grab
if (Input.GetMouseButtonDown (1)) {
if (grabbedObject == null)
TryGrabbedObject (GetMouseHoverObject (5));
//Set Grab and Drop as Same button (Right Mouse Button)
else DropObject ();
}
if (grabbedObject != null) {
//Set position of grabbed Object after grabbing
Vector3 newposition = gameObject.transform.position + Camera.main.transform.forward * grabbedObjectSize;
grabbedObject.transform.position = newposition;
}
Debug.Log (GetMouseHoverObject (5));
}
}