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
1
Question by MadJohny · Jul 18, 2013 at 07:55 PM · c#rotationslerp

Quaternion.slerp only on the y axis

Hey I've been making an AI script today, ran into alot of problems if you look at the qeustions I made only today... anyway this is another part of the script:

  using UnityEngine;
     using System.Collections;
 
 public class EnemyScript : MonoBehaviour {
 
     public int health = 100;
     public int rotationSpeed = 1;
     public int moveSpeed = 1;
     
     public GameObject[] gos;
     public GameObject closestPlayer = null;
     public float distance;
     
     void Start(){
         InvokeRepeating("FindClosestPlayer", 0.5f,0.5f);
     }
     
     void FixedUpdate(){
         if(closestPlayer != null){
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(closestPlayer.transform.position - transform.position), rotationSpeed * Time.deltaTime);
             transform.position += transform.forward * moveSpeed * Time.deltaTime;
         }
     }

ok the script does what I want, it looks at the player and wals forward at a certain speed, but it looks at the player in all axis, like if I jump he rotates on the x axis, and I don't want that to happen, I only want the Quaternion.Slerp work on the y axis, this is what I've tried to do:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyScript : MonoBehaviour {
 
     public int health = 100;
     public int rotationSpeed = 1;
     public int moveSpeed = 1;
     
     public GameObject[] gos;
     public GameObject closestPlayer = null;
     public float distance;
     
     void Start(){
         InvokeRepeating("FindClosestPlayer", 0.5f,0.5f);
     }
     
     void FixedUpdate(){
         if(closestPlayer != null){
             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(closestPlayer.transform.position - transform.position), rotationSpeed * Time.deltaTime);
             transform.position += transform.forward * moveSpeed * Time.deltaTime;
         }
         transform.rotation = Quaternion.Euler(new Vector3(0f,transform.rotation.y,0f));
     }

I don't know why but this way it doesn't rotate at all, like it goes straight forward, and doesn't rotate to look at the player, help would be apreciated.

Thanks in advance.

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

Answer by CgShady · Jul 18, 2013 at 08:19 PM

Hi, transform.rotation is a quaternion itself. using its y component doesn't give you what you expect.

try using transform.rotation = Quaternion.Euler(new Vector3(0f,transform.eulerAngles.y,0f)); http://docs.unity3d.com/Documentation/ScriptReference/Transform-eulerAngles.html

cheers

Comment
Add comment · Show 2 · 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 MadJohny · Jul 18, 2013 at 08:31 PM 0
Share

Thanks it worked :D , people are being so nice to me today :)

avatar image MadJohny · Jul 18, 2013 at 08:39 PM 0
Share

btw can I make rigidbodies collide but don't interact with each other?

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

Flip over an object (smooth transition) 3 Answers

Slerp doesnt go to the rotation its supposed to go to 1 Answer

Unit rotation fails consistently on all slerp rotations after the first? 0 Answers

HoloLens - Smooth Rotation doesn't work 0 Answers

Is it possible to use a quaternion from a gameobjects position to another gameobjects position? 1 Answer


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