Question by
thekrazyjames · May 13, 2018 at 09:37 AM ·
scripting problemcharactercontroller
Grappling hook physics script error
so I'm trying to make rope swinging physics, like what you'd see in a Spiderman film, my player has a grappling hook "Drill" that they fire and then it's supposed to move the player into its maximum distance and recalculate the velocity based on that movement, creating a swing like motion over time. but I'm having a few issues.
forces despite having large modifiers, feel week.
The rotation of the player doesn't seem to affect the application of forces like I'd expect it too.
the re-calculation of velocity seems to result in a bouncy like motion
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControl : MonoBehaviour {
public GameObject CameraPivot;
public Vector3 Vellocity;
public GameObject Player;
public GameObject HookRight;
public GameObject RopeStartRight;
public GameObject DrillRight;
public Camera PlayerCamera;
Transform TargetRight;
bool RightTargeted;
LineRenderer RightRope;
Vector3 StartPossition;
bool Collided = false;
float RightRopeDistance = 0.0f;
CharacterController CC;
void Start () {
CC = GetComponent<CharacterController>();
RightTargeted = false;
StartPossition = Player.transform.position;
RightRope = HookRight.GetComponent<LineRenderer>();
TargetRight = DrillRight.transform;
}
Vector2 Rotation = new Vector2(0,0);
// Update is called once per frame
void FixedUpdate () {
Vellocity = (Player.transform.position - StartPossition);
Vellocity = new Vector3(Vellocity.x / Time.fixedDeltaTime, Vellocity.y / Time.fixedDeltaTime, Vellocity.z / Time.fixedDeltaTime);
StartPossition = Player.transform.position;
Rotation.x = Input.GetAxis ("Mouse X")*5;
Rotation.y = Input.GetAxis ("Mouse Y")*5;
transform.Rotate (0, Rotation.x , 0);
CameraPivot.transform.Rotate (Rotation.y, 0, 0);
if (Input.GetKey(KeyCode.W)) {
Vellocity = Vellocity + new Vector3(Player.transform.forward.normalized.x * 10f * Time.fixedDeltaTime, Player.transform.forward.normalized.y * 10f * Time.fixedDeltaTime, Player.transform.forward.normalized.z * 10f * Time.fixedDeltaTime);
}
if (Input.GetKey(KeyCode.S)){
Vellocity = Vellocity - new Vector3(Player.transform.forward.normalized.x * 10f * Time.fixedDeltaTime, Player.transform.forward.normalized.y * 10f * Time.fixedDeltaTime, Player.transform.forward.normalized.z * 10f * Time.fixedDeltaTime);
}
if (Input.GetKey(KeyCode.D))
{
Vellocity = Vellocity + new Vector3(Player.transform.right.normalized.x * 10f * Time.fixedDeltaTime, Player.transform.right.normalized.y * 10f * Time.fixedDeltaTime, Player.transform.right.normalized.z * 10f * Time.fixedDeltaTime);
}
if (Input.GetKey (KeyCode.A)) {
Vellocity = Vellocity - new Vector3(Player.transform.right.normalized.x * 10f * Time.fixedDeltaTime, Player.transform.right.normalized.y * 10f * Time.fixedDeltaTime, Player.transform.right.normalized.z * 10f * Time.fixedDeltaTime);
}
if (Collided)
{
Vellocity.y = 0;
}
else
{
Vellocity.y = Vellocity.y - 16f * Time.fixedDeltaTime;
}
Vellocity.x = Vellocity.x * 0.9f;
Vellocity.y = Vellocity.y * 0.9f;
if (Vellocity.x > 10)
{
Vellocity.x = 10;
}else if (Vellocity.x < -10)
{
Vellocity.x = -10;
}
if (Vellocity.y > 10)
{
Vellocity.y = 10;
}else if (Vellocity.y < -10)
{
Vellocity.y = -10;
}
if (Vellocity.z > 10)
{
Vellocity.z = 10;
}
else if (Vellocity.z < -10)
{
Vellocity.z = -10;
}
CC.Move(new Vector3(Vellocity.x * Time.fixedDeltaTime, Vellocity.y * Time.fixedDeltaTime, Vellocity.z * Time.fixedDeltaTime) );
if (RightTargeted)
{
DrillRight.transform.SetPositionAndRotation(TargetRight.position, DrillRight.transform.rotation);
if (Input.GetAxis("RightRopeFire") == 0)
{
RightTargeted = false;
DrillRight.transform.position = RopeStartRight.transform.position;
TargetRight = DrillRight.transform;
}
else if (Vector3.Distance(Player.transform.position, TargetRight.position) > RightRopeDistance)
{
/*Vector3 Temp = Player.transform.position - TargetRight.position;
Temp.Normalize();
Temp.Set(Temp.x * RightRopeDistance, Temp.y * RightRopeDistance, Temp.z * RightRopeDistance);
RaycastHit hit;
if (Physics.Linecast(Player.transform.position, TargetRight.position + Temp, out hit))
{
Player.transform.SetPositionAndRotation(hit.transform.position, Quaternion.Euler(hit.normal));
Debug.Log("Collided");
}
else
{
Player.transform.position = TargetRight.position + Temp;
}
*/
Vector3 Temp = Player.transform.position - TargetRight.position;
Temp.Normalize();
Temp.Set(Temp.x * RightRopeDistance, Temp.y * RightRopeDistance, Temp.z * RightRopeDistance);
CC.Move((Player.transform.position - TargetRight.position) - Temp);
Player.transform.rotation = Quaternion.FromToRotation(Vector3.up, -Temp);
//Vellocity = Player.transform.position - StartPossition;
}
}
else
{
Vector3 temp = new Vector3(RopeStartRight.transform.position.x, RopeStartRight.transform.position.y, RopeStartRight.transform.position.z);
DrillRight.transform.SetPositionAndRotation(temp, RopeStartRight.transform.rotation);
if (!(Input.GetAxis("RightRopeFire") == 0))
{
Vellocity = Vector3.zero;
RaycastHit Hit;
Vector3 Rot = PlayerCamera.transform.forward;
Vector3 RayOrigin = PlayerCamera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0.0f));
if (Physics.Raycast(RayOrigin, Rot, out Hit, 60))
{
RightTargeted = true;
TargetRight.position = Hit.point;
DrillRight.transform.SetPositionAndRotation(TargetRight.position, Quaternion.Euler(Rot));
RightRopeDistance = Vector3.Distance(Player.transform.position, TargetRight.position);
}
}
}
Vector3[] RightPossitions = new Vector3[2];
RightPossitions[0] = RopeStartRight.transform.position;
RightPossitions[1] = TargetRight.position;
RightRope.SetPositions(RightPossitions);
Collided = false;
}
void OnCollisionStay(Collision collision)
{
Collided = true;
Debug.Log("True");
}
void OnCollisionExit(Collision collision)
{
Collided = false;
}
}
Anybody have any input on how i could fix these issues?
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612162450im_/https://answers.unity.com/themes/thub/images/avi.jpg)