11-01-2022, 03:30 AM
sorry for the long messy code, ive been trying to make a rope that shoot form the place i want towards a diraction and connects both ends for the last couple of days and i had alot of problems but the main one i have now and cant solve is i cant get the rope curses to extend the way i want it to,here my code:
using System.Collections.Generic;
using UnityEngine;
using Obi;
public class ChainPowerUpVersion3_ObiRope : PowerUpConstructor
{
//public override int UsegeNumber { get{return 2;} }
public GameObject ExpusedOutfit;
public override GameObject Outfit { get { return ExpusedOutfit; } }
bool tryToTurnOff = false;
PowerUpHandeler HandlerIns;
public List<string> objectsToTurnOff;
public override List<string> ObjectToTurnOf { get { return objectsToTurnOff; } }
public float MaxShotDst = 20f;
public GameObject ObiRopeSolver;
public bool AtchObject = false;
float timeC = 0;
bool ShotRope = false;
public float ShotingSpeed = 30f;
float length = 0;
ObiSolver SolverComponent;
public List<int> indexList = new List<int>();
public ObiRope rope;
public ObiActor actor;
public GameObject Player;
public ObiRopeCursor RopeCurser;
Vector3 RopeTarget;
public GameObject SolverObject;
private void Update()
{
if (ShotRope)
{
//set velocity to zero
for (int i = 0; i < actor.activeParticleCount; ++i)
{
SolverComponent.velocities[actor.solverIndices[i]] = Vector3.zero;
}
//extandrope
int solverIndex = actor.solverIndices[actor.activeParticleCount - 1];
Vector3 LastPoint = SolverComponent.positions[solverIndex];
if (Vector3.Distance(LastPoint, RopeTarget) > 0.1f)
{
int solverIndex2 = actor.solverIndices[actor.activeParticleCount - 2];
Vector3 SecoundLastPoint = SolverComponent.positions[solverIndex2];
float dis = Vector3.Distance(LastPoint, SecoundLastPoint);
Vector3 WorldPos = SolverObject.transform.TransformPoint(SecoundLastPoint);
SolverComponent.positions[solverIndex] = SolverObject.transform.
InverseTransformPoint(WorldPos + (RopeTarget - WorldPos).normalized * (dis));
length += ShotingSpeed * Time.deltaTime;
RopeCurser.ChangeLength(length);
}
else
{
ShotRope = false;
SolverComponent.gravity = Vector3.down * 9.81f;
}
}
}
public override void ActivatePowerUp(GameObject player, Vector3 AimVector)
{
Player = player;
RaycastHit hit;
if (Physics.Raycast(player.transform.position, AimVector, out hit, 20f))
{
Vector3 StartPos = player.transform.position + player.transform.forward * 0.3f;
SolverObject = Instantiate(ObiRopeSolver, Vector3.zero, Quaternion.identity);
rope = SolverObject.transform.GetChild(0).GetComponent<ObiRope>();
SolverComponent = SolverObject.GetComponent<ObiSolver>();
actor = SolverComponent.actors[0];
SolverComponent.gravity = Vector3.zero;
RopeTarget = hit.point;
for (int i = 0; i < actor.activeParticleCount; ++i)
{
int solverIndex = actor.solverIndices[i];
indexList.Add(solverIndex);
}
SolverComponent.positions[indexList[0]] = SolverObject.transform.
InverseTransformPoint(player.transform.position + player.transform.forward * 0.3f);
SolverComponent.positions[indexList[indexList.Count - 1]] = SolverObject.transform.
InverseTransformPoint(player.transform.position + player.transform.forward * 0.4f);
RopeCurser = SolverComponent.GetComponentInChildren<ObiRopeCursor>();
ObiParticleAttachment atch = rope.gameObject.AddComponent<ObiParticleAttachment>();
ObiParticleGroup particalGroup = rope.ropeBlueprint.groups[0];
//ObiParticleGroup particalGroup = actor.blueprint.AppendNewParticleGroup("End Point");
atch.particleGroup = particalGroup;
atch.attachmentType = ObiParticleAttachment.AttachmentType.Dynamic;
atch.target = Player.transform.GetChild(1).transform;
ShotRope = true;
}
}
}
using System.Collections.Generic;
using UnityEngine;
using Obi;
public class ChainPowerUpVersion3_ObiRope : PowerUpConstructor
{
//public override int UsegeNumber { get{return 2;} }
public GameObject ExpusedOutfit;
public override GameObject Outfit { get { return ExpusedOutfit; } }
bool tryToTurnOff = false;
PowerUpHandeler HandlerIns;
public List<string> objectsToTurnOff;
public override List<string> ObjectToTurnOf { get { return objectsToTurnOff; } }
public float MaxShotDst = 20f;
public GameObject ObiRopeSolver;
public bool AtchObject = false;
float timeC = 0;
bool ShotRope = false;
public float ShotingSpeed = 30f;
float length = 0;
ObiSolver SolverComponent;
public List<int> indexList = new List<int>();
public ObiRope rope;
public ObiActor actor;
public GameObject Player;
public ObiRopeCursor RopeCurser;
Vector3 RopeTarget;
public GameObject SolverObject;
private void Update()
{
if (ShotRope)
{
//set velocity to zero
for (int i = 0; i < actor.activeParticleCount; ++i)
{
SolverComponent.velocities[actor.solverIndices[i]] = Vector3.zero;
}
//extandrope
int solverIndex = actor.solverIndices[actor.activeParticleCount - 1];
Vector3 LastPoint = SolverComponent.positions[solverIndex];
if (Vector3.Distance(LastPoint, RopeTarget) > 0.1f)
{
int solverIndex2 = actor.solverIndices[actor.activeParticleCount - 2];
Vector3 SecoundLastPoint = SolverComponent.positions[solverIndex2];
float dis = Vector3.Distance(LastPoint, SecoundLastPoint);
Vector3 WorldPos = SolverObject.transform.TransformPoint(SecoundLastPoint);
SolverComponent.positions[solverIndex] = SolverObject.transform.
InverseTransformPoint(WorldPos + (RopeTarget - WorldPos).normalized * (dis));
length += ShotingSpeed * Time.deltaTime;
RopeCurser.ChangeLength(length);
}
else
{
ShotRope = false;
SolverComponent.gravity = Vector3.down * 9.81f;
}
}
}
public override void ActivatePowerUp(GameObject player, Vector3 AimVector)
{
Player = player;
RaycastHit hit;
if (Physics.Raycast(player.transform.position, AimVector, out hit, 20f))
{
Vector3 StartPos = player.transform.position + player.transform.forward * 0.3f;
SolverObject = Instantiate(ObiRopeSolver, Vector3.zero, Quaternion.identity);
rope = SolverObject.transform.GetChild(0).GetComponent<ObiRope>();
SolverComponent = SolverObject.GetComponent<ObiSolver>();
actor = SolverComponent.actors[0];
SolverComponent.gravity = Vector3.zero;
RopeTarget = hit.point;
for (int i = 0; i < actor.activeParticleCount; ++i)
{
int solverIndex = actor.solverIndices[i];
indexList.Add(solverIndex);
}
SolverComponent.positions[indexList[0]] = SolverObject.transform.
InverseTransformPoint(player.transform.position + player.transform.forward * 0.3f);
SolverComponent.positions[indexList[indexList.Count - 1]] = SolverObject.transform.
InverseTransformPoint(player.transform.position + player.transform.forward * 0.4f);
RopeCurser = SolverComponent.GetComponentInChildren<ObiRopeCursor>();
ObiParticleAttachment atch = rope.gameObject.AddComponent<ObiParticleAttachment>();
ObiParticleGroup particalGroup = rope.ropeBlueprint.groups[0];
//ObiParticleGroup particalGroup = actor.blueprint.AppendNewParticleGroup("End Point");
atch.particleGroup = particalGroup;
atch.attachmentType = ObiParticleAttachment.AttachmentType.Dynamic;
atch.target = Player.transform.GetChild(1).transform;
ShotRope = true;
}
}
}