Hello guys,
I am kinda new to Unity (working in Unity for 5weeks). I am trying to make simple Hockey game where I control my own character and puck.
I didn’t got really far, I have so far right click movement via NavMeshAgent, Hockey rink etc.
What Im trying to achieve is:
- Player that is spawned is spawning without puck with stick (I got this one done)
2)When he moves or more likely collides with puck, then puck follows the blade of stick which is puck handling / control - I kinda have this one done aswell - When Player press left click mouse button, puck is getting shot / moved towards the left click position
Code is here:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParentChildrenScript : MonoBehaviour
{
public GameObject mPlayer;
public GameObject mtPuck;
public bool playerHitsAPuck = false;
public Rigidbody mPuck;
public Rigidbody mrbPlayer;
public LayerMask whatCanBeClickedOn;
public int shotForce = 100;
void OnCollisionEnter(Collision collidingWith)
{
if (collidingWith.collider.name == "Puck")
{
//mrbPlayer.isKinematic = true;
//mPuck.isKinematic = true;
Debug.Log("Player just hit a puck");
playerHitsAPuck = true;
Debug.Log(playerHitsAPuck);
}
}
void Update()
{
if (playerHitsAPuck)
{
Debug.Log(playerHitsAPuck);
mtPuck.transform.parent = mPlayer.transform;
}
if (Input.GetMouseButtonDown(0) && playerHitsAPuck)
{
Vector3 mousePosition = Input.mousePosition;
Ray castPoint = Camera.main.ScreenPointToRay(mousePosition);
RaycastHit hit;
if (Physics.Raycast(castPoint, out hit, Mathf.Infinity, whatCanBeClickedOn))
{
//mPuck.AddForce(hit.point * shotForce);
mPuck.velocity = new Vector3(shotForce, 0, 0);
playerHitsAPuck = false;
//mrbPlayer.isKinematic = false;
//mPuck.isKinematic = false;
mPlayer.transform.DetachChildren();
}
}
}
}
My problem is clear: When player hits a puck then puck starts following however the puck is starting to act really weirdly while player is moving different positions etc. And most of the time just teleports away into insane coordinates or fall through ground or walls.
I guess its caused because of Rigidbodies on Puck and Player, but I have no idea how to make it act smoothly.
My goal is to reach point where I can do like self passes, dribbles shots on goal and so on.
I hope you guys able to help a noob,
I wish you all nice summer day!
Simon