Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

How can I improve my Push/Pull object script?

$
0
0
Hello everyone, I have been trying to get a push/pull script to work and finally I got something to at least work. I am work on a 2D game, and I am trying to push/pull a cube on a ground. The following is my script so far: var hit : RaycastHit; //Holds some of the properties the raycast hit. var pushforce : float = 1; // Pushing Force var pullforce : float = 1; // Pulling Force var forceMode : ForceMode = ForceMode.VelocityChange; //Force type function Update () { //Get PlatformInputController script var rotate : PlatformInputController = GetComponent(PlatformInputController); // Get CharacterMotor script var jump : CharacterMotor = GetComponent(CharacterMotor); // Check for any objects at a distance of 1 if (Physics.Raycast(transform.position, transform.forward,hit,1)) { // If tagged pushable and Action button is pressed if (hit.transform.gameObject.tag == "Pushable" && Input.GetButton("Action")){ // Character cannot rotate rotate.autoRotate = false; //Character cannot jump jump.jumping.enabled = false; // Get the distance between character and object position var D : Vector3 = transform.position - hit.transform.position; // If on the left of object and move away from the object if (Input.GetAxis("Horizontal") < 0 && D.x < 0){ // Apply pull only when the distance is greater than 0.5 if (hit.distance>0.5){ hit.rigidbody.AddForceAtPosition(D*pullforce, hit.transform.position, forceMode); } } // If on the left of object and move towards the object if (Input.GetAxis("Horizontal") > 0 && D.x < 0){ // Apply push only when the distance is less than 0.5 if (hit.distance < 0.5){ hit.rigidbody.AddForceAtPosition(-D*pushforce, hit.transform.position, forceMode); } } // If on the right of object and move towards the object if (Input.GetAxis("Horizontal") < 0 && D.x > 0){ // Apply push only when the distance is less than 0.5 if (hit.distance<0.5){ hit.rigidbody.AddForceAtPosition(-D*pushforce, hit.transform.position, forceMode); } } // If on the right of object and move away from the object if (Input.GetAxis("Horizontal") > 0 && D.x > 0){ // Apply pull only when the distance is greater than 0.5 if (hit.distance>0.5){ hit.rigidbody.AddForceAtPosition(D*pullforce, hit.transform.position, forceMode); } } } // Otherwise character can rotate and jump again else{ rotate.autoRotate = true; jump.jumping.enabled = true; } } } This script works fine(I have commented it if there is still anything not clear please ask), but when in action it is not smooth at all. Two problems that I notice is that my force is dependent on the distance of my character to the cube hence sometimes I get a fast motion and sometimes I get a slow motion when I push or pull. The other problem I have is that when I have the rotation of the cube not fixed, if I try to push or pull the cube it will start moving by rotating forward or backward. At the end I need the rotation to be not fixed since I want to throw the cube of a ledge and it should rotate by gravity as it falls. So making the cube fixed rotation is not an option for me. I am new to Unity and scripting and I hope if anyone can at least lead me to the right direction of thinking, or what methods I should use in order to have a more solid script. Any help is appreciated. Thank you very much.

Viewing all articles
Browse latest Browse all 1264

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>