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 stingman · Apr 10, 2012 at 10:25 PM · rigidbodyquaternionrotatepathfindingwaypoint

Smoothly rotate a rigidbody enemy 180 degrees when reaching a waypoint and path back to original waypoint

Hey guys,

I've got a 2d platformer here and what I'm trying to do is have my enemy travel to one waypoint and then rotate 180 degrees (smoothly... I don't want to use transform.LookAt because that simply creates a choppy visual effect). I want to rotate him on the y axis 180 degrees to face the original waypoint and then travel back to that waypoint (and then loop the waypoint system). Right now my code has the enemy ping ponging from waypoint to waypoint. No rotation when reaching the next waypoint. Any ideas? Here is the script:

var pathpoint : Transform[];

var currentPathPoint : int;

var speed : float = 5;

var loop : boolean = true;

var rotateSpeed : float = 300;

function Awake (){

pathpoint[0] = transform; }

function Update(){

if(currentPathPoint < pathpoint.length){

var target : Vector3 = pathpoint[currentPathPoint].position;

var moveDirection : Vector3 = target - transform.position;

moveDirection.y = 0;

var velocity = rigidbody.velocity;

if(moveDirection.magnitude < 1){

currentPathPoint++;

enemyRotate = Quaternion.FromToRotation(Vector3.forward, moveDirection);

pathpoint[currentPathPoint].rotation = Quaternion.RotateTowards(pathpoint[currentPathPoint].rotation, enemyRotate, rotateSpeed * Time.deltaTime);

}

else{

velocity = moveDirection.normalized * speed; } }

else{

if(loop){ currentPathPoint = 0; } }

rigidbody.velocity = velocity;
}

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 Owen-Reynolds · Apr 11, 2012 at 12:20 AM

Your current code says that if you get within 1 of the target, you should go to the next waypoint and rotate for ONE FRAME (towards the old waypoint, even.) Next frame, you're far from the new waypoint, so you stop turning and only move. If you want "if not facing target, spin; else move", then say that:

 float moveAngle = ...
 // Vector3.Angle has an example of exactly how to get angle to a target

 if(moveAngle>2) { // just picking 2 degrees as "close enough", for no reason
   // your rotation code here
 }
 else {
   // your move code here
 }

I'm not sure why you use pathpoint[currentPathPoint].rotation instead of just transform.rotation (most people just care about waypoints' position,) but I'm sure you have a reason.

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 stingman · Apr 11, 2012 at 01:43 AM 0
Share

Thanks Owen got it working correctly

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

How to turn a car like in real world? 1 Answer

Player character X rotation going haywire on game start 1 Answer

Rotate rigidbody using MoveRotation() around the global y axis 2 Answers

Orientation Vs. Rotation: How do I specify more than 360deg rotation for a quaternion, in the editor? 1 Answer

RigidBody.velocity incidence on NavMeshAgent movement capacity 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