- Home /
This question was
closed Sep 22, 2019 at 03:07 AM by
Beginner_at_Unity for the following reason:
Correct code finally found.
Question by
Beginner_at_Unity · Aug 02, 2019 at 07:39 AM ·
gameballbehaviourstrange
Flick soccer: Code correct but ball refuses to turn left/right
Update: I checked my code with the instructor's code and there's nothing wrong:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameController : MonoBehaviour
{
[SerializeField]
GameObject ballPrefab;
[SerializeField]
float ballForce;
GameObject ballInstance;
Vector3 mouseStart;
Vector3 mouseEnd;
float minDragDistance = 15f;
float zDepth = 25f;
void Awake()
{
}
// Use this for initialization
void Start()
{
CreateBall();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
mouseStart = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
mouseEnd = Input.mousePosition;
if (Vector3.Distance(mouseEnd, mouseStart) > minDragDistance)
{
//throw ball
Vector3 hitPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zDepth);
hitPos = Camera.main.ScreenToWorldPoint(hitPos);
ballInstance.transform.LookAt(hitPos);
ballInstance.GetComponent<Rigidbody>().AddRelativeForce(Vector3.forward * ballForce, ForceMode.Impulse);
Invoke("CreateBall", 2f);
}
}
}
void CreateBall()
{
ballInstance = Instantiate(ballPrefab, ballPrefab.transform.position, Quaternion.identity) as GameObject;
}
}
Comment
Answer by Santifocus · Sep 21, 2019 at 05:19 PM
ballInstance.transform.forward NOT Vector3.forward Vector3.forward = Vector3(0,0,1)
Answer by Beginner_at_Unity · Sep 22, 2019 at 02:54 AM
Thank you very much. This issue has been leftout for many months.
Follow this Question
Related Questions
how can i restrict Ball movement in -ve X axis ......? 1 Answer
Dynamic Batching Strange Scale Behaviour 0 Answers
Ball Falling Game. 2 Answers
Raycast with strange Behaviour 0 Answers
Floor and Sphere collision, strange physic behavior 1 Answer