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 /
  • Help Room /
avatar image
0
Question by FreeStyled · May 24, 2016 at 10:28 AM · c#transformpositionquaternion

Rotation set to face direction of movement but doesn't always work

Hi,

I've created a script which makes a capsule spawn in a random point and then moves to another random point and once that point is reached it recreates a new point in which to move to and it does.

Once I created this script I realised soon after running it that the rotation of the capsule needed to be in the direction of movement. I added a Quarternion which looks at the end point and then should rotate towards it but for some reason sometimes it works and sometimes it doesn't and that's where my problem lies. Hopefully someone can see what/where I've gone wrong and would be so helpful as to point this out to me and correct me.

(I understand that the Y axis also floats around and the capsule starts to spin because Y isn't set to 0 - I haven't looked into fixing this yet but if you notice anything that can be changed then please feel free to mention).

 using UnityEngine;
 using System.Collections;

 public class Movement : MonoBehaviour {

 public Vector3 startMarker;
 public Vector3 endMarker;
 public Vector3 currentPos;

 public float speed = 1.0f;

 private float startTime;
 private float journeyLength;

 void Start()
 {

     startMarker = new Vector3(Random.Range(-10f, 10f), 0f, Random.Range(-10f, 10f));
     endMarker = new Vector3(Random.Range(-10f, 10f), 0f, Random.Range(-10f, 10f));
     startTime = Time.time;
     journeyLength = Vector3.Distance(startMarker, endMarker);

 }       

 void ResetMovement()
 {
     startMarker = transform.position;
     startTime = Time.time;
     endMarker = new Vector3(Random.Range(-10f, 10f), 0f, Random.Range(-10f, 10f));
     
 }

 void EnemyMovement()
 {
     
     float distCovered = (Time.time - startTime) * speed;
     float fracJourney = distCovered / journeyLength;
     transform.position = Vector3.Lerp(startMarker, endMarker, fracJourney);

     Quaternion targetRotation = Quaternion.LookRotation(endMarker);
     transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime * 100);

     currentPos = transform.position;
     if (currentPos == endMarker)
     {
         ResetMovement();
     }
 }

 void Update()
 {
     EnemyMovement();
     
 }

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

Answer by FreeStyled · May 24, 2016 at 12:25 PM

OK managed to sort it out by following the Vector3.RotateTowards documentation and just altered the variables to my own. I've included the Debug.DrawRay in the new script just to show that the capsule is actually moving in the correct direction. I've also only included the function which I changed to minimise clutter. Hopefully someone else will benefit from this answer.

 void EnemyMovement()
 {
     
     float distCovered = (Time.time - startTime) * speed;
     float fracJourney = distCovered / journeyLength;
     transform.position = Vector3.Lerp(startMarker, endMarker, fracJourney);

     Vector3 targetRotation = endMarker - transform.position;
     Vector3 newDir = Vector3.RotateTowards(transform.forward, targetRotation, Time.deltaTime * 5, 0f);
     Debug.DrawRay(transform.position, newDir, Color.red);

     transform.rotation = Quaternion.LookRotation(newDir);

     currentPos = transform.position;
     if (currentPos == endMarker)
     {
         ResetMovement();
     }
 }


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

159 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 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Camera isn't move position? Why my camera isn't change position? 0 Answers

I am trying to say if the Y of my player reaches below 0 change the players position to 0,1,0 it wont work thought and I tried using Vector3 but it says I cant use Vector3 like a method Please help! 1 Answer

Position of empty game objects 1 Answer

Raycast only occurs on start 2 Answers

How to get the position of the Main Camera 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