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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by firestorm713q · Apr 01, 2013 at 08:55 PM · vector3transform.forward

Calculate x units forward?

Here's a link to the game, so you can see what's going on.

Here's my object Heirarchy:

alt text

Simply put, what I'm trying to do is have a laser shoot 10 units away from the player Mesh. If the player stays at (0,0,0), no problems. For some reason, the moment I move, the second vertex in the lineRenderer seems to stay in the same spot. Any clues?

 using UnityEngine;
     using System.Collections;
     
     public class Bangbang : MonoBehaviour {
         
         RaycastHit hit;
         Ray ray;
         LineRenderer lr;
         Transform GunPos;
         Transform Player;
         public Vector3 forwardpos;
     
         void Start () {
             lr = GetComponent<LineRenderer>();
             GunPos = transform.Find("Gun");
             Player = GameObject.Find ("PlayerMesh").transform;
             forwardpos.z = transform.forward.z * 10;
             forwardpos.x = transform.forward.x * 10;
         }
         
         void Update () 
         {
             forwardpos.z = transform.forward.z * 10;
             forwardpos.x = transform.forward.x * 10;
             if(Input.GetKey(KeyCode.Space))
             {
                 lr.enabled = true;
                 lr.SetPosition(0, GunPos.position);
                 lr.SetPosition(1, forwardpos);
                 if(Physics.Raycast(GunPos.position, transform.forward, out hit, 10.0f))
                 {
                     lr.SetPosition(1, hit.point);
                     Debug.DrawLine(GunPos.position, hit.point);
                 }
                 Debug.DrawLine(GunPos.position, forwardpos);
             }
             else lr.enabled = false;
         }
     }


heirarchy.png (2.6 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by fafase · Apr 01, 2013 at 10:46 PM

transform.forward is a unit vector meaning it is equal to (0,0,1) that is why you always get the same position, you would have to convert it to world position.

I would go for:

 forwardpos = transform.position + transform.forward*10;


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 Bunny83 · Apr 01, 2013 at 11:21 PM 0
Share

Almost. This:

    transform.TransformDirection(transform.forward*10);

doesn't make much sense ;) transform.forward is already a direction in worldspace. You treat it like a local space direction and transform it again into worldspace which would be quite rubbish.

transform.forward is the same as:

    transform.TransformDirection(Vector3.forward);

Vector3.forward is a constant which equals 0,0,1. transform.forward is the objects forward vector in worldspace.

So it should be simply:

    forwardpos = transform.position + transform.forward*10;
avatar image fafase · Apr 02, 2013 at 05:06 AM 0
Share

Oh yeah, my bad...even the doc starts with "The blue axis of the transform in world space."...

avatar image firestorm713q · Apr 02, 2013 at 03:48 PM 0
Share

lol you might want to get rid of the old one. But thanks, that worked perfectly!

avatar image
0

Answer by Nidre · Apr 01, 2013 at 09:01 PM

I cant be sure without seeing your object hierarchy but i presume that GunPos object is not a child of the mesh which is moving since you are calling GameObject.Find.

So are you sure GunPos object is also moving when player is moving ? Because u are using this objects position as start point for line renderer.

I would suggest adding GunPos object as child of player so it would always move with player.

Comment
Add comment · Show 1 · 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 firestorm713q · Apr 01, 2013 at 09:45 PM 0
Share

Updated my question with my object Heirarchy. The first position for the line renderer is following my Player$$anonymous$$esh object, but for whatever reason the second position is not being updated properly.

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

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there any way to translate normalized vectors into angles? 1 Answer

transform.forward doesn't correspond to rotation 1 Answer

transform.forward wobbles about instead of rolling 1 Answer

transform.forward breaks when changing scenes 1 Answer

"Fake" Vector3.forward? 3 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