[ad_1]
i have been creating a 3rd individual journey sport on Unity with a grappling hook and i discovered an issue that i do not perceive and it is driving me mad.
Basically, if the participant character collides sturdy with one other object it will not cease transferring with out my management.
Can you assist me?
Here’s a gameplay video (the bug begins on 00:20): https://www.youtube.com/watch?v=2XAw-nIMSvQ&characteristic=youtu.be
Here’s a screenshot of the inspector of the participant character and the article that i hooked.
Here’s the character controller code:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class ThirdPersonCharacterManagement : MonoBehaviour
{
public float Speed;
//
public Rigidbody rb;
//
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
PlayerMotion();
}
void PlayerMotion()
{
float hor = Input.GetAxis("Horizontal");
float ver = Input.GetAxis("Vertical");
Vector3 playerMovement = new Vector3(hor, 0f, ver) * Speed * Time.deltaTime;
remodel.Translate(playerMovement, Space.Self);
//Debug.Log("Movimento");
}
///detetor de colisões
void OnCollisionEnter (Collision col)
{
if(col.gameObject.tag == "collidable")
{
Debug.Log("Tocou");
}
}
///
}
Here’s the digital camera controller:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class ThirdPersonCameraControl : MonoBehaviour
{
float rotationSpeed = 1;
public Transform Target, Player;
float mouseX, mouseY;
//aqui meto os obstructions (onde podes fazer grappling hook)
public Transform Obstruction1;
public Transform Obstruction2;
public Transform Obstruction3;
///
float zoomSpeed = 2f;
void Start()
{
Obstruction1 = Target;
Obstruction2 = Target;
Obstruction3 = Target;
Cursor.seen = false;
Cursor.lockState = CursorLockMode.Locked;
}
non-public void LateUpdate()
{
CamControl();
// ViewObstructed();
}
void CamControl()
{
mouseX += Input.GetAxis("Mouse X") * rotationSpeed;
mouseY -= Input.GetAxis("Mouse Y") * rotationSpeed;
mouseY = Mathf.Clamp(mouseY, -35, 60);
remodel.LookAt(Target);
if (Input.GetKey(KeyCode.LeftShift))
{
Target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
}
else
{
Target.rotation = Quaternion.Euler(mouseY, mouseX, 0);
Player.rotation = Quaternion.Euler(0, mouseX, 0);
}
}
}
Here’s the grappling hook code:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class GrapplingHook : MonoBehaviour
{
public GameObject hook;
public GameObject hookHolder;
public float hookTravelSpeed;
public float playerTravelSpeed;
public bool leaping;
public static bool fired;
public bool hooked;
public GameObject hookedObj1;
public float maxDistance;
non-public float presentDistance;
public bool grounded;
public Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
//firing the hook
if (Input.GetMouseButtonDown(1)/*Input.GetKeyDown(KeyCode.E)*/ && fired == false)
{
fired = true;
}
if (fired)
{
LineRenderer rope = hook.GetComponent<LineRenderer>();
rope.SetVertexCount(2);
rope.SetPosition(0, hookHolder.remodel.place);
rope.SetPosition(1, hook.remodel.place);
}
if (fired == true && hooked == false)
{
hook.remodel.Translate(Vector3.ahead * Time.deltaTime * hookTravelSpeed);
presentDistance = Vector3.Distance(remodel.place, hook.remodel.place);
if (presentDistance >= maxDistance)
{
ReturnHook();
}
}
if (hooked == true && fired == true)
{
hook.remodel.guardian = hookedObj1.remodel;
remodel.place = Vector3.MoveTowards(remodel.place,
hook.remodel.place, Time.deltaTime * playerTravelSpeed);
float distanceToHook = Vector3.Distance(remodel.place, hook.remodel.place);
this.GetComponent<Rigidbody>().useGravity = false;
if (distanceToHook < 1)
{
if (grounded == false)
{
this.remodel.Translate(Vector3.ahead * Time.deltaTime * 13f);
this.remodel.Translate(Vector3.up * Time.deltaTime * 18f);
}
StartCoroutine("Climb");
}
}
else
{
hook.remodel.guardian = hookHolder.remodel;
this.GetComponent<Rigidbody>().useGravity = true;
}
CheckIfGround();
PlayerLeap();
}
IEnumerator Climb()
{
yield return new WaitForSeconds(0.1f);
rb.AddForce(new Vector3(0, 3, 0), ForceMode.Impulse);
ReturnHook();
}
void ReturnHook()
{
hook.remodel.rotation = hookHolder.remodel.rotation;
hook.remodel.place = hookHolder.remodel.place;
fired = false;
hooked = false;
LineRenderer rope = hook.GetComponent<LineRenderer>();
rope.SetVertexCount(0);
}
public void CheckIfGround()
{
RaycastHit hit;
float distance = 1.1f;
Vector3 dir = new Vector3(0, -1, 0);
if (rb.velocity.y <= 0.1 && Physics.Raycast(remodel.place, dir, out hit, distance))
{
grounded = true;
leaping = false;
//Debug.Log("Grounded");
}
else
{
grounded = false;
}
}
void PlayerLeap()
{
if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
{
rb.AddForce(new Vector3(0, 20, 0), ForceMode.Impulse);
leaping = true;
//print("leaping");
}
else if (Input.GetKey(KeyCode.Space) && grounded == false && rb.velocity.y < -1)
{
this.GetComponent<Rigidbody>().useGravity = false;
}
}
}
And the hook detector:
utilizing System.Collections;
utilizing System.Collections.Generic;
utilizing UnityEngine;
public class HookDetector : MonoBehaviour
{
public GameObject participant;
void OnTriggerEnter(Collider different)
{
if(different.tag == "Hookable")
{
participant.GetComponent<GrapplingHook>().hooked = true;
participant.GetComponent<GrapplingHook>().hookedObj1 = different.gameObject;
//Debug.Log("Tocou");
}
}
}
[ad_2]