Hi All,
I have been trying to find a tutorial on Picking and Throwing on object in Unity for a 2D project and well, I only found one quite old and well it’s useless for asking questions to.
I could tell there were problems with the script just after he starting showing the code and was typing it in line by line, like a good little boy. Until I ran into a problem I couldn’t solve, than I panicked got the whole code pasted it in and played around to get it in the order used in the project. Still didn’t like the look of it, fixed up all the linkages in the code as per the instructions and the code did indeed pick of the objects and throw it as per the title of the vid.
However, there was one major difference, with the code whenever the character picked up the object the character was immediately pushed continually backwards, like an addForce has been applied to the Player controller except I can’t figure out how or why?!? I tried setting the velocity to zero after getting the object but that didn’t work - so I’m completely lost as to what’s happening. So, I am posting the code being used in the hope that one of you may be able to tell me why my player is going backwards at a rate of knots, and also how I might be able to stop this motion. If you are able to help here is a big thankyou in advance.
Regards.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class grabberScript : MonoBehaviour {
public bool isGrabbed;
RaycastHit2D hit;
public float distance = 2f;
public Transform holdPoint;
public LayerMask notGrabbed;
//public LayerMask notGrabbed;
public float throwForce;
//stop moving right?!?
//??
Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.B))
{
if (!isGrabbed)
{
//grab object
Physics2D.queriesStartInColliders = false;
hit = Physics2D.Raycast(transform.position, Vector2.right * transform.localScale.x, distance);
if(hit.collider!= null && hit.collider.tag =="grabbable")
{
isGrabbed = true;
//rb.velocity = new Vector2(-1, 0);
}
}
//thowing
else if(!Physics2D.OverlapPoint(holdPoint.position,notGrabbed))
{
isGrabbed = false;
hit.collider.gameObject.GetComponent<Rigidbody2D>().velocity =
new Vector2(transform.localScale.x, 1) * throwForce;
}
}
if (isGrabbed)
{
hit.collider.gameObject.transform.position = holdPoint.position;
//rb.velocity = new Vector2(-10f, 0);
}
}//end Update
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, transform.position+ Vector3.right*transform.localScale.x*distance);
}//end OnDrawGizmos
}//end GrabberScript