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 /
  • Help Room /
avatar image
0
Question by Calfa · Dec 15, 2018 at 11:32 PM · rotationtransformchild

Please help with transform child rotation

I have seen and read a few similar questions in this forum, but i didn't find exactly what i was looking for.

Here is my issue:

I'm trying to make a 2D game similar to Worms. My player uses a line to help aiming which can be rotated from 0 (horizontal) to 90 degrees (vertical). When player faces to the right, it will go from 0º to 90º and when facing to the left it will go from 90º to 180º.

Since i don't understand the concept of Quaternion and how to work with it, i'm trying to do the whole thing using eulerAngles.

This is what i came up with:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerControl : MonoBehaviour {
 
     public float rotationSpeed = 25f;
     public float movementSpeed = 2f;
     // Use this for initialization
     void Start () {
         
     }

     void Update () {
         this.ControlPlayer();
         this.AimControl();
         
     }
     void ControlPlayer(){
         Vector3 p = transform.position;
         var y = transform.eulerAngles.y;
         //Prevent player from moving on the Z axis
         if(p.z != 0){
             p.z = 0;
         }
         transform.position = p;
             //Player movement
         var x = Input.GetAxis("Horizontal") * Time.deltaTime * movementSpeed;
             //Set the direction player is facing
         if(x < 0){
             y = 0;
         }else if(x > 0){
             x = -x;
             y = 180;
         }
         transform.eulerAngles = new Vector3(0,y,0);
         transform.Translate(x, 0, 0);
     }
     void AimControl(){
             //Child is an empty GO attached to player containing the aiming line
         var child = transform.GetChild(0);
         var z = Input.GetAxis("Vertical") * Time.deltaTime * rotationSpeed;
         var rot = new Vector3(0,0,z);

         //Limit Aim Line rotation to values between 0 and 90.
         var minRot = new Vector3(0,0,0);
         var maxRot = new Vector3(0,0,90);
         child.Rotate(rot, Space.World);

         if(child.eulerAngles.z < 0){
             if(child.eulerAngles.z > -90){
                 child.eulerAngles = minRot;
             }else{
                 child.eulerAngles = maxRot;
             }
         }else if(child.eulerAngles.z > 90){
             //child.eulerAngles = maxRot;
         }
     }
 }
 

My problem is in "AimControl()". I just want it not to go below 0º and above 90º regardless which side player is facing at. I've tried many modifications on the last if/else part. I've also tried to rotate it local and world but i don't really know what i'm doing and can't see any satisfying results.

Hope you guys can help me understand it better. 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

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

210 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 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 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

Rotation Of Transform Does Not Result In Correct Value 0 Answers

rotate object2d the direction it is moving? 0 Answers

Unity Bug? Parent object rotates around child object. 1 Answer

Set parent of an object when they collide? 2 Answers

Child object in animation transform Problems 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