Search Unity

Collision Effect

Discussion in 'Scripting' started by TalhaJawed, Jan 21, 2020.

  1. TalhaJawed

    TalhaJawed

    Joined:
    Sep 18, 2019
    Posts:
    10
    I have applied force on object. I want it moving constantly with same speed, it will collide with other objects such as player, enemy... I want it(object) to move constantly with same speed and don't change the velocity after collision.
    What should I do, because as it collides with other objects its velocity changes according to physics....

    Any help will be appreciated..

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5. public class Platform : MonoBehaviour
    6. {
    7.     Rigidbody2D rb2d;
    8.  
    9. void Start()
    10.     {
    11.         startMoving();
    12.     }
    13. public void startMoving()
    14.     {
    15.         Vector2 direction = new Vector2(-30, 0);
    16.         Vector2 force = direction * 3f;
    17.         rb2d.AddForce(force, ForceMode2D.Impulse);
    18.     }
    19. }
     
    Last edited: Jan 21, 2020
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    If you want it to pass through objects and not "collide" you can set the colliders to be triggers.
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Modify velocity directly ( rb2d.velocity = force ) if you want to override the physics completely.
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Depends on what you want to have happen to other objects reacting to this object.

    You could try using rigidbody.moveposition instead of manipulating forces or velocities to see if that works out.
     
  5. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,261
    Neither one of these wouldn't change anything. Collisions still work using both.

    Even setting it to triggers wouldn't work.(misunderstood the OP, thought it was for projectiles, saw the code was for a platform.)

    For platforms you need Kinematic movement. Set the rigidbody body type to Kinematic. If AddForce doesn't work after that try movePosition or setting velocity directly.
     
    TalhaJawed likes this.
  6. TalhaJawed

    TalhaJawed

    Joined:
    Sep 18, 2019
    Posts:
    10
    thanks setting to kinematic and setting velocity works!!