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 justinkatic · Sep 14, 2017 at 04:58 AM · velocityprojectilenewbiebouncereflect

unity bounce without physics

Alright so I'm just completely stuck on how to do this I am using velocity to make my projectile rotate and move forward towards the player which is all working fine what I am stuck with is having a mechanic In my game that the player puts up a barrier to then bounce the projectile of it and give it a new force in direction depending on where it hit this barrier (pong) any help would be super amazing been on this one for few days now. so this is my basic bullet rotating towards player code since I'm sure my paragraph explained it terribly.

 private GameObject target;
 private Vector3 targetPoint;
 private Quaternion targetRotation;
 public float speed = 2.0f;
 public float MoveSpeed = 30.0f;
 void Start () 
 {
     target = GameObject.FindWithTag("Player");
 }

 void Update()
 {
     targetPoint = new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z) - transform.position;
     targetRotation = Quaternion.LookRotation (targetPoint, Vector3.up);
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
     transform.Translate (Vector3.forward * MoveSpeed * Time.deltaTime);
 }

}

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 hexagonius · Sep 14, 2017 at 08:17 AM 0
Share

In case you decide to use physics after all: Use FixedUpdate and either AddForce or change in velocity to achieve this. Rotation would be torque, but I think this would still be fine in your case.
OnCollisionEnter gives you relevant information when colliding. If you pick any of the returned contacts and raycast against it you'll receive a surface normal at that point. Using Vector3.Reflect would mirror reflect any vector, like the velocity, which could be the new direction. Or you just give the projectile a bouncy material, might result in an appropriate reflection too.

avatar image unit_nick hexagonius · Sep 14, 2017 at 08:20 AM 0
Share

I answered similarly. Then I reread the title.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unit_nick · Sep 14, 2017 at 08:05 AM

 private GameObject target;
 private Vector3 targetPoint;
 private Quaternion targetRotation;
 public float speed = 2.0f;
 public float MoveSpeed = 30.0f;
 
 // added a barrier radius x. assumes barrier always x in front of target
 public float barrierRadius = 1f;
     
 void Start()
 {
     target = GameObject.FindWithTag("Player");
 }
 
 void Update()
 {
     targetPoint = new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z) - transform.position;
     targetRotation = Quaternion.LookRotation(targetPoint, Vector3.up);
     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * speed);
 
     // calculate movement for easy access
     Vector3 movement = transform.forward * MoveSpeed * Time.deltaTime;
 
     // get distance to barrier
     float distanceToBarrier = Vector3.Distance(transform.position, targetPoint) - barrierRadius;
 
     // if movement will encroach barrier radius then...
     if (movement.magnitude > distanceToBarrier)
     {
         // move the projectile to the barrier
         transform.position += movement.normalized * distanceToBarrier;
 
         // subtract the distance the projectile travelled to the barrier
         movement -= movement.normalized * distanceToBarrier;
 
         // then ricochet
         // for this example i will simply reflect the projectile straight back
         movement *= -1f;
         transform.rotation = Quaternion.Inverse(transform.rotation);
 
         // we can then leave this if clause and finish the movement
     }
 
     // move the transform
     transform.position += movement;
 }
Comment
Add comment · 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

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

71 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

Related Questions

ball breaks through walls 1 Answer

move 2D object in opposite direction after collision 1 Answer

Moving a ball in the opposite direction of where it hits 1 Answer

Firing projectile in curve 1 Answer

How do you shoot a 2d projectile at any angle from your character? 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