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 Purpose2 · Apr 09, 2014 at 10:36 PM · movementquaternionmoveeuler

Having trouble moving at right angles properly

Essentially I'm looking to make a game like ChuchuRocket if you are familiar with that seems simple enough for a newbie like me. Ultimately the first main step in this is to setup the "mouse" to run around the field automatically. However currently I'm having no success.

One of the main problems I am having is that I cannot seem to get the rotation/movement of the character clamped to the set movements, IE, 90 degree turns upon colliding with an object. Below is my current (heavily cut down) script for this, but I end up rotating excessively on my turn and wandering out in another direction rather than the hard 90 degree turn. Been doing a lot of reading on Quaternions but no love. :/

 using UnityEngine;
 using System.Collections;
 
 public class minionMovement : MonoBehaviour 
 {
     public float speed;
 
     void OnCollisionEnter()
     {
         transform.rotation = Quaternion.Euler(0, 90, 0);
     }
 
     void Update () 
     {
         transform.Translate(Vector3.forward * Time.deltaTime * speed);
     }
 }
Comment
Add comment · Show 2
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 LostInTime · Apr 10, 2014 at 01:23 AM 1
Share

Well, I don't think the rotation should be an issue there (just looking at the code...). But there is one thing I have to say about it. Despite what you are trying to do:

1) if you are using a rigidbody, when the objects collide they actually affect each other (apply forces and change directions) - applying rotations different from the one you are trying to get.

2) If you are just using a collider, it should not change trajectory after detecting collision, but I think there is a better way of achieving this. The problem I think that can happen is that, when the objects collide, they don't necessarily stop really that inch before bumping into the other object; sometimes they get a little bit inside one another before realizing they collided (therefore applying the rotation more times than the ones you wanted - because they get into each other, one of them turns, but they keep touching).

Simple suggestion: RayCast. When your raycast hits, it turns (this way it will only check collisions in one direction - if it collides, turns and doesn't give a crap anymore for the object beside him, just pays attention to the ones in front of him);

Of course, before you try to change anything just comment out the code inside "OnCollisionEnter()" and write "Debug.Log("Trying to turn");". This way you will see if there is rotations applied to the object other than the ones in the code and, at the same time, you get to know if it just detects the amount of collisions you want

avatar image ToxxicSin · Apr 10, 2014 at 01:25 AM 0
Share

Have you tried using the transform.Rotate(); function ins$$anonymous$$d?

2 Replies

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

Answer by Purpose2 · Apr 26, 2014 at 07:00 PM

Solved the problem using LostInTime's suggestion, it is a bit intensive using raycasts (especially because I plan on having a fair number of these... but its working which will allow me to progress for the time being)

 using UnityEngine;
 using System.Collections;
 
 public class minionMovement : MonoBehaviour 
 {
     public float speed;
     public float interactionDistance;
     bool hitAnObject;
     RaycastHit hit;
     float yRotation;
 
     void Update () 
     {
         transform.Translate(Vector3.forward * Time.deltaTime * speed);
 
         Debug.DrawRay(transform.position, transform.forward * interactionDistance, Color.green);
         
         if(Physics.Raycast (transform.position, transform.forward, out hit, interactionDistance))
         {
             hitAnObject = (hit.collider.tag == "Wall");
             if(hitAnObject)
             {
                 yRotation = yRotation + 90;
                 transform.rotation = Quaternion.Euler(0, yRotation, 0);
             }
         }
         else
         {
             hitAnObject = !hitAnObject;
         }
     }
 }
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 ToxxicSin · Apr 27, 2014 at 11:16 PM 0
Share

Ins$$anonymous$$d of doing transform.rotation, can't you just do transform.rotation.y = transform.rotation.y + 90; or transform.rotation = transform.rotation + (0, 90, 0); ?

avatar image
0

Answer by WhiteWolf93 · Apr 10, 2014 at 01:17 AM

Instead of moving forward what you need is to transform the direction.

Something like this maybe solves it:

 using UnityEngine;
 using System.Collections;
 
 public class minionMovement : MonoBehaviour 
 {
     public float speed;
     private Vector3 Direction;
 
     void Start()
     {
         Direction = Vector3.forward;
     }
     void OnCollisionEnter()
     {
         transform.rotation = Quaternion.Euler(0, 90, 0);
     }
     
     void Update () 
     {
         Direction = transform.TransformDirection(Direction);
         transform.Translate(Direction * Time.deltaTime * speed);
     }
 }
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 Purpose2 · Apr 26, 2014 at 05:29 PM 0
Share

Sadly is just causing it to bizarrely vibrate... I feel I may be best served by giving what LostInTime suggests - will report back incase other people have similar issues.

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

24 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

Related Questions

How to move object without physic? 1 Answer

How make child object rotate inverse to its parents rotation ? 1 Answer

Need help with storing Rotation in a Quaternion 1 Answer

Moving textures over a model 1 Answer

How to stop coroutines or functions 2 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