• 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
1
Question by BurnoutLance · Nov 13, 2014 at 02:08 PM · transform.position

transform.position not working for me. What is wrong with my code?

I simply do not understand what is wrong with my code. Everything compiles fine, but when I try to pick up an object, the object just gets shoved forward.

 // Lance Lindsey
 using UnityEngine;
 using System.Collections;
 
 public class Raycast : MonoBehaviour {
     private GameObject mainCamera; 
     private bool inSight;
     public string pickupMsg; 
     public float distance;
     public float smooth;
 
 
     // Use this for initialization
     void Start () {
         inSight = false;
         mainCamera = GameObject.FindWithTag ("MainCamera");
     
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 fwd = transform.TransformDirection (Vector3.forward);
         RaycastHit hit; 
 
         if(Physics.Raycast (transform.position, fwd, out hit))
         {
             Rigidbody rBody = hit.transform.gameObject.GetComponent<Rigidbody>();
             Pickupable p = hit.transform.gameObject.GetComponent<Pickupable>();
             //print (rBody);
             //print (p);
 
             if(rBody != null && p != null)
             {
                 GameObject obj = hit.transform.gameObject;
                 if(hit.distance <= 5 && obj != null)
                 {
                     inSight = true;
                     if(Input.GetKeyDown (KeyCode.E))
                     {
                         carryObject (obj);
                     }
                 }
                 else
                     inSight = false;
             }
         }
     
     }
     void carryObject(GameObject o)
     {
         o.rigidbody.useGravity = false;
         o.transform.position = Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);
     }
     void OnGUI()
     {
         GUI.Label (new Rect (Screen.width / 2, Screen.height / 2, 50, 50), "X");
 
         if (inSight == true) {
             int width = pickupMsg.Length * 10;
             GUI.Box(new Rect(10, 10, width, 20), pickupMsg);
         }
     }
 
 
 }
 


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
0

Answer by CHPedersen · Nov 13, 2014 at 02:09 PM

It seems to me that to pick up an object, its transform should be moved upwards, not forward. You're moving it forward in your lerp:

 Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.forward * distance, Time.deltaTime * smooth);

What happens if you change this to:

 Vector3.Lerp (o.transform.position, mainCamera.transform.position + mainCamera.transform.up * distance, Time.deltaTime * smooth);

Would that work?

Comment
Add comment · Show 3 · 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
avatar image BurnoutLance · Nov 14, 2014 at 01:33 PM 0
Share

When I change it from forward to up, it does almost the same thing. It just lifts it up and drops it.

avatar image CHPedersen · Nov 14, 2014 at 01:45 PM 0
Share

Well, what do you want to happen? Can you explain in more elaborate terms what behaviour you're looking for? This is unclear from your question.

"Picking up" an object could mean any of the following:

  1. The object disappears from the world and is added to an inventory

  2. An animation occurs that transfers the object graphically onto the player model and alters his state, e.g. picking up a dead body

  3. Grabbing hold of an object which hovers in front of the player or is held in hands and follows the player only while a key is held, e.g. dragging a crate Half-Life style.

Your code looks like you're trying to accomplish the third option.

avatar image BurnoutLance · Nov 14, 2014 at 03:50 PM 0
Share

I apologize for not being clear. You're exactly right about the third option. I really have no way to word it but I believe I know what is wrong with my code and it'll take a lot of reorganizing before I can get it to work.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I get the precise transform position in Update? 1 Answer

I'm trying to displace a UI object in the opposite direction I move a scrollbar 1 Answer

2D Game Checkpoint & Respawning 2 Answers

Varying behaviour of transform.position.localPosition.x in StateMachineBehaviour OnStateUpdate 1 Answer

Animate from instantiated GameObject 0 Answers


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