Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing Vector direction without effecting speed

Discussion in 'Scripting' started by Bazz_boyy, Aug 8, 2015.

  1. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    Hi friends,

    I'm using AddForce to have my object travel to my mouse input. However, I want the direction of force to always be in the direction of the mouse input. At the moment, when I try to change the direction of the object, I'm getting a lot of 'over-shooting' or 'drag effect'.

    I would like to know how I could approach this problem

    Thanks! Here's my code:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     float proxyAngle;
    7.     float speed = 20;
    8.     Vector3 vectorToTarget;
    9.     Vector3 mouseLocal;
    10.  
    11.  
    12.     void Start () {
    13.    
    14.     }
    15.    
    16.     void Update () {
    17.  
    18.  
    19.         if(Input.GetMouseButton(0))
    20.         {
    21.             mouseLocal = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    22.             vectorToTarget = mouseLocal - transform.position;
    23.             float angle = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
    24.             proxyAngle = angle;
    25.             Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
    26.             transform.rotation = Quaternion.Slerp(transform.rotation, q, Time.deltaTime * speed);
    27.         }
    28.     }
    29.  
    30.     void OnCollisionEnter2D(Collision2D coll) {
    31.         if(coll.gameObject.tag == "Floor")
    32.         {
    33.             Debug.Log ("FLOOR");
    34.             Application.LoadLevel(0);
    35.  
    36.         }
    37.         if(coll.gameObject.tag == "Goal")
    38.         {
    39.             Debug.Log ("GOAL YE");
    40.             Application.LoadLevel(0);
    41.  
    42.         }
    43.     }
    44.     void OnTriggerStay2D(Collider2D coll){
    45.         Debug.Log (coll.tag);
    46.         if(coll.tag == "Ramp"){
    47.             Rigidbody2D rb = gameObject.GetComponent<Rigidbody2D>();
    48.             //Vector3 currentVelocity = rb.velocity;
    49.             rb.AddForce(vectorToTarget * 5);
    50.        
    51.         }
    52.  
    53.     }
    54. }
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    According to the laws of physics, changing direction is changing speed, so doing what you said shouldn't be possible.

    That said, if you want your object to move at a constant rate, maybe you don't want to use the physics system at all, but rather make it kinematic and move it solely through your script.
     
  3. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    I will just say that what you want seems like it's negating much of the usefulness of actually using the physics engine so you may want to look into simpler (and more easily controlled) methods of moving objects. Also, you're using smooth-lerping to slowly change your rotation, which seems counterproductive if your goal is to have immediate and absolute control over the vector of your object.
     
  4. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    Yeah I guess you're probably right. By the way, did you mean changing direction is changing velocity?
     
  5. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    I actually want to keep the lerping (so yes there is a slight delay but not as strong as the addforce effect). I think I will scrap the physics engine and just adjust the players velocity directly instead, because that actually achieves the effect I want.

    Thanks for the answers guys
     
  6. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    Possibly. It's been many years since I've had a physics class. The difference between speed and velocity escapes me.

    You knew what I meant, though. :p
     
    Bazz_boyy likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You want to maintain a constant speed regardless of the direction? This is actually relatively simple. Pseudo code as follows.

    Code (CSharp):
    1. // Set these variables elsewhere
    2. public Vector3 targetDirection;
    3. public float targetSpeed;
    4.  
    5. void FixedUpdate (){
    6.     // Cacheing your RigidBody is still a good idea
    7.     GetComponent<RigidBody>().velocity = targetDirection.normalised * targetSpeed;
    8. }
     
    berk_can and Bazz_boyy like this.
  8. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Speed is just a number... velocity is speed and direction... ie

    Code (csharp):
    1.  
    2. rigidbody.velocity.magnitude = speed; // a float
    3. //rigidbody.velocity; // is a vector3
    4. [\code]
     
  9. Bazz_boyy

    Bazz_boyy

    Joined:
    May 22, 2013
    Posts:
    192
    not a constant speed, but a changing speed due to addforce.
     
  10. sharnold52

    sharnold52

    Joined:
    Sep 18, 2017
    Posts:
    1
    Actually, speed is ignorant of direction. Speed is just the rate at which an object covers a distance. What you are thinking of is velocity, which is both speed and direction. Therefore, changing direction would not affect the speed of the object, but rather the velocity of the object.

    Edit: I see this was already addressed later in the thread... my bad
     
  11. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    And also, that post was over two years ago. :p