Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

The rotation problem

Discussion in 'Scripting' started by Rutenis, Sep 8, 2014.

  1. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    So hey, i have been creating game for a while, but i still cant figure it out, why is this happening, when im shooting to the left, the rotation flips, when im shooting right, its just perfect. Any ideas how to fix this?

    Long story short, the Y axis should make 90degrees angle, even if im rotating the object, but it doesnt.

    I will attached the photos, maybe you will see the problem.

    The PROBLEM, bullets rotation is wrong:

    http://s3.postimg.org/okebhlr83/Left.png


    It works when im looking in the right side:

    http://s29.postimg.org/poe7qns7b/Right.png


    -Thanks! :))
     

    Attached Files:

    Last edited: Sep 8, 2014
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    It looks like you're not rotating it at all. What are you doing right now to make it rotate, script-wise?
     
  3. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Its an android game, when im moving my joystick, the weapon rotates, when im looking left the rotation of the bullet is the opposite of the bullets rotation when im looking right (it shouldnt). Script wise, im rotating the object according to the joysticks y position. So bassicly, im rotating the object. You can see the y axis is not working(rotating) correctly when im shooting left.

    Long story short, the Y axis should make 90degrees angle, even if im rotating the object, but it doesnt.
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Let me rephrase the question. Do you, at any point, "tell" the bullet object that your character is shooting left rather than right? If your bullet's rotation is based solely on the joystick's Y position, then the screenshot is exactly what I would expect to see.

    Posting your bullet spawning code will make it much easier to help.
     
  5. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Im instantiating it like this:

    Code (csharp):
    1.  
    2. if(joyScript.TouchedShooting == true && Time.time > nextFire)
    3.         {
    4.  
    5.             Transform ObjectG = Instantiate(bulletPrefab, LineStart.position, LineStart.rotation) as Transform;
    6.             ObjectG.rigidbody2D.AddForce((target.transform.position - transform.position) * Force * Time.smoothDeltaTime);
    7. }
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Alright then let's back up. Where does LineStart.rotation come from?
     
  7. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Its a child of the weapon, im rotating the weapon by the joystick. Its so frustrating! Thanks for helping tho! :)
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Alright, instead of backing up one step at a time, let me be more specific: show me the lines of code where you take the character's position/orientation and the joystick, and turn that information into a rotation.
     
  9. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Sure, i will show you the rotation, the joystick script, im just saving the touches position and the texture follows it:



    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class WeaponRotation : MonoBehaviour {
    6.  
    7.     public float rotateSpeed= 100.0f;
    8.     public float x = 0.0f;
    9.     public int invert= 1;
    10.     public WeaponJoystick Joy;
    11.     public float AdditionalSpeed;
    12.  
    13.     void Start ()
    14.     {
    15.         Delta();
    16.     }
    17.  
    18.     public void Delta()
    19.     {
    20.         x -= Input.GetTouch(0).deltaPosition.y * rotateSpeed * invert * Time.deltaTime;
    21.  
    22.         x = Mathf.Clamp(x, -10, 30);
    23.         transform.eulerAngles = new Vector3(0, 0, Joy.joyDelta.y * AdditionalSpeed);
    24.  
    25.     }
    26. }
     
    Last edited: Sep 8, 2014
  10. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    So the problem is as I suspected. Your rotation is making no changes based on the facing of the character. You're sending it "rotate 30 degrees down", but you never tell it "oh, by the way, that's 30 degrees down from the left side".

    Check if your character is facing left, and if so, modify the rotation value to match. This would likely be something like (180 - x) if x is the rotation value you're generating now.
     
  11. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Yeah, that makes sense, but in this case i dont know how to implement that into this script. Im never checking wheter im looking left or right, but im not sure if i need to. My rotation value is "Joy.joyDelta.y", How should i change the value so it would work? It stays 90 degrees when im looking right, but when im looking left, it starts to loose 90 degrees whenever i start to rotating. What should modify, when im looking left?

    ALL I NEED IS TO SET THE Y AXIS TO ALWAYS BE 90 DEGREES, (or to be purpendicular to the line). It stays 90 degrees when im looking right

    Thank you very much.