Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CyberAngel · Apr 11, 2018 at 03:50 PM · unity 2dtransform.rotation

How to move sprites in the opposite direction to player.

I have a sprite on the screen, that is static in its position and when I use the keys/controller it rotates in the direction it is supposed to go in. For testing purposes I have another sprite that is moving in the x direction with the following code.

     protected virtual void Step()
     {
         Vector3 direction = new Vector2(Mathf.Sin(Mathf.Deg2Rad * 90), Mathf.Cos(Mathf.Deg2Rad * 90));
 
         Vector3 position = transform.position;
 
         position.x += direction.x * 2 * Time.deltaTime;
         position.y += direction.y * 2 * Time.deltaTime;
 
         transform.position = position;
 
     }

This is an abstract class that can be inherited by other objects, to provide different speeds. So the sprite on the screen then has this in the code.

     private void Update()
     {
         Step();
     }
 
     protected override void Step()
     {
         base.Step();
 
         Vector3 direction = new Vector2(Mathf.Sin(Mathf.Deg2Rad * playerScript.oppositeDirection()), Mathf.Cos(Mathf.Deg2Rad * playerScript.oppositeDirection()));
 
         position = transform.position;
         position.x += direction.x * 4 * Time.deltaTime;
         position.y += direction.y * 4 * Time.deltaTime;
         if (position.x > 16)
         {
             position.x = -16;
         }
         transform.position = position;
     }

For testing purposes I have it wrapping back to the left of screen once it goes of the screen on the right.

Now the player script, has a public field that is supposed to get the opposite direction the player is going in and provide the second sprite a different movement. To explain this, think of the player sprite moving towards the sprite then the sprite would appear to arrive at the player quicker. If the player is going away from the other sprite, then it is supposed to gradually move away from the other sprite.

The code on the player script, is as follows.

     private void FixedUpdate()
     {
         if (yRot != 0 || xRot != 0)
         {
             angle = Mathf.Atan2(yRot, xRot) * Mathf.Rad2Deg;
             quat = Quaternion.Euler(0f, 0f, -angle);
             transform.rotation = Quaternion.LerpUnclamped(transform.rotation, quat, rotationSpeed * Time.deltaTime);
         }
     }
 
     public float oppositeDirection()
     {
         Vector3 direction = transform.rotation.eulerAngles;
         return (direction.z + 180) % 360;
     }

And of course the same should be true if the player is moving up or down the screen, the parent class should keep the player moving across the screen and if they player moves up towards the other sprite, then it moves towards that sprite, if I am moving down the screen the the player is supposed to move away from the other sprite.

I am not getting this to work the way I am hoping it too. Anyone have any idea?

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 ABerlemont · Apr 11, 2018 at 03:54 PM 0
Share

A simple context would be to get the vector between the player (transform) and the sprite (transform) and invert the normalize vector of that vector ?

 Vector3 vec = player.transform.position - other.transform.position;
 vec = vec.normalize;
 other.tranform.Translate(-vec * speed);

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

76 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

Unity2d shooting bullets is off when the gun is rotated to the left 0 Answers

PUN 2, Player A not seeing Player B's hat 0 Answers

getting out of LookAt messes up first person view 1 Answer

Getting the rotation of an object from another object within a prefab. 3 Answers

Help with making script rely on rotation 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