• 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 /
  • Help Room /
avatar image
Question by NikAgrawal2121 · Mar 08, 2022 at 02:39 PM · smoothmomentum

I want to smooth transfer the object ,i want some solution to transfer my object Smoothly

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Moment : MonoBehaviour { public Rigidbody2D rb; // To acss the rigidBody public Vector2 direction; // Used for the directio of the movement public float speed = 30f; public float MaxBounceAngle = 75f; public AudioSource Audio; public AudioClip BallTap;

 private void Awake()
 {

     Audio = GetComponent<AudioSource>();
     rb = GetComponent<Rigidbody2D>(); // getcomponent search the rigidbody2D in the paddle 
                                       //RigidBody is used to move the obj 
 }


 public void movePlayerRight()
 {
     rb.transform.position += Vector3.right * speed * Time.deltaTime;

     

 }
 public void movePlayerLeft()
 {
     rb.transform.position += Vector3.left * speed * Time.deltaTime ;
 }

 private void FixedUpdate()
 {

     RunCharacter(Input.GetAxis("Horizontal"));
     if (this.direction != Vector2.zero)
     {
         rb.AddForce(this.direction * this.speed); // this add the speed in the direction
     }
 }
 private void RunCharacter(float horizontalInput)
 {
     //move player    
     rb.AddForce(new Vector2(horizontalInput * speed * Time.deltaTime, 0));
 }
 public void ResetPaddle()
 {
     this.transform.position = new Vector2(0f, this.transform.position.y); // new vector becouse we have to maintain position of paddle to middle 
                                                                           //thats what x =0 and y == start position

     this.rb.velocity = Vector2.zero;
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     Ball ball = collision.gameObject.GetComponent<Ball>(); // To check paddle collide with ball
                                                            // GetComponent G is capital becouse to acess global veriable

     Audio.clip = BallTap;
     Audio.Play();

     if (ball != null)
     {
         Vector3 paddlePosition = this.transform.position; // Used to find the position of paddle
         Vector2 ContactPoint = collision.GetContact(0).point; //used to find the contact point of ball with paddle

         float offset = paddlePosition.x - ContactPoint.x;

         float width = collision.otherCollider.bounds.size.x / 2; // Used to calculate width of the paddle (Half )


         float currentAngle = Vector2.SignedAngle(Vector2.up, ball.rb.velocity); // this will give actual direction of the ball

         float BounceAngle = (offset = width) * this.MaxBounceAngle; // this is disired angle of the ball

         float NewAngle = Mathf.Clamp(currentAngle + BounceAngle, -MaxBounceAngle, MaxBounceAngle);

         Quaternion rotation = Quaternion.AngleAxis(NewAngle, Vector3.forward); //Quaternion used for rotation

         ball.rb.velocity = rotation * Vector2.up * ball.rb.velocity.magnitude;

     }
 }

} ,using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Moment : MonoBehaviour { public Rigidbody2D rb; // To acss the rigidBody public Vector2 direction; // Used for the directio of the movement public float speed = 30f; public float MaxBounceAngle = 75f; public AudioSource Audio; public AudioClip BallTap;

 private void Awake()
 {

     Audio = GetComponent<AudioSource>();
     rb = GetComponent<Rigidbody2D>(); // getcomponent search the rigidbody2D in the paddle 
                                       //RigidBody is used to move the obj 
 }


 public void movePlayerRight()
 {
     rb.transform.position += Vector3.right * speed * Time.deltaTime;

     

 }
 public void movePlayerLeft()
 {
     rb.transform.position += Vector3.left * speed * Time.deltaTime ;
 }

 private void FixedUpdate()
 {

     RunCharacter(Input.GetAxis("Horizontal"));
     if (this.direction != Vector2.zero)
     {
         rb.AddForce(this.direction * this.speed); // this add the speed in the direction
     }
 }
 private void RunCharacter(float horizontalInput)
 {
     //move player    
     rb.AddForce(new Vector2(horizontalInput * speed * Time.deltaTime, 0));
 }
 public void ResetPaddle()
 {
     this.transform.position = new Vector2(0f, this.transform.position.y); // new vector becouse we have to maintain position of paddle to middle 
                                                                           //thats what x =0 and y == start position

     this.rb.velocity = Vector2.zero;
 }

 private void OnCollisionEnter2D(Collision2D collision)
 {
     Ball ball = collision.gameObject.GetComponent<Ball>(); // To check paddle collide with ball
                                                            // GetComponent G is capital becouse to acess global veriable

     Audio.clip = BallTap;
     Audio.Play();

     if (ball != null)
     {
         Vector3 paddlePosition = this.transform.position; // Used to find the position of paddle
         Vector2 ContactPoint = collision.GetContact(0).point; //used to find the contact point of ball with paddle

         float offset = paddlePosition.x - ContactPoint.x;

         float width = collision.otherCollider.bounds.size.x / 2; // Used to calculate width of the paddle (Half )


         float currentAngle = Vector2.SignedAngle(Vector2.up, ball.rb.velocity); // this will give actual direction of the ball

         float BounceAngle = (offset = width) * this.MaxBounceAngle; // this is disired angle of the ball

         float NewAngle = Mathf.Clamp(currentAngle + BounceAngle, -MaxBounceAngle, MaxBounceAngle);

         Quaternion rotation = Quaternion.AngleAxis(NewAngle, Vector3.forward); //Quaternion used for rotation

         ball.rb.velocity = rotation * Vector2.up * ball.rb.velocity.magnitude;

     }
 }

}

Comment

People who like this

0 Show 0
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.

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

176 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

Related Questions

Terrain smoothing brush opacity settings 0 Answers

How can I smooth out 2D object movement? 0 Answers

Screen to World Point on 2.5D game 0 Answers

Vaulting mechanism without losing speed / momentum 0 Answers

How do I get smooth edges for a voxel game? 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