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 russelldc · Apr 19, 2013 at 03:27 AM · rotationmovementcharactercontroller

AI Character not rotating when spawned in

To give you some background, I have a button which spawns a prefab wherever I click/touch afterward to place it down. After that, I have a simple wandering script that changes direction every 3 seconds and travels forward using it's character controller.

The problem, however, is that if I drag the prefab into the editor and press play, the little fella moves fine, but if I use the button to spawn it in-game, it will only move forward, never turning. This is the script I use:

 using UnityEngine;
 using System.Collections;
  
 [RequireComponent(typeof(CharacterController))]
 public class Wander : MonoBehaviour {
     public float velocityMax;
   
     private float x;
     private float z; 
     private float time;
     private float angle;
     public bool isTouchWall;
  
     // Use this for initialization
     void Start () { 
         x = Random.Range(-velocityMax, velocityMax);
         z = Random.Range(-velocityMax, velocityMax);
         angle = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
         transform.rotation = Quaternion.Euler( 0, angle, 0);
         
         animation.wrapMode = WrapMode.Loop;
         animation.Play("walk");
         isTouchWall = false;
         
     }
     
     void OnCollisionStay(Collision collisionInfo) {
         foreach (ContactPoint contact in collisionInfo.contacts) {
             if (contact.otherCollider.gameObject.tag.Equals("Barrier")) {
                 isTouchWall = true;    
             }
         }
     }
  
     // Update is called once per frame
     void Update () {
          if (Time.timeScale >= 1) {
             time += Time.deltaTime; 
         
             if (time > 3.0f) {
                 x = Random.Range(-velocityMax, velocityMax);
                 z = Random.Range(-velocityMax, velocityMax);
                 angle = Mathf.Atan2(x, z) * (180 / 3.141592f) + 90;
                 
                 if (isTouchWall == true) { 
                     if (angle + 180 < 360)
                         angle += 180;
                     else
                         angle -= 180;
                     
                     isTouchWall = false;
                 }
                 
                 transform.rotation = Quaternion.Euler(0, angle, 0);
                 time = 0.0f;
                 Debug.Log("turn");
             }
             
             Vector3 forward = transform.TransformDirection(Vector3.forward);
             gameObject.GetComponent<CharacterController>().SimpleMove(forward * velocityMax); 
         }
     }
 }
Comment
Add comment · Show 4
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 ExpiredIndexCard · Apr 19, 2013 at 03:37 AM 0
Share

The angle is only changing when it touches a wall. Is that on purpose?

avatar image russelldc · Apr 19, 2013 at 03:56 AM 0
Share

No it changes right before that with angle = $$anonymous$$athf.Atan2(x, z) * (180 / 3.141592f) + 90;

avatar image ExpiredIndexCard · Apr 19, 2013 at 04:00 AM 0
Share

$$anonymous$$aybe it isn't rotating because Time.timeScale >= 1 is not true. would that be the problem? I really cant think of anything else

avatar image russelldc · Apr 19, 2013 at 04:08 AM 0
Share

Nah, it does everything else in the method except rotate. And like I said in the initial post, it acts completely normal if I place it into the world by just dragging it in. It's only if I instantiate it that it doesn't turn. It still moves forward and prints the debug log.

0 Replies

· Add your reply
  • Sort: 

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

11 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

Related Questions

How to change the angle my character falls at 1 Answer

Player model rotates forward with camera 0 Answers

How can I get my model to face the direction it is moving? 2 Answers

[2D] CharacterController rotation 0 Answers

Face direction to move in 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