Sign in to follow this  

Unity Pickup and Throw Script Unity

Recommended Posts

I am relatively new to game development and am having a huge problem in my game scripts.  I was following this tutorial, -> https://www.youtube.com/watch?v=jOOdJZS987Y&t=99s , on how to pickup and drop objects.

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));

	
	}
}

Share this post


Link to post
Share on other sites

In unity you can rotate vectors with a quaternion multiplication.
There're also direction vectors, those vectors are representations of relative coordinates to the center of your object. 


Let's say you want to throw something to the right:

1) Transform.rotation.quaternion*vector(1,0,0) (or you can use transform.right). (I can't recall if it was vector*quaternion or backwards)
2) This will be the first step to your velocity vector, you can add magnitude if you multiply it with a float. 

Edited by RenzoCoppola

Share this post


Link to post
Share on other sites

Ah so that might explain why one of my solutions didn't work.  I was thinking it projected from the "front face" of the object (set by the direction of pickup) instead of the center of the object but I am still not sure when or how it determines the initial rotation.

I never really understood what that quaternions entail either even though I have scoured the unity docs and tried to make sense of the definition on wikipedia. One of my attempts used quaternion but I can't figure out how they work in the code.  Is there any chance you could provide a generic example of how to manipulate them on a game object?

Share this post


Link to post
Share on other sites

