• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How can I calculate new direction angle when player hits wall diagonally? 1 Answer

hinge joint bounce min velocity - physical explanation please 0 Answers

Need some help with firing projectile at player using Rigidbody Velocity 1 Answer

instantiate finding upstream velocity 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges