• 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 theProdWorm · Nov 17, 2020 at 10:44 PM · transformreferencereferencing

How do I get and manipulate Transform values without changing the original Transform?

I want to add spread and inacurracy to my top-down shooter. The bullets spread as they should, but my gun kinda wiggles because it thinks I'm changing its Transform. My code so far looks like this:

 protected void InstantiateBullet(float spread)
 {
     Transform clickTransform = transformAtClick;

     float deviation = Random.Range(-inaccuracy, inaccuracy);
     float actualAngle = clickTransform.eulerAngles.z;
     Vector3 euler = Vector3.forward * (actualAngle + spread + deviation);

     clickTransform.rotation = Quaternion.Euler(euler);

     GameObject bullet = Instantiate(bulletPrefab, firePoint.position, clickTransform.rotation);

     Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
     rb.AddForce(clickTransform.right * bulletSpeed * Time.fixedDeltaTime, ForceMode2D.Impulse);
 }

What I think is the problem is that all Transforms store a reference to gameObject.transform, but I'm not sure. Honestly, I don't know how to provide any further information, so I hope this is enough.

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

2 Replies

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

Answer by Namey5 · Nov 18, 2020 at 05:59 AM

A Transform is a class, which in C# means that it is a reference type. As such, setting a Transform equal to another Transform doesn't make a new copy, it just points to the same Transform (which is why adjusting a value on either will change the original). In this case, you don't even need a Transform at all and can just use vectors and quaternions to get your desired rotation;

  protected void InstantiateBullet(float spread)
  {
      float deviation = Random.Range(-inaccuracy, inaccuracy);
      Quaternion clickRotation = Quaternion.AngleAxis (transformAtClick.eulerAngles.z + spread + deviation, Vector3.forward);
      GameObject bullet = Instantiate(bulletPrefab, firePoint.position, clickRotation);
      Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
      rb.AddForce((clickRotation * Vector3.right) * bulletSpeed * Time.fixedDeltaTime, ForceMode2D.Impulse);
  }
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 theProdWorm · Nov 18, 2020 at 01:19 PM 0
Share

Thank you very much, this works perfectly!

I did search a lot beforehand and found out the difference between classes and structs, and tried making a struct out of the Transform object. Of course, this did not work. I did figure out I should just use Quaternions and Vectors but I just couldn't figure out how rotation works. The point where I gave up was when I read something about Quaternions using a fourth dimension. Why do they have to be so complicated?...

avatar image
0

Answer by MurphyMurph_21 · Nov 18, 2020 at 06:03 AM

I think if you add the euler rotation to the firePoint.position instead of the clickTransform.position it should work @theProdWorm

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 theProdWorm · Nov 18, 2020 at 01:24 PM 0
Share

Thing is, the firePoint is the reference that the transformAtClick gets when I click the fire button. What I tried to do was get a copy of the firePoint, but no matter what I did, I'd get a reference to it instead.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

184 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

Related Questions

Getting a " NullReferenceException: Object reference not set to an instance of an object " - Space Shooter Tutorial 14 - Counting Points 3 Answers

How do I use one GameObject's properties in a different GameObject's script? 0 Answers

Object reference not set to an instance of... 2 Answers

Weird problem with referencing game manager 1 Answer

I'm having problems with referencing other scripts. 2 Answers


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