Imagine that quaternion multiplications are like applying a rotation (that means that quat1*quat2 isn't like quat2*quat1). You might get into the analytical stuff sooner or later, but you are ready to go over this problem. 

Edited by RenzoCoppola

Share this post


Link to post
Share on other sites

Maybe I am missing something but I have applied them at pickup and at throw and neither one actually does anything noticeable to the object.  Is there something I am missing? Is a parent/child thing? Do I set it or read it in?  Does it inherit the parent's position? Maybe something to do with rigidbodies?  Is it possible that my coding practices are shit?  I make 3d models and and such and can rig, animate, etc but the programming aspect is the achilles heel that keeps most of my games from ever being published.  Is there some fundamental concept that I obviously do not posses?  If there is such a thing I cannot find it on google or youtube?

 

ANyway, no matter when or where I apply the rotation tt seems it is always the rotation on the object's origin instead of it's current position/rotation. The few times I can make a noticeable difference it is always on some sort of skewed plane I didn't declare.  I know this problem should be an easy one but this is essentially the core mechanic of my game and though I have gone through countless iterations of solutions of the problem the real fix still eludes me. And last time I decided to try everything I broke something I couldn't fix even after deleting all of the scripts.  ANd I feel like trying new stuff is going to result in the same thing, me remaking the whole thing from scratch.The large majority of my game is ready for alpha testing but without this mechanic it is all for naught.  The weird thing is that I have been working on a multitude of scripts for the various mechanics and this is the ONLY one that refuses to cooperate. I am at my wits end here and am seriously  considering abandoning the game at this point.  Partially because of frustration but also because I have spent weeks on a part of the game I can't get to work and changing that part of the game means changing the whole thing, which I just can't do.  Either I finish this thing or give up and I am not sure how  I can continue trying to make games when I spend too much time trying to do it already.

Share this post


Link to post
Share on other sites

Difficult stuff the first time. You need to make sure that you are using the correct components.

Let's say that you have your script in the throwable item. 
You know your camera/shoot direction transform. You take your  Transform.Forward from that transform, you multiplcate by speed, and then you modifiy the RigidBody.Velocity vector directly (the rigidbody from your throwable). 

Edited by RenzoCoppola

Share this post


Link to post
Share on other sites

Not sure if a typo or something, but line 49 looks like you're setting grabbed object to null, rather than testing?

 

also line 45 you have :

 

grabbedObject.GetComponent<Rigidbody> ().velocity = Vector3.forward * 50;

 

when you _probably_ want something like :

 

grabbedObject.GetComponent<Rigidbody> ().velocity = grabbedObject.transform.forward * 50;

 

as your update code looks to be already setting the position/rotation of the object.

Share this post


Link to post
Share on other sites

So I tried using the transform.forward and at least now it can leave my hands at something other than the same direction every time, which is good.  But the problem still seems to be that it takes "forward" as a particular face on the object, in my case it seems to be the one that is facing "up" on the cube. When it rotates in my hand it keeps that face as forward but it is always spinning and I can't stop the rotation from in game aside from setting it down and picking it up again. I think the solution would be to set the rotation on pickup and lock it until I throw it but I still can't figure out how to implement that.  I know this is probably simple for some people but I am new to programming and though I understand some of the the basic concepts I am still having a hard time figuring out how to manipulate functions of objects and such and getting the results I expect from them.

Share this post


Link to post
Share on other sites

Finally figured it out.

 

I needed to set the rotation of the picked object like this:

var newRot = gameObject.GetComponent<Rigidbody> ().transform.rotation;
            grabbedObject.transform.rotation = newRot;

Really wish it wouldn't have taken me a week and a broken game to figure it out though.

Share this post


Link to post
Share on other sites

What you could also do is when you 'pickup' the grabbed object you could just parent it to the gameObject - would mean that any changes in position , rotation etc from the gameObject would be automatically transferred to the grabbed object.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      628735
    • Total Posts
      2984449
  • Similar Content

    • By INTwindwolf
      THE PROJECT

      INT is a 3D Sci-fi RPG with a strong emphasis on story, role playing, and innovative RPG features such as randomized companions. The focus is on the journey through a war-torn world with fast-paced combat against hordes of enemies. The player must accomplish quests like a traditional RPG, complete objectives, and meet lively crew members who will aid in the player's survival. Throughout the game you can side and complete missions through criminal cartels, and the two major combatants, the UCE and ACP, of the Interstellar Civil War.
      Please note that all of our current positions are remote work. You will not be required to travel.
      Talent Needed
       
      Unity Engine Programmer
      Website Administrator
      3D Animator
      We have made great strides in the year 2017! INT has received a comprehensive face-lift compared to the start of the year. We look forward to a productive, fruitful year 2018!
      Revenue-Share
      This is the perfect opportunity to get into the game development industry. Being an Indie team we do not have the creative restrictions often imposed by publishers or other third parties. We are extremely conscientious of our work and continuously uphold a high level of quality throughout our project.
      We are unable to offer wages or per-item payments at this time. However revenue-sharing from crowd-funding is offered to team members who contribute 15-20 hours per week to company projects, as well as maintain constant communication and adhere to deadlines. Currently the crowd-funding campaign is scheduled for the year 2018. Your understanding is dearly appreciated.
       
      Thank you for your time! We look forward to hearing from you!
       
      John Shen
      HR Lead
      Starboard Games LLC
    • By Apollo Cabrera
      Energy particles being harnessed by collection multi-hedron energy matrix. Whuuuttt?
      Love it :)
    • By AndySv
        Total Music Collection (http://u3d.as/Pxo)   THE COLLECTION CONTAINS:   Mega Game Music Collection   Universal Music Collection   Huge library of high quality music for any project! All at an incredibly low price!   - 2,5GB of high quality audio - 100+ different music tracks - Loop and short versions   Action, fantasy, casual, horror, puzzle, epic, dramatic, romantic, positive, inspiring, motivational and more!
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!



      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064

      View full story
    • By Dafu
      FES Retro Game Framework is now available on the Unity Asset Store for your kind consideration!
      FES was born when I set out to start a retro pixel game project. I was looking around for an engine to try next. I tried a number of things, from GameMaker, to Fantasy Consoles, to MonoGame and Godot and then ended up back at Unity. Unity is just unbeatable in it's cross-platform support, and ease of deployment, but it sure as heck gets in the way of proper retro pixel games!
      So I poured over the Unity pipeline and found the lowest levels I could tie into and bring up a new retro game engine inside of Unity, but with a completely different source-code-only, classic game-loop retro blitting and bleeping API. Months of polishing and tweaking later I ended up with FES.
      Some FES features:
      Pixel perfect rendering RGB and Indexed color mode, with palette swapping support Primitive shape rendering, lines, rectangles, ellipses, pixels Multi-layered tilemaps with TMX file support Offscreen rendering Text rendering, with text alignment, overflow settings, and custom pixel font support Clipping Sound and Music APIs Simplified Input handling Wide pixel support (think Atari 2600) Post processing and transition effects, such as scanlines, screen wipes, screen shake, fade, pixelate and more Deploy to all Unity supported platforms I've put in lots of hours into a very detail documentation, you can flip through it here to get an better glimpse at the features and general overview: http://www.pixeltrollgames.com/fes/docs/index.html
      FES is carefully designed and well optimized (see live stress test demo below). Internally it uses batching, it chunks tilemaps, is careful about memory allocations, and tries to be smart about any heavy operations.
      Please have a quick look at the screenshots and live demos below and let me know what you think! I'd love to hear some opinions, feedback and questions!
      I hope I've tickled your retro feels!



      More images at: https://imgur.com/a/LFMAc
      Live demo feature reel: https://simmer.io/@Dafu/fes
      Live blitting stress test: https://simmer.io/@Dafu/fes-drawstress
      Unity Asset Store: https://www.assetstore.unity3d.com/#!/content/102064
  • Popular Now