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 relicright · Dec 27, 2013 at 12:13 AM · 2drotationmovementsprite

Having Issues Rotating 2d Sprites to face another 2d Object

Hello,

I've been searching the forums for some time and haven't found a solution to my problem. I'm making a 2d sprite game that is running in a 3d world (characters are sprites, terrain and some objects are 3d models). When my character cast lightning, I have the spell showing up, but I can't get it to rotate towards the enemy it was cast on. Here's what I have, I think i've gotten it to rotate every way except the right one. Needs rotated in the Z-Axis only.

Lightning.js:

function Update () {

 var angle = Mathf.Atan2(lookPoint.position.z, lookPoint.position.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

}

LightningItem.js:

function Update () {

 //IF PLAYER PRESSES THE SHOOT BUTTON
 if(Input.GetKey(KeyCode.Mouse0) && spellToCast != null && Time.time >= nextFire)
 {
     nextFire = Time.time + fireRate;

     var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit, 100))
     {
         //IF Lightning
         if(spellToCast == "Lightning" && hit.transform.tag != "Floor")
         {
             var launchPosition = spellOrigin.TransformPoint(0, 0, 0);
             
             
             point = hit.transform;
             
             networkView.RPC("Lightning", RPCMode.All, launchPosition,
              Quaternion.Euler(transform.eulerAngles.x + 90, transform.eulerAngles.y, 360));
              
              point = null;

         }
     }
 }

}

@RPC function Lightning(position : Vector3, rotation : Quaternion) {

 var NewLightning = Instantiate(lightning, position, rotation);
 NewLightning.name = "NewLightning" + x;
 var lightChange = GameObject.Find("NewLightning" + x).transform.GetComponent(LightningItem);
 lightChange.damageDelay = 2;
 lightChange.lookPoint = point;

}

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 robertbu · Dec 27, 2013 at 12:47 AM 0
Share

A few questions:

  • I assume your scene is setup as 'y' being up, and your 3D object being on the XZ plane?

  • Does the camera always look towards positive 'z', or does in roam in the scene with the sprite billboarded?

  • Are the target objects on the same XY plane with the sprite?

  • Are you using a Sprite or Quad for displaying your sprite, or something other game object?

  • With rotation (0,0,0), the front of your sprite to the right or is it up?

avatar image relicright · Dec 27, 2013 at 02:27 AM 0
Share

The camera always faces the Z direction. "Y" is up. I'm using sprites to animate my characters and the spells. Rotation(0,0,0) displays the lightning like the picture included, but when it's instantiated it's rotation is (90, 0, 300) to make it flat.alt text

3 Replies

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

Answer by relicright · Dec 27, 2013 at 05:02 AM

Ok guys,

Your help was greatly appreciated. First off, I can't make my sprites into planes, because I am using Unity's new sprite animation system to animate them, but I have found a solution to the problem.


LightningItem.js


var newRotation = Quaternion.LookRotation(lookPoint.transform.position - transform.position).eulerAngles;

newRotation.x = Mathf.Clamp(-90, -90, -90);

newRotation.z = 0;

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(newRotation), 1);


Thank you again for all of your help and I hope this can help someone in the future.

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

Answer by sparkzbarca · Dec 27, 2013 at 01:51 AM

you can use

transform.rotation = quanternion.lookat(target) to look at something.

do you understand what a z axis rotation is perhaps your confused.

A z axis rotation is a roll. Imagine a plane so the plane rotation on the Z axis is a barrel roll thing where you keep facing the same forward but rotate.

for a 2d game this is confusing to me if your side scrolling because the z axis is in fact the axis that shouldnt exist so you should never rotate on it.

If your doing a top down game this kind of makes sense.

I'd recommend doing this.

Screenshot what the lightning looks like.

Pause unity

manually rotate how you want it to look in editor

assuming your using global axes if you rotate it in the Z axis in the editor to get it how you want it to look then your correct you want to use the Z axis. Screenshot it and post it looking correct quickly and I can easily tell you how to achieve that affect.

In your screenshots make sure you screenshot the editor not the game, and make sure you have local axes shown in case you need to rotate on those to achieve the affect (do this buy selecting the object then it should say either local or global written on a button ROUGHLY under the component menu wrod, not in the component menu just under the word itself, make sure it says local)

This will allow us to know which axis which for you sprite (we may think forward isn't what it is depending on how it was made)

with a screenshot of the how it looks and how it should look I guarantee I can tell you the line of code to rotate it properly.

Comment
Add comment · Show 3 · 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 relicright · Dec 27, 2013 at 02:03 AM 0
Share

Thank you for your assistance. I've attached the screenshot to this comment.alt text

top2.png (441.5 kB)
avatar image relicright · Dec 27, 2013 at 02:09 AM 0
Share

This is how it should look.alt text

top3.png (380.7 kB)
avatar image sparkzbarca · Dec 27, 2013 at 02:27 AM 0
Share

ok so as you noticed you used the Y axis to rotate not the Z axis.

however it looks like you do have it set up properly so that Z is forward.

transform.rotation = quanterion.lookat(enemyplayer) should actually work have you tried that in the update section?

alternatly AS LONG AS THE ENE$$anonymous$$Y IS DIRECTLY IN FRONT OF THE PLAYER just to test it quickly do this

transform.rotation = quanternion.lookat(transform.forward)

that should tell you if it'll work if you cant easily access the enemy position right away. it should throw lightning down on the ground in front of your character like you have in the second screen.

let me know if that works

avatar image
0

Answer by robertbu · Dec 27, 2013 at 03:07 AM

Getting rotation like this one right is not hard, but it takes understanding a whole list of things (i.e. my list of questions). I'm going to assume you have a particular setup:

  • The camera is generally from somewhere above like it is in these images.

  • The lightening is a 2D texture on some sort of flat surface.

I suggest you create the surface for your lightening from a plane (not a Quad or Sprite) created by Wiki CreatePlane editor script. You will use a default setting for this editor script except you are going to set the Anchor to 'BottomHalf'. Put your lightening material on this plane. Then you can do this to aim and scale the object:

 var launchPosition = spellOrigin.position;
 var point = hit.transform.position;
 
 transform.position = launchPosition;
 transform.localScale.z = (point - launchPosition).magnitude * 2.0;
 transform.LookAt(point);

I'm not sure if you need the scale or not. You should be able to use transform.rotation as the Quaternion passed in your RPC call.

Note that this fragment:

 spellOrigin.TransformPoint(0, 0, 0);

...is equivalent to:

 spellOrigin.position; 

Note there are lots of other setups for this layout, but using the CreatePlane script makes things easy.

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

19 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

Related Questions

Joystick 2D Sprite Rotation and Movement 0 Answers

How should I move a sprite in a direction based on the rotation of a child? 1 Answer

2D sprite rotation with velocity and interpolation 1 Answer

Rotating an object towards target on a single axis 2 Answers

Change Sprite Origin in 2D 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