Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by _Keagan · Apr 28, 2017 at 12:36 PM · scripting problemphysicsrigidbodyscripting beginnerplayer movement

transform.forward not always forward?

I have coded a "Dash" co-routine it's very simple. It's using a start point and an end point. The "end point" is calculated as "transform.forward * dash distance"

     Vector3 dashStartPoint = transform.position;
     Vector3 dashEndPoint = transform.forward * dashDistance;

     float dashMovePercent = 0;

     while (dashMovePercent < 1) {

         dashMovePercent += dashSpeed * Time.deltaTime; 

         Vector3 position = Vector3.Lerp (dashStartPoint, dashEndPoint, dashMovePercent);

         transform.position = position;

         yield return null;
     }

Any other tips or problems with these calculations would be appreciated. By the way I use a rigidbody for general movment and as the co-routine starts I set the state to "Dashing" and the player can't do any general movment (with the right stick btw).

It should be noted that sometimes the dash goes in the right direction other it can go in some strange directions.

Regards and thanks in advance Keagan

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
2
Best Answer

Answer by Bunny83 · Apr 28, 2017 at 03:08 PM

transform.forward is a direction vector with the length 1.0. It's not a point in space. You would need to do:

 Vector3 dashStartPoint = transform.position;
 Vector3 dashEndPoint = dashStartPoint + transform.forward * dashDistance;

ps: Not many people seem to use a for loop in such cases, but to me it always seems to look cleaner:

 Vector3 dashStartPoint = transform.position;
 Vector3 dashEndPoint = dashStartPoint + transform.forward * dashDistance;
 for(float t = 0f; t < 1f; t += Time.deltaTime * dashSpeed)
 {
     transform.position = Vector3.Lerp (dashStartPoint, dashEndPoint, t);
     yield return null;
 }

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 _Keagan · Apr 28, 2017 at 03:15 PM 1
Share

Thanks @Bunny83 I realized earlier on today that that was the problem. I didn't realize at first since I would always just hit play and the player would dash from the origin so i didn't even noticed. Thanks. That would've been exactly the answer I was looking for but I gave up waiting because I am still a newbie and my questions take hours to get moderated.

avatar image
0

Answer by Igor_Vasiak · Apr 28, 2017 at 12:43 PM

Try this:

  private bool dash;
  private float speed = 120000;
  private Rigidbody rbody;

  void FixedUpdate ()
  {
       rbody = GetComponent<Rigidbody>();

       rbody.velocity = new Vector3(0, rbody.velocity.y, 0);

       if (Input.GetKeyDown(KeyCode.E))
            dash = true;

       if (Input.GetKeyUp(KeyCode.E))
            dash = false;

       if (dash)
            AddForce(transform.forward * speed * Time.fixedDeltaTime, ForceMode.Impulse);
  }

Hope this helps!

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 _Keagan · Apr 28, 2017 at 03:18 PM 0
Share

This kind of solves the solution in a different way but it was definitely not an answer to the question that I posted. Thank you anyway @Galahad_Hohan!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue with Instantiate and the instant that spawns on a moving object that rotates 1 Answer

Rigidbody set velocity on 1 axis without changing the others. 5 Answers

Help making orbiting planets,How to rotate around the objects with the largest mass? 0 Answers

How to apply a force after launching the player? 0 Answers

Use MoveRotation to Look At Another Object 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