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 Ecrodin · May 05, 2020 at 05:44 AM · rotationparentchildparent-childmouse position

Why does the child of a game object not rotate to face mouse position when the parent object is moving?

So I am new to unity, forgive me if my question is already answered somewhere on the forum.

I am creating as top down shooter, and a problem I am having is that I want my player to be forward facing at all times, but I want the weapon/firepoint to rotate towards wherever the mouse is currently on the screen. This only seems to work when the parent object is not moving, otherwise the childs rotation is locked into place. What would I be doing incorrectly? Any advice would be greatly appreciated.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     public float moveSpeed = 5f;
 
     public Rigidbody2D mainbody;
     public Rigidbody2D firePoint;
     public Camera cam;
 
     Vector2 movement;
     Vector2 mousePos;
 
     void Update()
     {
         //Input
         movement.x = Input.GetAxisRaw("Horizontal");
         movement.y = Input.GetAxisRaw("Vertical");
 
         mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
 
     }
 
     void FixedUpdate()
     {
         //Movement
         //Time.fixedDeltaTime is there because it results in consistent movement speed in game, as to how I am uncertain
         mainbody.MovePosition(mainbody.position + movement * moveSpeed * Time.fixedDeltaTime);
 
         Vector2 lookDir = mousePos - mainbody.position;
 
         //For the Z rotation of the object, we need to find the angle that will force the player to turn and change the initial firing position
         //will be visualized later in animation if we choose to
         float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
         firePoint.rotation = angle;
 
     }
 }


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · May 05, 2020 at 05:43 AM

Is firePoint the child of mainbody?? If so, take the rigidbody off of firePoint. Rigidbodys should never be parented with another rigidbody

Comment
Add comment · Show 1 · 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 Ecrodin · May 07, 2020 at 07:45 PM 0
Share

You were right that the child should not have a rigid body, I removed that component. I wound up finding a solution to the problem by creating a different object that would constantly move towards the firepoint at a rate faster than the player would ever be able to move. Then I had the object that was following the firepoint be the one that pointed towards the mouse ins$$anonymous$$d of the player. Not sure if there are better ways of doing it but it worked out for me.

public class GunAttatchment : $$anonymous$$onoBehaviour { public Rigidbody2D mainbody; public float speed; public Camera cam; private Transform target; Vector2 mousePos; // Start is called before the first frame update void Start() { //Find the target that you want the object to move towards target = GameObject.FindGameObjectWithTag("FirePoint").GetComponent<Transform>(); } // Update is called once per frame void Update() { //If the distance between the object and the target is greater than 0, have the object move towards the target if(Vector2.Distance(transform.position, target.position) > 0) { transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.position, speed * Time.deltaTime); } mousePos = cam.ScreenToWorldPoint(Input.mousePosition); } void FixedUpdate() { //Subtracting two vectors results in a vector that points from one to the other Vector2 lookDir = mousePos - mainbody.position; //For the Z rotation of the object, we need to find the angle that will force the player to turn and change the initial firing position //will be visualized later in animation if we choose to float angle = $$anonymous$$athf.Atan2(lookDir.y, lookDir.x) * $$anonymous$$athf.Rad2Deg - 90f; mainbody.rotation = angle; } }

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

165 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

Related Questions

Make a simple tree 1 Answer

Why is a child object not rotating properly with parent? 1 Answer

Child ignoring parent rotation? 1 Answer

Parent object is getting generated why ?? 1 Answer

How can I identify a non-uniform scaled mesh, and fix it? 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