Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 cppietime · Dec 31, 2014 at 09:24 PM · cameradirectionspheremotiondirectional

Attempting to make sphere travel in direction of camera

As the title states, I'm trying to make a sphere travel in the direction of the camera upon pressing 'E'. Here's its script at the moment.

 #pragma strict
 
 var used = false;
 var time = 0;
 var direction = new Vector3(0, 0, 0);
 var target : Transform;
 
 function Start () {
     used = false;
     time = 0;
     direction = Camera.main.transform.forward;
 }
 
 function Update () {
     if(!used){
         direction = Camera.main.transform.forward;
         transform.position = target.position;
         if(Input.GetKeyDown(KeyCode.E)){
             used = true;
         }
     }
     else{
         transform.position = transform.position + direction * 10 * Time.timeDelta;
         time++;
     }
 }

There at least two problems with this that I've noticed so far, though. The first is that, upon pressing 'E', the sphere does not travel in the direction of 'direction', but rather simply goes straight to the left. The second problem is that 'direction' changes as the direction of the camera changes, as it should, but it does so before and after pressing 'E', even though it should only before pressing 'E'. The object has no gravity and its sphere collider is not a trigger. Any chance you can help me modify the script so it works how I want?

EDIT: O...k... for some reason, even if I COMPLETELY DELETE THE SCRIPT, the object this script is on just teleports straight to the camera and I cannot figure out why.

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 Eric5h5 · Dec 31, 2014 at 09:30 PM

You need to read the error messages in Unity, and fix them, before scripts can be used. In this case there is no "timeDelta" property of Time. (Not relevant, but your "time" variable is misnamed since it's counting frames, not time.)

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Camera/Direction Rotation 2 Answers

Rotate a sphere in the same direction the camera is pointing at 2 Answers

How do I get rid of this motion blur? 0 Answers

Change characters direction using camera view 0 Answers

Measuring XYZ coordinates in Unity vs in real life. 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