• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RexxarTheHunter · Dec 26, 2015 at 11:18 AM · addforcethrowing

AddForce always pushes down

I'm trying to get my bearings around unity regarding the physics and AddForce doesnt behave as i'm expecting it to...

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class PickUpScript : MonoBehaviour
 {
     private GameObject _mainCamera;
     private bool _isCarrying;
     private GameObject _carriedObject;
     private Vector3 _relativeRotation;
     private float _accumelatedForce = 0;
     private bool _isThrowing;
 
     public float _distanceFromSelf;
     public float _transitionSmoothness;
 
     // Use this for initialization
     void Start()
     {
         _mainCamera = GameObject.FindWithTag("MainCamera");
     }
 
     // Update is called once per frame
     void Update()
     {
         if (_isCarrying)
         {
             CarryItem(_carriedObject);
             CheckThrow();
             CheckDrop();
         }
         else
         {
             PickUp();
         }
 
     }
 
     private void CheckThrow()
     {
         if (Input.GetKey(KeyCode.T))
         {
             _isThrowing = true;
             _accumelatedForce += Time.deltaTime;
         }
         else
         {
             if (_isThrowing)
             {
                 int x = Screen.width / 2;
                 int y = Screen.height / 2;
 
                 var ray = _mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y)).direction;
                 _carriedObject.GetComponent<Rigidbody>().AddForce(ray * _accumelatedForce, ForceMode.Impulse);
                 DropObject();
                 _accumelatedForce = 0;
                 _isThrowing = false;
             }
         }
     }
 
     private void PickUp()
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             int x = Screen.width / 2;
             int y = Screen.height / 2;
 
             var ray = _mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y));
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit, 10)
                 && hit.collider.GetComponent<PickUpable>() != null)
             {
                 _isCarrying = true;
                 _carriedObject = hit.collider.gameObject;
                 _relativeRotation = _carriedObject.transform.rotation.eulerAngles - gameObject.transform.rotation.eulerAngles;
             }
         }
     }
 
     private void CheckDrop()
     {
         if (Input.GetKeyDown(KeyCode.E))
         {
             DropObject();
         }
     }
 
     private void DropObject()
     {
         _isCarrying = false;
         _carriedObject = null;
     }
 
     private void CarryItem(GameObject item)
     {
         item.transform.position = _mainCamera.transform.position + _mainCamera.transform.forward * _distanceFromSelf;
         item.transform.LookAt(gameObject.transform);
         item.transform.eulerAngles += _relativeRotation;
     }
 }
 

on CheckThrow, i'm trying to push the object (a generic cube) which i'm holding away from me. but regardless of the force vector i'm setting in, the cube is awlays pushed down towards the ground. I've tried setting different vectors but no effect has been noticed...

any ideas?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by LazyElephant · Dec 26, 2015 at 01:08 PM

The problem is happening because you're directly changing the item's transform.position instead of moving it through the physics system. In order for that to work, you have to change the rigidbody on the item to kinematic.

To solve the problem, set the item's rigidbody.isKinematic to true inside the if statement starting at line 71. Then set isKinematic back to false inside your CheckThrow function before line 54. You'll also need to make sure isKinematic is set to false in your DropObject function in case you drop the item without throwing it.

http://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

What appears to be happening is, because the object is held above the ground, gravity is being applied. This is causing the downwards velocity to keep increasing as long as you hold the object. So, when you finally release the object, the force in the downward direction is much greater than your throwing force and the object flies downwards.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rigidbody2D linearDrag - Suddenly Change 0 Answers

`Inventory' does not contain a definition for `Add' 2 Answers

I need to add a force to an object based on the local vector of the object that is hitting it and not the world coordinates or local coordinates of the object being hit. 1 Answer

Facing the target 0 Answers

activate and speed in z axis from a pooled inactive object!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges