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 Cian911 · Mar 22, 2015 at 11:09 AM · c#instantiateprefabunity5physics2d

Object coordinates not updating as expected

I'm having an issue with a prefab I'm instantiating in my script. What I'm attempting to do is simulate the physics behind the projectile motion of a cannonball. I have the following script attached to my prefab:

 using UnityEngine;
 using System.Collections;

 public class ShootPhysics : MonoBehaviour {

     // vectors & angles to be calculated
     private float fx = 0f;
     private float fy = 0f;
     private float angle;

     // forces acting upon object
     private float gravity = 9.8f;
     private float force = 1.2f;

     // new distance & vector variables
     private float dis_x = 0f;
     private float dis_y = 0f;

     // Script & CannonBall transform
     private CannonPhysics cannon_angle;

     void Start() {
         cannon_angle = GameObject.FindWithTag("Gun").GetComponent<CannonPhysics>();
         angle = cannon_angle.getAngle ();
         fx = Mathf.Round (force * Mathf.Cos(Mathf.Deg2Rad * angle));
         fy = Mathf.Round (force * Mathf.Sin(Mathf.Deg2Rad * angle));
     }

     void FixedUpdate() {
         dis_x = (float)(fx * Time.timeSinceLevelLoad);
         dis_y = (float)(fy + Time.timeSinceLevelLoad + (0.5) * (-gravity) * Time.timeSinceLevelLoad * Time.timeSinceLevelLoad);
         Debug.Log (dis_x+", "+dis_y);
         gameObject.transform.Translate (new Vector2(dis_x, dis_y));
     }

 }


And the prefab will be instantiated from the following script:

 using UnityEngine;
 using System.Collections;

 public class CannonPhysics : MonoBehaviour {
     
     // This will be our script to rotate our cannon
     private float angle = 0f;
     
     // cannonball object
     public Transform FirePoint;
     GameObject cannon = null;
     
     void Update() {
         if(Input.GetKey(KeyCode.UpArrow)) {
             angle = angle + 1;
         }
         if(Input.GetKey(KeyCode.DownArrow)) {
             angle = angle - 1;
         }
         
         // update cannon angle
         transform.rotation = Quaternion.Euler (new Vector3(0, 0, angle));
         
         if(Input.GetMouseButtonDown(0)) {
             CreateCannon();
         }
     }
     
     public float getAngle() {
         return angle;
     }
     
     private void CreateCannon() {
         cannon = Instantiate (Resources.Load("Prefabs/CannonBall"), FirePoint.position, FirePoint.rotation) as GameObject;
         Destroy (cannon, 3f);
     }
     
 }


The object gets instantiated as it should, and the object is destroyed after 3 seconds as it should, however the object x and y position do not change at all, and are not what i should expect them to be. Let me give you an example:

These are the x and y coordinates i should expect which i have displaying in the console.

enter image description here

However these are the actual coordinates displayed in Unities inspector:

enter image description here

What's strange too, is that these coordinates will not change for each object, they will literally stay the same, even after the object has been destroyed. Can anyone explain why this is happening? And perhaps offer a solution?

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

2 People are following this question.

avatar image avatar image

Related Questions

How to randomly instantiate other prefabs parallel? 1 Answer

Why is my Prefab Instantiating when the Scene is Loaded? 2 Answers

How to merge two cells from a Grid Layout Group 1 Answer

How do i Instantiate a prefab with specific assests included 3 Answers

Prefab Instantiation Creating Object At Origin 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