Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by korimako · Mar 02, 2015 at 03:08 AM · rotationmovementspherical

Make one GameObject look at, move toward another across a sphere

I have two game objects on a sphere. Using code derived form: http://wiki.unity3d.com/index.php/WalkOnSphere one object is moving on the sphere. I want the second object to orient toward the first and walk towards it.

I tried to get the Quaternion.Angle of the two object's Quaternions and then set that angle on the follower, that makes the follower walk toward the first object and then start circling it, which is actually something I'd like to be able to control with set radiuses, but was not intentional. Also the follower starts spinning on the spot if the target s moving too quickly across the sphere.

 using UnityEngine;
 using System.Collections;
 
 // SEE: http://answers.unity3d.com/questions/41050/how-can-i-make-movement-on-a-sphere.html
 
 public class CharacterBehavior : MonoBehaviour, ISphericalMoveable {
     
     private GameObject planet;
     public Vector3 direction = Vector3.one;
     public float radius = 6f; 
     public float angle = 0f; 
     public Quaternion rotation = Quaternion.identity;
     public Animator animator;
     public bool walkAround = false;
     public ISphericalMoveable targetChar;
 
 
     void Awake() {
         this.animator = this.transform.GetComponent<Animator>();
     }
 
     // Use this for initialization
     void Start () {
         this.planet = GameObject.Find("Planet");
         this.radius = this.planet.transform.localScale.y; 
     }
     
     public virtual void OnAnimatorMove()
     {    
         if( this.walkAround){
             
             // THE PROBLEM
             // Find the angle required to walk around the target
             // Part one of the problem
             //    - make the charater walk toward the character - angle toward it
             // Part two 
             //    - as you get closer to the target alter the angle  
             //  - so that when it gets really close the angle is always perpendicular to the direction it needs to travel in.
             
             // TODO: make this.angle a property of the animator - so that it can affect the animation. 
             /**
              * THIS WORKSISH - see description of problems in question above
             float targetAngle = Quaternion.Angle( rotation,  this.targetChar.GetRotation());
             this.angle = targetAngle;
             **/
 
 
         }
 
         if (this.animator)
         {
             this.direction = new Vector3(Mathf.Sin(this.angle), Mathf.Cos(this.angle));
             Translate(0, this.animator.GetFloat("Speed"));
             UpdatePositionRotation();
         }
 
 
 
     }
     
     void Translate(float x, float y)
     {
         Vector3 perpendicular = new Vector3(-direction.y, direction.x);
 
         Quaternion verticalRotation = Quaternion.AngleAxis(y * Time.deltaTime, perpendicular);
         Quaternion horizontalRotation = Quaternion.AngleAxis(x * Time.deltaTime, direction);
 
         rotation *= horizontalRotation * verticalRotation;
     }
     
     void UpdatePositionRotation()
     {
         transform.localPosition = rotation * Vector3.forward * radius;
         transform.rotation = rotation * Quaternion.LookRotation(direction, Vector3.forward);
     }
 
     public Quaternion GetRotation()
     {
         return this.rotation;
     }
 
     public Transform GetTransform()
     {
         return this.transform;
     }
 
     public void StartWalking()
     {
         this.animator.SetFloat("Speed", 4f);
     }
 
     public void StartWalkingAround(ISphericalMoveable character){
         this.walkAround = true;
         this.targetChar = character;
     }
     
 
 
     public void StopWalkingAround(){
         this.walkAround = false;
     }
 
 
 
 } 

 using UnityEngine;
 using System.Collections;
 
 public interface ISphericalMoveable
 {
     Quaternion GetRotation();
     Transform GetTransform();
 }

Comment
Add comment · Show 1
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 Statement · Oct 26, 2015 at 10:43 PM 0
Share

Here's an answer to a similar question, which rotates a game object toward another game object on a sphere. Don't know if it's of any use for you now but perhaps some lonesome soul search the site and finds it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mah_0003 · Mar 02, 2015 at 01:39 PM

Well, you could use walktowards.Transform.LookAt(); and as to walking towards said object, you could get the character to point at the object and then use vector3 changes. I'm not very good myself at automated walking or pathfinding, but a think using LookAt would make a lot of the code shorter. For help, try this: http://docs.unity3d.com/ScriptReference/Transform.LookAt.html

Just a small fix, Good luck.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GetKeyUp not registering 2 Answers

How can I keep my z-axis perpendicular to a target location while simultaneously rotating around the x-axis 1 Answer

Rotating object's xyz-axis but not its mesh 2 Answers

What is a correct way to move a Drone follow a list of Vector3 points 1 Answer

PlayerMovemeny Help with rotation? 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