• 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
1
Question by MajorSmurf · Jul 31, 2018 at 12:23 AM · vector3vectorvector3.position

Making a vector3 z coord have a zero on reflect?

Right so before you laugh at my silly question lets get the laughs out of your system with a joke.... why did the chicken fall through the world?.... cause he wasn't rigid body enough HAHAHA..... alright in all seriousness i found this little code and want to implement it so that x,y are always reflected but not the z. so when it hits an angle of an edge i will notice through debugged that it gains a z and will fly off in the distance.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Ball_Behaviour : MonoBehaviour 
 {
     [SerializeField]
     private Vector3 initialVelocity;
 
     [SerializeField]
     private float minVelocity = 10f;
 
     private Vector3 lastFrameVelocity;
 
     private Rigidbody rb;
 
     void OnEnable()
     {
         rb = GetComponent<Rigidbody> ();
         rb.velocity = initialVelocity;
     }
     
     // Update is called once per frame
     void Update () 
     {
         lastFrameVelocity = rb.velocity;
     }
 
     void OnCollisionEnter(Collision col)
     {
         Bounce (col.contacts [0].normal);
     }
 
     private void Bounce(Vector3 collisionNormal)
     {
         var speed = lastFrameVelocity.magnitude;
         var direction = Vector3.Reflect (lastFrameVelocity.normalized, collisionNormal);
 
         Debug.Log ("Out Direction:" + direction);
         rb.velocity = direction * Mathf.Max (speed, minVelocity);
     }
 }

so basically what i want is a way that if the object hits a corner and wants to go in z i can set it to 0 or make it so that the z isn't possible to start with.... i hope this makes sense for what i want.

Thank you for your time MajorSmurf

Comment
Add comment · Show 2
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 ModLunar · Jul 31, 2018 at 01:04 AM 0
Share

Lmao, I love the joke, thank you that made my night XD.
So what do you mean only reflect the x and y? Do you want to do this reflection calculation, but so that the z value of the vector is left untouched?

avatar image MajorSmurf ModLunar · Jul 31, 2018 at 02:12 AM 0
Share

ok so for my level design i wanted a 3D world which looks like a empty box with boxes round the side. and have random bouncing balls, moving platforms to stop you getting to the end. but the problem is when the player ball collides with a object that has a slight point to it. it will calculate out a z co-ord for the velocity which i dont want. as i only want my ball to move up - down and side and side but not in z direction..... is this making sense or did i babble?

basically i want the z coord to always be 0? so it cant move in that direction. and yes i want to do the reflect without any z is this possible. i tried vector2.reflect but it would still bounce towards the camera to never be seen again..... there are alot of balls in space somewhere.

1 Reply

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

Answer by ModLunar · Jul 31, 2018 at 02:36 AM

Ahh okay I think I know what you mean now (for future Google people, see the comments too, I asked for clarification)

Without looking too much into it, I'd probably force it along the xy-plane by changing it to
Mathf.Max (speed, minVelocity) * Vector3.Scale(new Vector3(1, 1, 0), direction).normalized;
instead of
direction * Mathf.Max (speed, minVelocity); on line 40 (at the very end)

Vector3.Scale(...) will take 2 Vector3s and multiply their corresponding x, y, and z compenents together, so you can multiply by 1 in the x and y to keep whatever the velocity was in the xy axes, but then multiply by 0 in the z to have the velocity there be zero.

For example, Vector3.Scale(new Vector3(1, 1, 0), new Vector3(10, 7, 4)) would give you back an expression equal to new Vector3(10, 7, 0)

By getting rid of the z-component, you need to re-normalize the vector cause it lost its length of 1. Then once it has a length of 1 again, you can multiply by the speed to give you the right size velocity vector.

And a side note, if you're working with Rigidbodies, you may want to check in the inspector, under "Constraints", turn on "Freeze Position Z" so that it can't move (from forces) in the z-axis. I'd still make that change though in the script if you want them to not "lose out on speed" if their direction was not going to be exactly xy-plane parallel.

Comment
Add comment · Show 3 · 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 MajorSmurf · Jul 31, 2018 at 01:51 PM 0
Share

that's perfect thank you ill give it a try later today. see i was thinking about freezing the rigidbody but i thought that would affect the bounce but maybe i took a easy to fix problem and made it difficult for no reason xD.

thanks for you help :3.

avatar image MajorSmurf MajorSmurf · Jul 31, 2018 at 02:34 PM 0
Share

Ok the code worked perfectly apart from the balls beco$$anonymous$$g a little floaty? I'm not sure that's the word to describe it but I'm guessing its just an easy solution of adjusting the numbers. But this works perfectly thank you again and I'm not gonna lie you must likely will see again soon xD.

oh one last question the vector.scale multiples two vectors together I'm assu$$anonymous$$g it has no problem with negatives, decimals and if i was to take a vector 2 and vector 3 and use this would it have a problem with that or does have to be 2 vector3s?

avatar image ModLunar MajorSmurf · Jul 31, 2018 at 09:17 PM 0
Share

Of course, haha I'm glad to help! :)

Ah, they have to have the same number of dimensions. So it can't work on a Vector2 and a Vector3, but there is Vector2.Scale(...) as well! You can cast Vector3s to Vector2s if you need to.

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

110 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

Related Questions

Iterating over Vector's axes 1 Answer

Instantiate Gameobjects in the mid position across several targets without overlap 0 Answers

Vector based on the rotation of the main camera 1 Answer

Rotate vector around vector? 2 Answers

Checking if a Vector3 is a int 0 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