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 dcolwell634 · Jul 20, 2013 at 04:27 AM · rotationcollisionaislerp

Slerp rotation on collision - C#

Hi all,

I'm having trouble with rotation on collision. Firstly, let me write what I'm trying to accomplish. I have an enemy (right now just a cube) that "wanders" around. When he hits a wall, I have him rotate but that part isn't working. When he hits the wall I would like it to rotate a random degrees and then continue moving. Currently he will hit the wall, rotate a few degrees and then keep going - like the rotation didn't have time to finish. Problem is with this setup, he will just drag along the wall because he didn't fully rotate in another direction.

So it's worth pointing out that the movement works fine. He hits the wall and I know this part is successful because the console reads that it hit the wall like I ask it to on the collision function. Its just the rotation part that doesn't work. Any suggestions?

Here is the code - or at least the important parts:

using System.Collections;

public class RotateOnCollisiont : MonoBehaviour {

 public float smooth = 2.0f;
 public float wanderSpeed;
 
 public bool wander;
 
 Vector3 wanderDirection;
 
 void Start () {        
     wander = true;
 }

 void Update()
 {
     Wander();
 }

 void Wander()
 {
     if (wander == true)
     {
         float wanderMovement = wanderSpeed * Time.deltaTime;
         
         wanderDirection = transform.position;
         
         rigidbody.velocity = transform.forward * wanderMovement;
         
         transform.LookAt(wanderDirection);

     }
 }
 void OnCollisionEnter(Collision collision)
 {
     if (collision.collider.tag == "Wall" && wander == true)
     {
         Debug.Log ("We hit a wall");
         wander = false;
         ChangeDirection();

     }
 }

 void ChangeDirection()
 {
 // Please note I only need it to rotate around the y axis:
     Quaternion target = Quaternion.Euler(0, Random.Range(45.0f, 270.0f), 0);
     transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
     wander = true;
     
 }

}

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

Answer by robertbu · Jul 20, 2013 at 06:53 AM

Your issue is line 46. OnCollisionEnter() only gets executed on the frame the collision occurs. Which means that ChangeDirection() only gets called for a single frame. So line 46 only gets call a single time. You can fix your code by moving line 46 into Update(). It will get called every frame, but no rotation will occur unless transform.rotation and target are different.

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 dcolwell634 · Aug 16, 2013 at 01:39 AM 0
Share

Thanks @robertbu - this makes sense. I don't see how I missed that.

avatar image
0

Answer by username707 · Jul 20, 2013 at 06:43 AM

Is cube having a rigid body? May that be the friction that cube wont complete rotation? Did you check target angle in console?

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

16 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

Related Questions

Basic AI Locked Axis 1 Answer

Follow and Rotate an object 1 Answer

Error with Rotating script 2 Answers

Jitter on Slerp - Raycast 0 Answers

Enemy AI don't collide with objects or rotate. 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